CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    23

    char* to string^ conversion

    hi

    i have linked list in which the node holds a char* to represent a string linked list is done using native code but for me to display it in a text box i have to make it a String^ but the problem is im able to insert the string value to this char* using the following coding

    IntPtr p = Marshal::StringToHGlobalAnsi(txtName->Text);
    char* pAnsiansiName = static_cast<char*>(p.ToPointer());
    list->appendNode(pAnsiansiName,pio,jobID);

    here list is the link list and pAnsiansiName is the char*

    when debugging i see that the string ivalue is actually inserted in to

    void LinkedList::appendNode(char* na,char p,int j)
    {
    Node* n=new Node();
    n->jobID=j;
    n->name=na;
    n->piority=p;
    n->percent=0;
    n->status='q';

    Node* temp_node;
    temp_node=front;

    ........

    n->name actually receive the string but later on i cant take it back it just shows some wired character. i tried many ways at last i did this

    System::String^ getName()
    {
    gcroot<String ^> myString;
    myString = gcnew String(this->name);

    return myString;

    }

    this is implemented inside the link node
    when debugging i see that the this->name hold some wired characters
    but when i use it like this

    System::String^ getName()
    {
    gcroot<String ^> myString;
    myString = gcnew String("my name");

    return myString;

    }

    it works. so it seems some this is wrong with this->name seems it cant retain its value may i have a work around for this please.its urgent

    thankz

  2. #2

    Re: char* to string^ conversion

    The char array assigned to n->name is probably not NULL terminated if you are seeing weird characters.

    You need to append a NULL char to all char arrays. (0 or '\0')

    Otherwise... The following WOULD work.

    Code:
    char * pData = new char[128];
    
    ::strcpy(pData,"This is some test data."); // This char array has an implicit null character
    
    String ^ gcString = gcnew String(pData);
    
    delete [] pData;
    
    MessageBox::Show(gcString);

  3. #3
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    Re: char* to string^ conversion

    Your code is not clear. If name is a native char * then you need to convert from String back to char * like this:
    Code:
       char* str2 = (char*)(void*)Marshal::StringToHGlobalAnsi(managedString);       
       strncpy(destNativePtr, str2); // Or whatever you want
       Marshal::FreeHGlobal(str2); // Required
    StringToHGlobalAnsi() allocates memory for you that needs a call to FreeHGlobal() in order to release it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured