Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Call C# DLL from (unmanaged) C++: question on passing out string parameter

Call C# DLL from (unmanaged) C++: question on passing out string parameter

Scheduled Pinned Locked Moved C#
questioncsharpc++htmlcom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Koushik Biswas
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • K Koushik Biswas

      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

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      Strings 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups