Problem calling unmanaged C++ Library from C# through managed wrapper

A

Adam Clauss

I have an unmanaged C++ library that I need to use through C#. I created a
Managed C++ class library and through it wrote a wrapper class to call what
I need.
However, one of the methods (an Initialize function in the unmanaged
library) crashes everytime I call it. Initialize calls another function
'GetInstance' (a static member function of a class) which looks something
like:


CLibConfig& CLibConfig::GetInstance()
{
//Our instance (to avoid singleton problems!)
static CLibConfig sInstance; <--------

//Return it
return sInstance;
}

It is on the static sInstance line that I get a debug assertion.
in dbheap.c, Line1291.
_CrtIsValidHeapPointer(pUserData).

The static line is the last line from the call stack within the C++ library,
from there it goes into the C runtime. Any suggestions?
 
I

Idael Cardoso

After doing something similar to use th Windows Media Format SDK in managed
code I learn that the best one can do is let the unmanaged code in unmanaged
world :) The problem of initialization of unmanaged static variables can
arise frequently as manged functions that need CRT initialization because
the initialization code don't run when compiled to produce managed code.
What I advise is wripper your wrapper as COM object(s) or make all include
all your unmanaged library in an unmanaged DLL that you can use by your
managed wrapper.

Hope this can helps, if you are interesting in what I did you can look it
at: http://www.thecodeproject.com/useritems/WmaCompressor.asp (the url can
change in the future)

Best regards,
Idael.
 

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