2013-09-27 55 views
0

我不知道如何解決這個問題,並且我已經在兩小時的最佳時間內撞擊到了我的頭頂磚牆。我在下列文檔的非託管DLL的函數:由於指針不平衡堆棧

BOOL ZLNET_QueryDeviceTime(LONG lLoginID, LPZLNET_TIME pDeviceTime, int waittime=2000); 

我假定pDeviceTime是一個指針,因此我在C#代碼傳遞出,以便作爲參考使用。 在我的C#項目,我有以下聲明:

[StructLayout(LayoutKind.Sequential)] 
    public struct ZLNET_TIME 
    { 
     public Int32 dwYear; 
     public Int32 dwMonth; 
     public Int32 dwDay; 
     public Int32 dwHour; 
     public Int32 dwMinute; 
     public Int32 dwSecond; 
    } ; 

[DllImport("zlnetsdk.dll")] 
unsafe public static extern bool ZLNET_QueryDeviceTime(long lLoginID, out ZLNET_TIME pDeviceTime, int waittime); 

的我把我的功能:

ZLNET_TIME t = new ZLNET_TIME(); 
ZLNET_QueryDeviceTime(loginResult, out t, 2000); 

然而,當我跑我的項目,我得到的錯誤:

Managed Debugging Assistant PInvokeStackImbalance has detected a problem in (MY APP EXE).

Additional Information: A call to PInvoke function VP::ZLNET_QueryDeviceTime has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

然後我在ZLNET_TIME變量中沒有得到任何回報。誰能幫我這個?

+0

兩側看起來都像是在使用指針。 C++端的'LPZLNET_TIME'的定義是什麼?它是一個typedef? –

+0

爲什麼而不是參考? – doctorlove

+2

第一個參數不是* long *,它是* int *。你可能需要使用CallingConvention屬性。描述是模糊的,但如圖所示,它應該是Cdecl。 –

回答

0

C#代碼中的第一個參數應該是int而不是long。在C++端long是一個32位類型。但在C#中它是64位。

另一個潛在的問題是調用約定。你確定C++代碼是stdcall嗎?它看起來像是cdecl,除非你省略了指定調用約定的代碼。