473,805 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixed mode DLL and Loader Lock issue - revisited

I have read and re-read KB 814472. There appears to be a little 1984
New Speak in it.

I am referring to option 3.
DLL that contains consumers that use managed code and dll exports or
managed entry points

The minitialize and mterminate functions are straight forward and make
sense. The problem is with the code

BOOL WINAPI DllMain(HINSTAN CE hModule, DWORD dwReason, LPVOID
lpvReserved) {
Console::WriteL ine(S"DllMain is called...");
return TRUE;
} /* DllMain */
Now, this seems to me that this code violates the rule that no managed
code should be run under loader lock.

I have a c# main program.

static void Main(string[] args)
{
ManagedWrapperr .minitialize();
,,,,,
ManagedWrapperr .mterminate();
}

And to no surprise I see the following output
DllMain is called...
__crt_dll_initi alize is called...

(I added the line Console::WriteL ine(S"__crt_dll _initialize is
called...");
in minitialize after the call to __crt_dll_initi alize.

I would assume that putting a managed dll in the references (or in the
#using list) would be the same as making it statically linked in
non-managed environment. If that is the case then how does this code
actually do us any good? (The implication being that the load caused
by the referencing managed code as in turn called to OS to do the DLL
load which as already called DLLMAIN which should have run the
non-managed dll initialize (__crt_dll_init ialize).

Prior to putting the work around code into my dll, I did not have a
user defined DLLMAIN therefore my DLLMAIN should (I would expect) have
been unmanaged only, so why do I even get the loader lock problem to
start with?
-------------------------------------------
Roy Chastain
KMSystems, Inc.
Nov 16 '05 #1
1 4728
Roy,

Roy Chastain wrote:
I have read and re-read KB 814472. There appears to be a little 1984
New Speak in it.

I am referring to option 3.
DLL that contains consumers that use managed code and dll exports or
managed entry points

The minitialize and mterminate functions are straight forward and make
sense. The problem is with the code

BOOL WINAPI DllMain(HINSTAN CE hModule, DWORD dwReason, LPVOID
lpvReserved) {
Console::WriteL ine(S"DllMain is called...");
return TRUE;
} /* DllMain */
Now, this seems to me that this code violates the rule that no managed
code should be run under loader lock.
You are compiling this DLL using the /NOENTRY switch. Therefore the
DllMain function does not get called by the OS Loader. Therefore the
rule is not violated.

I have a c# main program.

static void Main(string[] args)
{
ManagedWrapperr .minitialize();
,,,,,
ManagedWrapperr .mterminate();
}

And to no surprise I see the following output
DllMain is called...
__crt_dll_initi alize is called...

(I added the line Console::WriteL ine(S"__crt_dll _initialize is
called...");
in minitialize after the call to __crt_dll_initi alize.

The DllMain function gets called during your
ManagedWrapperr .minitialize() call, not when the DLL is loaded.
I would assume that putting a managed dll in the references (or in the
#using list) would be the same as making it statically linked in
non-managed environment. If that is the case then how does this code
actually do us any good? (The implication being that the load caused
by the referencing managed code as in turn called to OS to do the DLL
load which as already called DLLMAIN which should have run the
non-managed dll initialize (__crt_dll_init ialize).
Could you rephrase this paragraph?

Prior to putting the work around code into my dll, I did not have a
user defined DLLMAIN therefore my DLLMAIN should (I would expect) have
been unmanaged only, so why do I even get the loader lock problem to
start with?


If you have a mixed DLL, VC++ always creates a DllMain that calls into
managed code.

See
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/dv_vstechart/html/vcconMixedDLLLo adingProblem.as p>.

In any case, it might be better to avoid mixed DLLs completely, by
splitting the Managed C++ project into a pure Win32 C++ project and a
C++ Class Library project. In the C# project, you can use P/Invoke and
pointer manipulation (using the "unsafe" keyword). The biggest
disadvantage of this is probably that you have to manually translate the
unmanaged function prototypes into P/Invoke method declarations. If your
unmanaged code is stable, this is doable, but if it evolves in parallel
with your managed code, it's a pain.

Bart Jacobs

Nov 16 '05 #2

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

Similar topics

8
4528
by: Ted Miller | last post by:
Hi folks, I'm looking at moving a large base of C++ code to .Net under tight time constraints. The code runs in mission-critical environments, and I am extremely concerned about the loader lock problem and the potential for deadlocks. After pouring over the available information, and trying a few experiments, I am still left with a few questions and issues I hope someone out there can shed some light on.
2
5422
by: Craig Klementowski | last post by:
Pardon the cross post, but I'm not sure where exactly to post this question. We have MFC application using many MFC extention DLL's. We started using a new MFC extention DLL that is mixed mode so we could use some .Net components we have built. We are getting an assertion in ~CCmdTarget when exiting our app when after making a call to Windows Forms MessageBox. On line 48, m_dwRef is 2 for some reason.
3
1537
by: Hugh Williams | last post by:
I have a mixed-mode DLL doing some SSPI stuff that needs to support a web service. The article at http://support.microsoft.com/?id=814472 says you need to manually initialize the CRT when doing mixed-mode work. The only method I need to call in the mixed-mode DLL is a single, static method of one class. Do I need to manually initialize the CRT every time I call into that DLL, or
9
2593
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and explain it. I have some questions about the instructions for creating a mixed mode DLL in the MSDN topic "Converting Managed Extensions for C++ Projects from Pure Intermediate Language to Mixed Mode" in the "Managed Extensions for C++ Reference"....
4
5572
by: bonk | last post by:
This article http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcconconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomixedmode.asp seems to be somewhat out of date. Not all of it seem to apply to vs 2005 (beta 2) and c++/CLI. I followed the instructions given there but using vs 2005 beta 2 I get some warnings about depreciation: warning CRT1001: _vcclrit.h is deprecated warning C4996:...
3
3495
by: Benjamin Harper | last post by:
Hi, I've recently upgraded a mixed-mode MFC DLL of mine from VS2003 to VS2005 using the newer /clr language. In the process I also removed the manual entry point stuff that used to be necessary. My problem is that whenever I kill a thread (ie its function exits, and DllMain gets called with DLL_THREAD_DETACH), I get the LoaderLock MDA kicking in from line 195 of dllmodul.cpp, which is simply: AFX_MANAGE_STATE(&afxModuleState); I can't...
2
1505
by: Ubergeek | last post by:
I recently came accross an article that stated that there were unresolved issued with calling native (i.e. "unmanaged" C++) from managed C++. Does anyone know what the precise details of this issue is, and if there have been any fixes/workarounds yet?. I am using C# (VC7.1) for my frontend, but all my logic is tied up in Win32 C (and some C++) dlls. If I understand the issue correctly, so long as I compile the native C/C++ Dlls with...
8
2325
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR types. The problem: I have a number of mixed-mode functions which I want reuse. These functions revolve around converting a CLR String to a C++ std::string or
4
5051
by: Joey | last post by:
Hi, I wrote a mixed-mode dll (with MFC and C++/CLI) which is called from a C#-EXE. Under special cirumstances (that is: another process sends a windows-message to my process - this message is processed by the native part and calls managed code in turn) a loader lock appears. I have tracked down the call stack - the interessting part: - mscorwks.dll (where the exception is raised)
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.