System::String to LPCSTR?

G

George Ter-Saakov

How do i call from managed C++ function wich accepts LPCSTR?

void AddName( System::String *sName )
{
AddName( (LPCSTR) sName )); -- does not compile.
}


George.
 
J

Jochen Kalmbach

George said:
How do i call from managed C++ function wich accepts LPCSTR?

LPCSTR is ANSI =>

See: StringToHGlobalAnsi (System::Runtime::InteropServices::Marshal)
http://msdn.microsoft.com/library/en-
us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclassstringtohglobala
nsitopic.asp

But conversion to ANSI is bad... because String is UNICODE... so if it
contains some unicode characters this might be lost.

Better convert to unicde: StringToHGlobalUni
or use PtrToStringChars (which is faster; if you do not need to modify the
string)

See also:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/useritems/leakfinder.asp
 
G

George Ter-Saakov

Let me rephrase.

I have a __gc class with method AddName(String sName). This class is a
wrapper for regular C++ class with method AddName(LPCSTR sName)

How do i transfer call from AddName (String ) to p->AddName(LPCSTR)?


void AddName( System::String *sName )
{
p->AddName( (LPCSTR) sName )); -- does not compile.
}

Thanks.
George.
 
G

George Ter-Saakov

Great.

thanks.

George.

Jochen Kalmbach said:
LPCSTR is ANSI =>

See: StringToHGlobalAnsi (System::Runtime::InteropServices::Marshal)
http://msdn.microsoft.com/library/en-
us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclassstringtohglobala
nsitopic.asp

But conversion to ANSI is bad... because String is UNICODE... so if it
contains some unicode characters this might be lost.

Better convert to unicde: StringToHGlobalUni
or use PtrToStringChars (which is faster; if you do not need to modify the
string)

See also:
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/useritems/leakfinder.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top