0

I have a .NET Compact Framework (CF) application on Windows CE that interacts with native some DLL files, and the application has some unknown behaviors and problems.

For now, I'm using direct calls by using the [DllImport] attribute and define a method:

[DllImport("mynative.dll")]
private static extern void HelloWorld(byte[] arrName);

I want to try to release the loaded libraries, and web researches showed me LoadLibrary and FreeLibrary. How can I find a tutorial or sample code?

Stack Overflow question How to dynamically load and unload a native DLL file? is a desktop sample, but how can it done for Compact Framework? Note that .NET Compact Framework does not have 'Marshal.GetFunctionPointerForDelegate' method.

Community
  • 1
  • 1
losingsleeep
  • 1,849
  • 7
  • 29
  • 46
  • You don't need to "release" the libraries manually. What you're doing now is just fine. – Cody Gray - on strike Aug 14 '11 at 14:06
  • @Cody, but after 5 or 6 function call to the DLL, i face some problems. for example , my windows form does not close not by clicking the Close button and not by code : this.Close(). it's so amazing that after 3/4 times , this behavior does not appear. – losingsleeep Aug 14 '11 at 14:14
  • 3
    Then you have a memory leak or something else wrong with your DLL. Your question is asking about the wrong problem. You should be fixing whatever is causing *that*, instead. `FreeLibrary` is not going to magically fix bad code. – Cody Gray - on strike Aug 14 '11 at 14:16
  • 2
    While the reason it's being asked here is questionable, this question itself is quite valid. There are cases where you might want to update a native DLL while your app is running and being able to release the library could be useful in those scenarios. – ctacke Aug 14 '11 at 17:04
  • @ctacke, and what's the way (sample code) to release the DLL in those cases? – losingsleeep Aug 15 '11 at 04:51
  • From managed code there is no way. See my answer below. – ctacke Aug 15 '11 at 13:27

1 Answers1

1

The CF runtime itself calls LoadLibrary for P/Invokes - it calls it for every different "version" of the DLL you use (so if you used "mynative.dll" in one place and just "mydll" in another, it would get called twice).

The handle returned from that call is not exposed to applications, nor should it be. There is no way, short of closing the app, to get the runtime to call FreeLibrary on that DLL, so there is no way to release it.

I'm going to have to agree with @Cody Gray's comment on the question, though, in that even if it did work, it wouldn't fix the problem. You need to actually find and fix the bug causing the bad behavior you're seeing.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Thanks guys for your help. my native DLL does some processes and fills byte[] output parameters and program flow returns to .NET and continues. how can i find where's the problem when everything works properly? – losingsleeep Aug 15 '11 at 04:57
  • You should probably write a native test harness for your DLL and debug it using a native debugger. You can also debug your native DLL by setting your managed application in the DLL project's settings under Configuration Properties > Debugging > Remote Executable field. Note you need the full deployed device path to your executable here. – Damon8or Nov 08 '11 at 21:15