[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Issue: Attempt to use MSIL code from this assembly during native code initialization

Posted on 2014-12-29
3
Medium Priority
?
752 Views
Last Modified: 2014-12-29
Hello Everyone,
 
I have the following issue I would like to share with you:
 
I added to a dynamic library with MFC feature, the /clr option.
 
Since I have added this option, the library seems to behave properly expect that I cannot set code into DllMain statment as I was doing before the modification.
 
Let's add a MessageBox after CoInitialize:
#pragma unmanaged
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	g_hThisDll = hInstance;
 
    switch (dwReason)
	{
		case DLL_PROCESS_ATTACH:
			CoInitialize(NULL);
 
			::MessageBox(NULL, "DllMain", "mpOLEAutomationCLR", MB_OK);
			break;
 
		case DLL_THREAD_ATTACH:
			break;
		case DLL_THREAD_DETACH:
			delete g_pcfOleAutomationCLR;
			CoUninitialize();
			break;
		case DLL_PROCESS_DETACH:
			break;
    }	
	return TRUE;
 
}

Open in new window


Now, if I run:
C:\Windows\SysWOW64\regsvr32.exe ".\Debug\mpOLEAutomationCLR.dll"
 
It prompts the following error dialog box:

IssueError.jpg
If I remove the MessageBox line:
 
#pragma unmanaged
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    g_hThisDll = hInstance;
 
    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
            CoInitialize(NULL);
 
            //::MessageBox(NULL, "DllMain", "mpOLEAutomationCLR", MB_OK);
            break;
 
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            delete g_pcfOleAutomationCLR;
            CoUninitialize();
            break;
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
 
}

Open in new window


The regsv32.exe command line will return the expected messagebox.
 
IssueOK.jpg
I have added the sample program project to show you or try the issue.
Sample program link
 
Does somebody have a explanation or a fix to allow me to add code into the DllMain entry point.
 
Thank you very much in advance.
Happy end year season.
Best regards.
FestiJazz
0
Comment
Question by:festijazz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 40521882
MSDN is pretty clear about that issue: http://msdn.microsoft.com/en-us/library/ms173266.aspx ("Initialization of Mixed Assemblies")

Code within DllMain must not access the CLR. This means that DllMain should make no calls to managed functions, directly or indirectly; no managed code should be declared or implemented in DllMain; and no garbage collection or automatic library loading should take place within DllMain.

I only can assume that calling an UI function like 'MessageBox()' causes some CLR initialization code to be executed, which in turn triggers the error. But, why do you need that 'MessageBox()' call in the 1st place? 'DllMain()' (http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx) should only contain the most necessary code anyway, or as MS puts it:

If you need anything but the simplest initialization then do that in an initialization function for the DLL. You can require applications to call the initialization function after DllMain has run and before they call any other functions in the DLL.
0
 
LVL 1

Author Comment

by:festijazz
ID: 40521919
Hello jkr,

Thank you for your reply.

Let me comment a bit more about my query.

Before I migrated the code from vs6 to vs2010, I added into DllMain, the HeapCompatibilityInformation method to support: low-fragmentation heap.
	switch ( Reason ) 
	{

		case DLL_PROCESS_ATTACH:
			if (TRUE)
			{
				for (i = 0; i < nheaps; i++) 
				{
					HeapSetInformation(heaps[i],
						HeapCompatibilityInformation,
						&HeapFragValue,
						sizeof(HeapFragValue));
                               }
			}
                        break;
        }

Open in new window



As I am not sure if this is correctly executed.  I added a messagebox to validate the execution but with the CLR option this does not work and it reports the message mentioned in the issue description.

How could I then allow this ?
Do I need  to move it from the DllMain to a DllInit method ?

Thank you very much in advance.
Best regards,
Michel
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40521970
>> Do I need  to move it from the DllMain to a DllInit method ?

That is exactly what MS recommends, and I concur. BTW, if you need diagnostic output, 'MessageBox()' is overkill. I'd rather suggest using 'OutputDebugString()' (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362(v=vs.85).aspx) and a viewer like 'DebugView' (http://technet.microsoft.com/en-us/sysinternals/bb896647)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question