unmanged libs and /clr

K

kilroytrout

When trying to use some unmanged C++ libs in a VS 2005 Release
Candidate Windows Forms (/clr) project, I found that any dynamic
initialization in the lib's global STL objects (perhaps other C++
objects as well) create an exception at startup. Has anyone seen this
or know a workaround?

For example create a simple /clr C++ console app:

#include "unmanaged.h"

int main(array<System::String ^> ^args)
{
unmanaged_foo(); // empty func. to get linker to include library
globals
return 0;
}

Then create an unmanaged static lib with the header...

// unmanaged.h

void unmanaged_foo();

And the cpp file...

// unmanaged.cpp

#include <vector>

std::vector<int> stlVect;
void unmanaged_foo(){}


Compile the lib, and then compile and link the console app with the
unmanaged lib using /MDd (or /MD) and run. You'll get an exception in
the dynamic initializer for stlVect. I know in VS 03 there were some
issues with the and mixed mode C++ DLL's, but this is an EXE.

Tell me I'm missing something. It seems likely that various static libs
developers might use will have this problem if they're native and
linked into /clr projects. Recompiling them to /clr is not always an a
viable option.

--jeff
 
H

Holger Grund

When trying to use some unmanged C++ libs in a VS 2005 Release
Candidate Windows Forms (/clr) project, I found that any dynamic
initialization in the lib's global STL objects (perhaps other C++
objects as well) create an exception at startup. Has anyone seen this
or know a workaround?

IIRC, this is due to the signature of your main function. The
managed signature, __clrcall (array<String^>^) that is, doesn't
really work right with mixed mode yet.

Just use the ordinary (int,char*[]) signature for now.

-hg
 
K

kilroytrout

Indeed that worked for a console /clr app, and I would have never
guessed that was the culprit. But I still get the exception when
linking this unmanaged lib to a Windows forms app.

For Windows Forms, I couldn't make it (int,char*[]) without a linker
error (invalid /clr entry point), but I tried main(void) and I still
get the exception. Do you have any suggestion for a WinForms app in
this scenario?

--thanks,

jeff
 
K

kilroytrout

Ok I answered my follup question to you about Winforms. I changed the
signature to int __stdcall WinMain(int,char*[]) and no more exception
the the unmanged lib's initializers.

Many thanks.

jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top