Solved

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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