This project is read-only.

testing a MFC-Dll with com and serialisation

Mar 17, 2011 at 1:42 PM

I'm testing winUnit since a couple of days and encountered the following "issue" which may be interesting to others as well:

i want to test classes and methods from another dll. to do this i link the .lib into my winunit-testdll and include the needed header.

well... things work until

- the tested dll uses com-components. this fails until i add somewhere "::CoInitialize(0) ;"  pretty early in my test-dll (of yourse no winunit-issue but still important to know)

- the tested dll also uses the mfc-serialisation meachanism. i wanted to test that and serialized an object from within the test-dll. every call to deserialisazion failed with an exception. my class could not get created dynamically. well, after some hacking, testing and googleing i found out that you have to:

1. call "InitDllToTest" very early in the testing-dll. this is a call to some function in the tested dll which calls "new CDynLinkLibrary()". 

2. call "AFX_MANAGE_STATE(AfxGetStaticModuleState());"  in every testfunction as 1st statement bevor you do anything woth MFC-classes. after that (de-)serialisation works for me.

please refer to


so maybe it would be a good idea to add "AFX_MANAGE_STATE(AfxGetStaticModuleState());"-calls to the winunit-macros so it gets called automatically at every test?




Mar 31, 2011 at 8:39 PM

Calling CoInitialize and AFX_MANAGE_STATE(AfxGetStaticModuleState()); is application specific.

You can always create your own methods\macros that wrap around the winunit macros so that they call your application specific functions.