PkLab.Net - Meccatronica
Italiano English

C++/CLI String^ to char* conversion

Last update: 20/11/2009 - Viewed: 1732 dal 21 Sep 2009 - Vote:0.0 (0 Votes)
Translation for this document is not available or is not complete,
if you are intrested to receive information please write to

A tecnique to convert 'System::String' to 'char*' in MC++

Ottenere questa conversione non è una operazione immediata. La funzioni  StringToHGlobalUni, StringToHGlobalAnsi, StringToHGlobalUni, possono essere utilizzate allo scopo.

StringToHGlobalAnsi copies the contents of a managed String object into native heap, and then converts it into American National Standards Institute (ANSI) format on the fly. This method allocates the required native heap memory.

Questa funzione alloca la memoria necessaria e copia il contenuto di una System::String^ in un buffer nella memoria heap. La copia, quindi la quantità di byte allocati, avviene utilizzando il formato richiesto (ANSI o UNICODE) quindi è necessario utilizzare il cast appropriato per la lettura della stringa.

La versione da utilizzare dipende dal formato con cui è impostato il proprio ambiente di sviluppo, oppure da tipo di variabile che si vuole in uscita. Solitamente Visual Studio è configurato con UNICODE, quindi utilizzare la versione relativa.

La versione AUTO seleziona automaticamente il tipo di formattazione

Quindi con Visual C++ 8 (2005) si procede in questo modo:

    char * a;
    String ^ str = "Pk Lab dot net";

    // (Marshalling)Conversione del tipo String a char *
// Viene allocata memoria non gestita per contenere la stringa a = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str); // Utilizzo della variabile char *
printf(a); // Libero la memoria allocata per la stringa // La variabile 'a' non è piu' valida System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)(void*)a);

Allo stesso modo è possibile convertire da System::String^ a LPCWSTR

    LPCWSTR astring;
    System::String ^ str = "Pk Lab dot net";

    IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalUni(str);
    astring = (LPCWSTR)ptr.ToPointer();

    ... utilizzare la variabile 'astring'
    
     // Libero la memoria allocata per la stringa
     // Ora la variabile 'astring' non è più valida
    System::Runtime::InteropServices::Marshal::FreeHGlobal(ptr)

Vengono suggeriti anche altri metodi che usano PtrToStringChars o la classe CString.

Vedi anche:

 

Keywords: C/CPP, Visual Studio
Categories: Visual Studio
Vote this page
0    1    2    3    4    5   

Leave your comment:

Name:

Email:

Emails will not be visible or used in any way, and are not required

Comment:


Verify code:


Type the code you can see here on the left
 

Note:
  • Please keep comments relevant
  • Any content deemed inappropriate or offensive may be edited and/or deleted
  • No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your No HTML code is allowed. Please use BBCode to format your text
  • URLs, complete of "http://" o "mailto:" , will be auto-linked
  • Nota: questo materiale è di proprietà di Pk Lab ed è utilizzabile liberamente a condizione di citarne la fonte
    [Home Page]  [Search]
    Copyright PkLab (c) 2009
    Contatto: