Connecting Tech Pros Worldwide Forums | Help | Site Map

unmanaged dll calls and locks managed dll

Newbie
 
Join Date: Sep 2007
Posts: 5
#1: Sep 25 '07
Hi everybody.
I'm trying to call a managed dll from an unmanaged dll.
Everything is working fine except for the fact that after the execution the managed dll remains locked and I can't modify it. This is a big problem for what I've got to do.
what happens int the managed seems to be uninfluent.
here's a piece of my code in the unmanaged (calling) dll, :
Expand|Select|Wrap|Line Numbers
  1. unsigned __stdcall dlg2( void *ch )
  2. {
  3. HMODULE module;
  4. module =  LoadLibrary(L"c:\\bck_tools\\bck_main_nx4\\dlllib\\ds_18.dll");
  5. FARPROC proc = GetProcAddress(module, "myproc");
  6.  
  7. proc();
  8. FreeLibrary(pp);
  9. _endthreadex(0);
  10. return 0;
  11. void do_it()
  12. {
  13. Hole_Maker hm;
  14. hThread=_beginthreadex(NULL, 0, (hole_gui2 )dlg2, (void *)&hm,0,&ret);
  15. }
  16.  
Thanks, Peppe.

arunmib's Avatar
Member
 
Join Date: May 2007
Posts: 104
#2: Sep 25 '07

re: unmanaged dll calls and locks managed dll


what do you mean by the statement "dll locked and can't modify it" ?? I am not able to follow....
I presume that you are not able to access the "myproc" function in it. Did you check the return values of all the APIs?

Expand|Select|Wrap|Line Numbers
  1. module = LoadLibrary(L"c:\\bck_tools\\bck_main_nx4\\dlllib\\ds_18.dll");
  2.  
  3. FreeLibrary(pp);
  4.  
What is the "pp" in the FreeLibrary? I don't see it anywhere in your code? The input parameter for FreeLibrary function should be the HMODULE value returned by the LoadLibrary

Expand|Select|Wrap|Line Numbers
  1. module = LoadLibrary(L"c:\\bck_tools\\bck_main_nx4\\dlllib\\ds_18.dll");
  2.  
  3. FreeLibrary(module);
  4.  
If you don't free the library loaded properly during the first time calling the LoadLibrary will fail in the second time call....

Enlighten me if I have said or seen anything wrong
Newbie
 
Join Date: Sep 2007
Posts: 5
#3: Sep 25 '07

re: unmanaged dll calls and locks managed dll


Hi Arunmib
Thanks for you answer.

First of all I missed an important information. There's a cad Program (NX) which executes the unmanaged dll (which calls the managed)

1) When I say "dll locked and can't modify it" I mean that dll file is locked and I can't delete it or overwrite with the new version. I'm able to access to "myproc". The file is locked by the cad. Restarting the cad the file comes deletable.

2) FreeLibrary(pp); was a copy\paste error, in the code is FreeLibrary(module);

3) FreeLibrary(module) returns 1. I've also tried to call FreeLibrary(module) more then once and I noticed that it returns 1 two times, after that it always returns 0. Nothing changed (the dll is still locked).

4) I've also tried to use CreateEvent and CloseEvent API (CloseHandle too). No Change. Same result calling myproc without creating a new thread.

5) I've also tried to create a "bridge" dll to use a domain.
NX (the cad) calls the unmanaged wich calls the bridge wich calls myproc.
Bridge dll has with the following code:
extern "C" __declspec(dllexport) void bridge_proc(void *ref)
{
AppDomain ^Temporary = AppDomain::CreateDomain("BCK_Temporary");

HMODULE module = LoadLibrary(L"c:\\bck_tools\\bck_main_nx4\\dlllib\ \ds_18.dll");
FARPROC aa = GetProcAddress(pp, "myproc");
hole_gui hh =(hole_gui)aa;
hh(ref);
FreeLibrarymodule );
AppDomain::Unload(Temporary);
}
but I didn't succeed.

Thanks in advance,
Peppe.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#4: Sep 26 '07

re: unmanaged dll calls and locks managed dll


This code:
Quote:

Originally Posted by peppebck

HMODULE module = LoadLibrary(L"c:\\bck_tools\\bck_main_nx4\\dlllib\ \ds_18.dll");

isn't correct. LoadLibrary is a macro. It resolves to LoadLibraryA or LoadLIbraryW based on the character set used. LoadLibraryW needs an LPCWSTR. That L in front of your literal does not make an LPCWSTR.

Use the TCHAR mappings:
Expand|Select|Wrap|Line Numbers
  1. HMODULE module = LoadLibrary(TEXT("c:\\bck_tools\\bck_main_nx4\\dlllib\\ds_18.dll"));
  2.  
and check the returned HMODULE for null to be sure the library loaded. You aren't doing that either.

Include tchar.h to get the TCHAR macros.

Likewise the return from GetProcAddress needs to be checked for null ro you will crash at run time if the function can't be found in the dll.
arunmib's Avatar
Member
 
Join Date: May 2007
Posts: 104
#5: Sep 27 '07

re: unmanaged dll calls and locks managed dll


I have another question, is the managed DLL loaded by the CAD application when it's loaded and you are using "LoadLibrary" later in unmanaged DLL(Or already by some other module of the CAD application). If this is the case "LoadLibrary" simple returns a handle to the already mapped reference and FreeLibrary releases the handle. The DLL as usual stays in the application memory and you will not be able to modify/delete it.
Newbie
 
Join Date: Sep 2007
Posts: 5
#6: Sep 27 '07

re: unmanaged dll calls and locks managed dll


Weak, the code is working, probably is not that smart but LoadLibrary works, it loads the handle and I can use it to GetProcAddress and so on. (I've also changed in multy and removed the L)

Arunmib
The managed dll is only called by the unamanged dll.
To be sure I've done something else:
I've built another application (managed exe) which simply calls and release the dll.
Nothing changed using the appdomains, AppDomain::Load, Assembly load ecc.

Moreover I've continued to look for a solution and it seams to be "normal" for managed dll. Once used the're locked...till the calling application ends.
A lot of people has the same problem and is in rage for that.
This problem is more important for who wrote application with plugins and need to update a single plugin.
It can be done. using the AppDomain, a derived class ecc.. but for my need (testing & debug) it's too complicated.
when I'll have time I'll try to implement what's in this:
http://blogs.msdn.com/ericgu/archive/2007/06/05/app-domains-and-dynamic-loading-the-lost-columns.aspx

RIgth now a copy my dll with a new name and load the newly created dll. It's Ugly but it works fine for my purpose.

Thanks a lot, Peppe.
arunmib's Avatar
Member
 
Join Date: May 2007
Posts: 104
#7: Oct 9 '07

re: unmanaged dll calls and locks managed dll


first point sorry for the delayed reply...

is your requirement is just replacing the older version managed dll with a newer version and can your CAD application be stopped during this replacing process? Mean to ask is your requirement just automating the managed dll replacement process, kind of like an update softwares do?

Sorry if my question is really dumb, but i think if the answer to my question is YES, then there is a work around?
Reply