« Guide du C++ pour .NET Programmation » : différence entre les versions

Contenu supprimé Contenu ajouté
Ligne 52 :
Cet appel '''nécessite une libération en utilisant la méthode''' ''FreeHGlobal''.
 
using namespace System::Runtime::InteropServices;
<pre>
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));
</pre>
 
De la même façon que précédemment, on pourra créer des macros :
 
// creation
<pre>
#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 ));
</pre>
 
et les utiliser ainsi :
 
using namespace System::Runtime::InterropServices;
<pre>
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);
</pre>
 
= Les structures de données .NET ou non =