473,386 Members | 1,710 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,386 software developers and data experts.

unmanaged dll calls and locks managed dll

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.
Sep 25 '07 #1
6 2518
arunmib
104 100+
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
Sep 25 '07 #2
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.
Sep 25 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:
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.
Sep 26 '07 #4
arunmib
104 100+
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.
Sep 27 '07 #5
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.
Sep 27 '07 #6
arunmib
104 100+
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?
Oct 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Neil | last post by:
I am developing a demo in C# using Managed DirectX. I wanted to use a C++ static library in the demo (its a class for handling physics), so I decided to create my Graphics classes in C#, inherit...
5
by: _BNC | last post by:
I've been adapting an older unmanaged C++ legacy app to C#---with limited success. The original app made use of an older, but straightforward, C DLL that worked efficiently under the VC++ 6 model....
5
by: Adam McKee | last post by:
We are using Visual Studio.NET 2003 in our project with .NET framework 1.1. One of our libraries is a mixed-mode dll assembly consisting of one managed C++ library, and several unmanaged C++...
5
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However,...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
13
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
2
by: peppebck | last post by:
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....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.