Advertisment

How to overcome the new bug in VC++ .NET

author-image
CIOL Bureau
Updated On
New Update

The DllMain(), which id present in all DLLS is invoked when the DLL is initially loaded and when it is unloaded .The DllMain() function handles initialization and then cleanup. Because this function is very important, the operating system does not allow any other function fo the DLL to execute. No other DLL can be loaded either.

You probably know that there are a lot of things you cannot do in DllMain(). If you try loading another DLL, accessing the registry, or invokes another DLL’s function, the application will hang. The DllMain() is meant for initializing variables. The tricky part, however, is that the compiler does not issue any warnings if you try any of these no-no’s inside the DllMain().

Advertisment

The new problem

You can not execute any MSIL either, which means that you cannot write DllMain() in managed code, nor can you invoke any function that is written in managed code. To add to your woes, the CLR can do something unexpected — it might decide to execute the garbage collector or it might try to load a DLL because one of its methods are being called somewhere. The CLR’s “features” could (it may or may not) actually cause an application that uses mixed mode DLLs.

The solution

Advertisment

Do not write your DllMain() function in managed code but then, all methods in a managed C++ class Library are in managed code. You can use a pragma to indicate that the DllMain() but then some managed code will remain. Thinking of doing away with the DllMain()? Not so easy. The compiler will invoke functions from it after generating an unmanaged entry point. What do you do? Just set the /noentry option in the Project Properties.

For example if your project name is myProject,

1. Go to Solution Explorer

2. Right-click the project, myProject, and select Properties in the shortcut menu

3. In the Properties window, click the + sign to expand the Linker folder

4. Choose the Advanced sub-section

5. Type in the Resource Only Dll property as Yes

These steps will certainly reduce your chances of being affected by the bug, but some niggling questions will remain. How do you initialize static variables and how do invoke into ATL, MFC and so on? For more info, read the article at Microsoft support website.

tech-news