Call C# DLL from (unmanaged) C++: question on passing out string parameter
-
I have written a C# DLL that I intend to call from an unmanaged C++ app. I have so far followed all the rules that Microsoft explains in this article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkcominteroppart2cservertutorial.asp[^]. My ultimate goal is to have a method in the C# DLL which will populate a string OUT parameter, and call it from C++. Something like
**void GetErrorString( int iErrorCode, ref string szErrorString )**
. Now do not take that signature seriously - because that is what the question is! What should be the signature???? And how exactly do I call it from C++? So far I have tried: Attempt 1. C# signature:void Func2( int iCode, ref char [] szOut );
C++ call:char szRetString[ 128 ]; cpi->Func2( 10, &szRetString );
Result: Runtime error Variation: Instead of "&szRetString" in C++, tried "szRetString". Same result. Variation: Instead of "ref" in C#, tried without ref. Same result. Attempt 2. C# signature:void Func2( int iCode, ref string szOut );
C++ call:char szRetString[ 128 ]; cpi->Func2( 10, &szRetString );
Result: Runtime error Variation: Instead of "&szRetString" in C++, tried "szRetString". Same result. Variation: Instead of "ref" in C#, tried without ref. Same result. Attempt 3. C# signature:void Func2( int iCode, ref StringBuilder szOut );
C++ call:char szRetString[ 128 ]; cpi->Func2( 10, &szRetString );
Result: Runtime error Variation: Instead of "&szRetString" in C++, tried "szRetString". Same result. Variation: Instead of "ref" in C#, tried without ref. Same result. And all the cross-variations of the above combinations as well!!!! Can somebody please tell me where am I going wrong? My C++ project does not have UNICODE defined. But please note that I have succeeded in calling the example provided by the MSDN article (link above) - which means I am successful in sending a read - only copy of string as IN parameter into C#. Ever wondered that microsoft examples avoid the harder part?;) Koushik Biswas If you would not be forgotten as soon as you are dead... either wri -
I have written a C# DLL that I intend to call from an unmanaged C++ app. I have so far followed all the rules that Microsoft explains in this article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkcominteroppart2cservertutorial.asp[^]. My ultimate goal is to have a method in the C# DLL which will populate a string OUT parameter, and call it from C++. Something like
**void GetErrorString( int iErrorCode, ref string szErrorString )**
. Now do not take that signature seriously - because that is what the question is! What should be the signature???? And how exactly do I call it from C++? So far I have tried: Attempt 1. C# signature:void Func2( int iCode, ref char [] szOut );
C++ call:char szRetString[ 128 ]; cpi->Func2( 10, &szRetString );
Result: Runtime error Variation: Instead of "&szRetString" in C++, tried "szRetString". Same result. Variation: Instead of "ref" in C#, tried without ref. Same result. Attempt 2. C# signature:void Func2( int iCode, ref string szOut );
C++ call:char szRetString[ 128 ]; cpi->Func2( 10, &szRetString );
Result: Runtime error Variation: Instead of "&szRetString" in C++, tried "szRetString". Same result. Variation: Instead of "ref" in C#, tried without ref. Same result. Attempt 3. C# signature:void Func2( int iCode, ref StringBuilder szOut );
C++ call:char szRetString[ 128 ]; cpi->Func2( 10, &szRetString );
Result: Runtime error Variation: Instead of "&szRetString" in C++, tried "szRetString". Same result. Variation: Instead of "ref" in C#, tried without ref. Same result. And all the cross-variations of the above combinations as well!!!! Can somebody please tell me where am I going wrong? My C++ project does not have UNICODE defined. But please note that I have succeeded in calling the example provided by the MSDN article (link above) - which means I am successful in sending a read - only copy of string as IN parameter into C#. Ever wondered that microsoft examples avoid the harder part?;) Koushik Biswas If you would not be forgotten as soon as you are dead... either wriStrings are immutable in C#, so "ref string" might not mean much, since the target would still be unchangeable. Also strings in c# are by definition Unicode, so you will likely need to define UNICODE in your c++ project. C# strings map reasonably well to BSTRs from an interop point of view. You might try making the C++ signature "Func2(10, &BSTRmystr) and match that with the stringbuilder variant odf the C# one... Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke