| How to use DllMain in a MFC dll project |
|
| MFC - 其他 | ||||
| 作者:Administrator | ||||
| 周日, 20 7月 2008 09:22 | ||||
want to add some initial code in DllMain in a MFC dll project, but after I added the code and compiled, there was a link error:
DLLMAIN.cpp is the file I created by my own and I define DllMain() in it. What’s the reason? the linker complains that I have a DllMain in DLLMAIN.cpp but there’s another DllMain in mfcs42d.lib. So how to use my own DllMain if a MFC dll project? There’s a quick answer on codeguru , but that article just show the tip without explaining it with more details. The article says, just copy MFC’s dllmodule.cpp into your own project and compile, it will be OK. It seems to be nonsense but after I tried I found it works. But why? By commenting out unnecessary lines, I find these lines are the key point:
Do you noticed the comment? it forces the inclusion of the module of dllmodule.obj. But how? A searching for _afxForceUSRDLL in MFC source code gives me the answer:
Again the MFC designer gives us a good comment: “force inclusion of DLLMODUL.OBJ”, OK, got it. Now let summary it up: Then you want to use your own DllMain in a mfc dll project, linker complains that there are two DllMain, one in your code, one in Dllmodule.obj. The solution? Tell the linker to add my dllmain.obj for __afxForceUSRDLL. So we define __afxForceUSRDLL in our own cpp file where our own DllMain is defined, then the linker will ignore mfc’s dllmodule.obj and see only one DllMain and never complains. So the solution is just to add extern “C” { int _afxForceUSRDLL; } in the file where your own DllMain is defined, copying mfc’s dllmodule.cpp is not necessary :-)
|
||||