Solved

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

Posted on 2004-03-29
2
693 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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