在我的C#應用程序中,我有IntPtr y = VoidPointer(x);
其中VoidPointer(x)是一個返回void *(void指針)的非託管函數。問題是我沒有得到y在C#代碼中指出的正確值。我使用Marshal.ReadInt32(y)
並試圖ReadByte
,ReadInt64
等下面是代碼片段,
託管函數在C:我們如何訪問C#中C指針指向的值?
void* VoidPointer()
{
int Var1 = 7113;
return &Var1;
}
在C#
管理功能:(使用的DllImport訪問非託管函數)
IntPtr z = VoidPointer();
Console.WriteLine(" z = {0} ", Marshal.ReadInt32(z));
但是在輸出中,我沒有得到7113.我怎樣才能在C#中訪問Var1的正確值?
作爲,嘗試定義的C整數靜態和你發佈的代碼再試一次。 –