我新装了2005,想做一个关机程序试一下,调用的是exitwindowsex函数,结果在运行时点下按钮,总是出现这个问题PInvokeStackImbalance ,全部提示是:ExitWindowsEx' 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.这是怎么回事?

解决方案 »

  1.   

    把你的代码中对ExitWindowsEx的声明贴出来看看吧。从错误信息来看因为你对这个API的声明有误导致了栈平衡被破坏了(参数不正确或者调用约定不正确,因为是API,所以API实现有错基本上不太可能了,一般只可能是前两者),导致调用函数之前的栈地址和调用之后的栈地址不一致,这样程序运行下去肯定会崩溃掉。
    比如你这样声明:
    [DllImport( "Kernel32.dll", EntryPoint="Beep" )]
    public static extern bool Beep( double freq, int duration );
    Beep本来第一个参数是DWORD,4Byte,结果声明成了Double,8Byte,这样在调用的时候便会导致StackImbalance。你可以检查一下你的代码,看看有否类似问题
      

  2.   

    thanks,的确是这个问题,给的例程害死我
      

  3.   

    编译出现以下异常,看不懂,请高手帮忙:
    对 PInvoke 函数“test_xxx!test_xxx.Program::SkinStart”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。在源代码中用到以下:
    [DllImport("appface.dll")]
            public static extern int SkinStart(string SkinFile, int nDefWinType, string CheckSum, long nType,
                long hInstance, long nLen);
            [DllImport("appface.dll")]
            public static extern long SkinRemove();
            [DllImport("appface.dll")]
            public static extern long SkinWindowSet(IntPtr hWnd, long nSkintype);
            [DllImport("appface.dll")]
            public static extern long SkinWindowSetEx(IntPtr hWnd, int nSkintype, int nResourceId,
                int nUrfLoadType, string SkinFile, int hInstance, int nLen);