473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

dllimport to unload dll

I have this DllImport attribute on an external C++ dll. What i am noticing that the dll isn't unloaded after the static
method is complete. I was thinking that when the method (it is a static method) completed then the dll would be unloaded.

[System.Runtime.InteropServices.DllImport(@"C:\dnld s\R3Rse_a.dll"
, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]

Is there a way i can force this to unload or will i have to use loadlibrary?

danny
Jul 14 '08 #1
3 8493
Dan Holmes wrote:
I have this DllImport attribute on an external C++ dll. What i am
noticing that the dll isn't unloaded after the static method is
complete. I was thinking that when the method (it is a static method)
completed then the dll would be unloaded.
Well, first of all, the method being static or not has nothing to do with
anything. Imported functions do not take a "this" parameter whether they're
declared as static or not, and there are no special semantics associated
with it. To lessen confusion it's a good idea to always declare an imported
function as static, though.

Second, there is no way for the runtime to know that it's safe to unload the
DLL. Many DLLs cannot be unloaded at all, at least not without crashing the
executable that loaded them. They can start new threads, allocate resources,
register callbacks, etc, and many have no proper cleanup code. It would be
quite annoying to load a DLL, call a function that starts a background
thread, and then have the runtime helpfully unload that DLL behind your
back, which would either crash the thread or force the DLL to terminate it
(barring some hax code injection).
[System.Runtime.InteropServices.DllImport(@"C:\dnld s\R3Rse_a.dll"
, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]

Is there a way i can force this to unload or will i have to use
loadlibrary?
There's always GetModuleHandle() and FreeLibrary() (twice) if you're sure
the DLL can be safely unloaded. I don't know if explicit FreeLibrary() calls
cooperate nicely with the interop loading mechanism, though. Personally, I
wouldn't bother. About the only thing explicit unloading would be good for
is to allow the DLL to be replaced while the main application is still
running, which is a dubious versioning mechanism to say the least. .NET's
explicitly unloadable AppDomains are a better match for that.

--
J.
Jul 14 '08 #2
Jeroen Mostert wrote:
Dan Holmes wrote:
>I have this DllImport attribute on an external C++ dll. What i am
noticing that the dll isn't unloaded after the static method is
complete. I was thinking that when the method (it is a static method)
completed then the dll would be unloaded.
Well, first of all, the method being static or not has nothing to do
with anything. Imported functions do not take a "this" parameter whether
they're declared as static or not, and there are no special semantics
associated with it. To lessen confusion it's a good idea to always
declare an imported function as static, though.

Second, there is no way for the runtime to know that it's safe to unload
the DLL. Many DLLs cannot be unloaded at all, at least not without
crashing the executable that loaded them. They can start new threads,
allocate resources, register callbacks, etc, and many have no proper
cleanup code. It would be quite annoying to load a DLL, call a function
that starts a background thread, and then have the runtime helpfully
unload that DLL behind your back, which would either crash the thread or
force the DLL to terminate it (barring some hax code injection).
> [System.Runtime.InteropServices.DllImport(@"C:\dnld s\R3Rse_a.dll"
, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]

Is there a way i can force this to unload or will i have to use
loadlibrary?
There's always GetModuleHandle() and FreeLibrary() (twice) if you're
sure the DLL can be safely unloaded. I don't know if explicit
FreeLibrary() calls cooperate nicely with the interop loading mechanism,
though. Personally, I wouldn't bother. About the only thing explicit
unloading would be good for is to allow the DLL to be replaced while the
main application is still running, which is a dubious versioning
mechanism to say the least. .NET's explicitly unloadable AppDomains are
a better match for that.
Fair enough though this is actually a SqlProcedure so the dll loads in SqlServer's memory space. That means if i need
to replace the dll i have to restart SqlServer. I don't want to do that.

If i used LoadLibrary and FreeLibrary would multiple calls to the dll get different instances of the dll thus enhancing
thread safety? Right now globals are shared among function calls ( i proved it). If the loadlibrary gets around that
that would be one more reason for the loadlibrary/freelibrary mechanism.

dan
Jul 15 '08 #3
Dan Holmes wrote:
Jeroen Mostert wrote:
>Dan Holmes wrote:
>>I have this DllImport attribute on an external C++ dll. What i am
noticing that the dll isn't unloaded after the static method is
complete. I was thinking that when the method (it is a static
method) completed then the dll would be unloaded.
Well, first of all, the method being static or not has nothing to do
with anything. Imported functions do not take a "this" parameter
whether they're declared as static or not, and there are no special
semantics associated with it. To lessen confusion it's a good idea to
always declare an imported function as static, though.

Second, there is no way for the runtime to know that it's safe to
unload the DLL. Many DLLs cannot be unloaded at all, at least not
without crashing the executable that loaded them. They can start new
threads, allocate resources, register callbacks, etc, and many have no
proper cleanup code. It would be quite annoying to load a DLL, call a
function that starts a background thread, and then have the runtime
helpfully unload that DLL behind your back, which would either crash
the thread or force the DLL to terminate it (barring some hax code
injection).
>> [System.Runtime.InteropServices.DllImport(@"C:\dnld s\R3Rse_a.dll"
, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]

Is there a way i can force this to unload or will i have to use
loadlibrary?
There's always GetModuleHandle() and FreeLibrary() (twice) if you're
sure the DLL can be safely unloaded. I don't know if explicit
FreeLibrary() calls cooperate nicely with the interop loading
mechanism, though. Personally, I wouldn't bother. About the only thing
explicit unloading would be good for is to allow the DLL to be
replaced while the main application is still running, which is a
dubious versioning mechanism to say the least. .NET's explicitly
unloadable AppDomains are a better match for that.
Fair enough though this is actually a SqlProcedure so the dll loads in
SqlServer's memory space. That means if i need to replace the dll i
have to restart SqlServer. I don't want to do that.
Hold on, are you telling me you've written an assembly that's hosted in SQL
Server which explicitly calls unmanaged code? Ugh.

You're jeopardizing the stability and security of the server with such
antics. Never mind unloading the DLL, loading it in the first place is a bad
idea, unless it's under your full control and you're certain it does nothing
untoward. CLR hosting in SQL Server is subject to pretty serious
restrictions precisely because they didn't want just any old code to execute
as part of SQL Server itself. Too bad unmanaged imports can just waltz
completely around that.

If at all possible I'd investigate if you really need to use an unmanaged
library to achieve your goals. If it's small and standalone functionality,
maybe reimplementing it as pure managed code is feasible. If this isn't
feasible, I'd investigate if this functionality is really needed in a stored
procedure hosted in the database itself, rather than in a separate data
access layer that uses the database server rather than trying to become a
part of it.

OK, enough sermonizing. If you really want to do this, then yes, you'll
probably want to unload the DLL after using it, and an explicit
LoadLibrary() might be a better idea than relying on the automagic loading
done by the interop layer. Note that this means you'll have to use
GetProcAddress() and carefully crafted delegates (see for example
http://blogs.msdn.com/jmstall/archiv...cAddress.aspx).
This also notes the caveats with FreeLibrary().

On the other hand, if you can't unload it, it at least forces you to
consider upgrades to the library very carefully. This is a good thing,
because bugs in the unmanaged library could have nasty consequences.
If i used LoadLibrary and FreeLibrary would multiple calls to the dll
get different instances of the dll thus enhancing thread safety?
If you mean "will multiple calls to LoadLibrary give me multiple instances
of the same DLL loaded simultaneously", then no. A DLL can be loaded only
once per process. Thread-safety has to be achieved through other means.
Ideally, the DLL is thread-aware and duplicates its per-thread data or
provides objects you can lock on. If not, you'll have to synchronize access
to the DLL yourself, for example through a mutex.

If you mean "will the state of the DLL be flushed if the DLL is unloaded and
then reloaded", it depends a bit on what mechanism the DLL uses for storing
its state. Regular C-like static variables are stored in a section of memory
that will be freed when the library is unloaded, and the DLL should free
anything else it allocated when it's unloaded, but it's possible for a DLL
to persist information across reloading (deliberately or accidentally).

A DLL becomes part of the loading process' address space, so it's not really
possible to guarantee it's not leaving mud stains on the carpet after you've
invited it in. Repeated unloading and reloading of a DLL can cause problems
if the DLL doesn't do proper cleanup.

--
J.
Jul 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Harry J. Smith | last post by:
I have written a Visual Basic program that does a long calculation and writes the results to disk as it runs. If I click the Close button the window closes but the program keeps running. How can I...
0
by: razheev | last post by:
Hi, I have a Table which contains 1 million rows. I want to do an unload of the table and do some massaging (tranforming) of data and do a load to a different table where the column attributes are...
15
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the...
2
by: Lauren Hines | last post by:
Hello, I have read numerous post stating that the only way to unload an assembly (DLL in my case) is to create a separate AppDomain, load the assembly, then unload it by calling AppDomain.Unload....
6
by: Wal Turner | last post by:
Hi there. There are various snippets on forums regarding issues with AppDomain.Unload and how it just doesnt work. Fortunately, I got it working with the base case, after much fiddling. Consider...
1
by: hal | last post by:
I have an application that includes an activex component that consumes resources that must be released when the a page is unloaded. Toward this end I subscribe to the unload event of the body...
1
by: Hal | last post by:
My most sincere gratitude to anyone who can help me work around this! I have work that needs to be done in javascript on the client whenever a page is unloaded. To this end, I subscribe to...
3
by: Gauthier Segay | last post by:
Hello, I've an application where all my pages implement a PAGE_CODE string property, this property is stored in HttpContext.Current.Items. In some page, I must persist data in session while...
11
by: Timofmars | last post by:
I'm try to Unload DB2 data from a table into a record sequential file on NT. I can an unload on Mainframe, but it doesn't seem to be an option in NT. In NT, all I can do is export/import. I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.