Valhalla Legends Forums Archive | .NET Platform | Marshaling String __gc *ptr to char *cptr

AuthorMessageTime
Myndfyr
Hey everyone,

I have some functions I want to use in C# that someone else wrote in C++ 6. I undertook the task of creating the wrapper MC++ class for it, like it said in the docs. Here's basically what I'm doing:

[code]
// original api (header file):
extern "C" void __stdcall get_Response(const char *inpBuf,
const char *outBuf,
int bufLen);
extern "C" void __stdcall do_Command(const char *inpBuf,
const char *outBuf,
int bufLen);
extern "C" void __stdcall get_VerInfo(const char *outBuf,
int bufLen);
}

// my managed .h file:
#using <mscorlib.dll>

using System::String;
public __gc class ManagedAI
{
public:
__gc String *GetResponse(String *input);
__gc String *DoCommand(String *input);
__gc String *GetVerInfo();
}

// impl. file
#import "ManagedAI.h"

String *ManagedAI::GetResponse(String *input)
{
return String::Empty;
}

String *ManagedAI::DoCommand(String *input)
{
return String::Empty;
}

String *ManagedAI::GetVerInfo()
{
char *rep = new char[512];
get_VerInfo(rep, 512);
return new String(rep);
}
[/code]

So I got the third function in, but the other two I couldn't - I keep getting compiler errors, and I can't figure out how to go from the managed System::String to a character pointer array.

Thanks for the help!

--Rob
December 4, 2003, 5:51 AM
K
Here's some code I wrote a while back:
[code]
// Managed String to char*
void mstosz(String* sIn, char* szOut)
{
   const wchar_t __pin* tmp = PtrToStringChars(sIn);

   wcstombs(szOut, tmp, sIn->Length);
   
   szOut[sIn->Length] = 0;
      return;
}

// managed string to basic_string via char *
string mstobs(String* sIn)
{
   string result;
   char* szTmp = new char[sIn->Length + 1];
   mstosz(sIn, szTmp);
   result = szTmp;
   delete [] szTmp;
   return result;
}
[/code]

Edit: You will need to include <vcclr.h> Also note that the above code doesn't check the length of the buffer in the first case.
December 4, 2003, 6:23 PM

Search