views:

259

answers:

1

In unmanaged c++ dll i have a function which takes constant std::string as argument

 Prototype : void read ( const std::string &imageSpec_ )

I call this function from managed c++ dll by passing a std::string.
When i debug the unmanaged c++ code the parameter imageSpec_ shows the value correctly but does not allow me to copy that value in other variable.

 imageSpec_.copy( sFilename, 4052 );

It shows length of imageSpec_ as 0(zero).
If i try copying like std::string sTempFileName(imageSpec_); this statement string new string is a empty string.

But for std::string sTempFileName(imageSpec_.c_str()); this statement string gets copied correctly. i.e. with charpointer string is copied correctly.

Copying this way will need a major change in unmanaged c++ code.
I am building unmanaged code in Visual studio 6.0 and managed c++ in Visual studio 2008.
Is there any specific setting or code change in managed c++ that will solve the issue?

+1  A: 

I am building unmanaged code in Visual studio 6.0 and managed c++ in Visual studio 2008.

You are trying to copy the std::string implementation of Visual studio 6.0 into the std::string implmentation of Visual Studio 2008. If you want to pass C++ objects across DLL boundaries, you have to be very careful - you must build both side of the boundary using exactly the same object layout (which is compiler specific) and library implementation and build them against compatible runtimes.

If you want to create a .dll that is useable from code compiled with a different compiler/library from that .dll, a pure C interface is the way to go.


See this related question.

Joe Gauterin