« Guide du C++ pour .NET Programmation » : différence entre les versions
Contenu supprimé Contenu ajouté
m →Conversion des char* en String* : syntaxe wiki |
m →Conversion des String* en char* : syntaxe wiki |
||
Ligne 52 :
Cet appel '''nécessite une libération en utilisant la méthode''' ''FreeHGlobal''.
using namespace System::Runtime::InteropServices;▼
▲using namespace System::Runtime::InteropServices;
String* s = new String("hello, World");▼
const char* temp = (const char*)(Marshal::StringToHGlobalAnsi(s).ToPointer());▼
...▼
Marshal::FreeHGlobal(IntPtr((void*)temp));▼
▲String* s = new String("hello, World");
▲const char* temp = (const char*)(Marshal::StringToHGlobalAnsi(s).ToPointer());
▲...
▲Marshal::FreeHGlobal(IntPtr((void*)temp));
De la même façon que précédemment, on pourra créer des macros :
// creation▼
#define CONVERT2CHAR(x) (char*)(Marshal::StringToHGlobalAnsi(x).ToPointer())▼
▲// creation
▲#define CONVERT2CHAR(x) (char*)(Marshal::StringToHGlobalAnsi(x).ToPointer())
// libération▼
#define FREECHAR(x) Marshal::FreeHGlobal(IntPtr((void*) x ));▼
▲// libération
▲#define FREECHAR(x) Marshal::FreeHGlobal(IntPtr((void*) x ));
et les utiliser ainsi :
using namespace System::Runtime::InterropServices;▼
▲using namespace System::Runtime::InterropServices;
String* ch_1;▼
▲String* ch_1;
// allocation en mémoire▼
char* ch_2 = CONVERT2CHAR(ch_1);▼
▲// allocation en mémoire
...▼
▲char* ch_2 = CONVERT2CHAR(ch_1);
▲...
// désallocation▼
FREECHAR(ch_2);▼
▲// désallocation
▲FREECHAR(ch_2);
= Les structures de données .NET ou non =
|