2009-04-12 198 views

回答

3

您可以使用Marshal.StringToHGlobalAnsi將System.String轉換爲非託管字符*。確保您完成後通過撥打Marshal.FreeHGlobal解鎖。要將字符串轉換爲數字值,可以使用常規的.NET解析函數,如Int32.Parse

0

要在本地代碼中使用託管內存,您必須首先將託管內存的內容複製到本機內存中。

因此,例如:

從託管內存複製的內容如下:

const int len = 50; 
BYTE *destination = new BYTE[nLength]; 
System::Byte source[] = new System::Byte[len]; 

System::Runtime::InteropServices::Marshal:: 
    Copy(source, 0, IntPtr((void *)destination, len); 

因爲我們正在處理的管理內存,垃圾收集可以移動和管理數據移動到另一個位置,如果我們試圖找到我們想要轉換的數據,全部都會丟失。

因此,我們希望通過__pin轉換「別針把它在內存中」從託管到非託管:

const int len = 50; 
BYTE *source    = new BYTE[len]; 
System::Byte destination[]  = new System::Byte[len]; 
BYTE __pin *managedData = &(destination[0]); 

::memcpy(source, managedData, len); 
0

只需通過

CString* name = new CString(managedName); 
轉換系統:字符串^對象,MFC的CString

其中managedName是託管字符串。

相關問題