Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

}


Now, if I run:
C:\Windows\SysWOW64\regsvr32.exe ".\Debug\mpOLEAutomationCLR.dll"

It prompts the following error dialog box:
Errorregistration message.

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;

}


The regsv32.exe command line will return the expected messagebox.

Expected registration message.

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.
SuperMiqi
Posted
Updated 29-Dec-14 3:41am
v2

1 solution

 
Share this answer
 
v2
Comments
SuperMiQi 29-Dec-14 9:54am    
Hello Richard,

I suppose you refer to:
<<
If your DLL is linked with the C run-time library (CRT), the entry point provided by the CRT calls the constructors and destructors for global and static C++ objects. Therefore, these restrictions for DllMain also apply to constructors and destructors and any code that is called from them.
>>

The issue I am having is the hInstance validity value which seems not to be initialized properly.
To verify that, I was hoping to be able to prompt a messagebox like using the library without the clr option.
SuperMiQi 29-Dec-14 10:08am    
How could I then be able to add win32 api statment into the DllMain if I want to prompt something.
I have a licensing library where the validation prompting message is done in DllMain and if fails, it will unload the dll by returning FALSE.

What could you suggest me ?

Thank you very much in advance.
Best regards.
SuperMiqi
Richard MacCutchan 29-Dec-14 10:33am    
You should not be doing any of this in DllMain, unless it is acceptable practice as described in the MSDN documentation, per the link I gave you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900