Solved

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

Posted on 2004-03-29
2
686 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now