Solved

converting a System::String to a c++ char array

Posted on 2004-03-29
2
690 Views
Last Modified: 2008-02-01
I have a managed type of String str. I want to convert it into a C++ style char* and copy it into str1. I am using VC++.Net.

I use the following code. The str1 gets converted to const char* str2 but I get a null exception
when I try to copy the const char* str2 to char* str1. What do I need to do to get this code running? Please help.. Thank you.

//to convert a String* to char*            

char* str1 = "";

      
try {
IntPtr ptr = Marshal::StringToHGlobalAnsi(str);
const char *str2 = static_cast<const char*>(ptr.ToPointer());
            
strcpy(str1, str2); //this is the problem
            
Marshal::FreeHGlobal(ptr);
}
catch (System::Exception *x) {
// StringToHGlobalAnsi() failed.
}
0
Comment
Question by:VPitre
  • 2
2 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10710813
This is one of my mixed programming exersizes:

    String* s10 = S"char ptr";
    const __wchar_t __pin* sp = PtrToStringChars(s10);
    char* sChar = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s10);

    std::cout << sChar << std::endl;

    System::Runtime::InteropServices::Marshal::FreeHGlobal(sChar);

I don't remember why it is written by such way, but it works.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 10710851
Now I see your bug:

char* str1 = "";
...
strcpy(str1, str2);

Define str1 as:
char str1[100]; // allocate enough space
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question