DelboyDee
August 27th, 2003, 05:34 PM
Hi People,
I am writing a .NET wrapper for an old C++ API DLL. I have a function that inputs a char* for a name. My Problem is that my C++ .NET wrapper function passes a System.String* but I cannot convert this to a char*. See the following code.
long Coreco::SapManager::getServerByName(System::String* name, SapServer* Server)
{
_CORSERVER hServer;
char* strname = CopyString(name);
long ret = _CorManGetServerByName( strname , &hServer);
Server->SetServer( &hServer );
delete [] strname;
return ret;
}
char* Coreco::CopyString(System::String* srcstr)
{
char* strname = new char[srcstr->Length]; //*** This line Fails At runtime ***
for( int f=0;f<srcstr->Length;f++ ){
strname[f] = (char)srcstr->Chars[f];
}
return strname;
}
The line "char* strname = new char[srcstr->Length]; " fails at runtime saying that a nullreference exception occured!?!? basically new returned NULL (I think). Why?
Questions:
1) I have included libc.lib for the new operator implementation is this correct?
2) Am I going about this in the correct way or is there an easier way to convert from managed string types to char*?
Many Thanx,
Delboy
I am writing a .NET wrapper for an old C++ API DLL. I have a function that inputs a char* for a name. My Problem is that my C++ .NET wrapper function passes a System.String* but I cannot convert this to a char*. See the following code.
long Coreco::SapManager::getServerByName(System::String* name, SapServer* Server)
{
_CORSERVER hServer;
char* strname = CopyString(name);
long ret = _CorManGetServerByName( strname , &hServer);
Server->SetServer( &hServer );
delete [] strname;
return ret;
}
char* Coreco::CopyString(System::String* srcstr)
{
char* strname = new char[srcstr->Length]; //*** This line Fails At runtime ***
for( int f=0;f<srcstr->Length;f++ ){
strname[f] = (char)srcstr->Chars[f];
}
return strname;
}
The line "char* strname = new char[srcstr->Length]; " fails at runtime saying that a nullreference exception occured!?!? basically new returned NULL (I think). Why?
Questions:
1) I have included libc.lib for the new operator implementation is this correct?
2) Am I going about this in the correct way or is there an easier way to convert from managed string types to char*?
Many Thanx,
Delboy