[System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr ph, IntPtr ch, String cn, String wn);
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, long nCmdShow);
        private void button1_Click(object sender, System.EventArgs e)
        {
            try
            {
                IntPtr handle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Shell_TrayWnd", null);
                ShowWindow(handle, 0);//运行到这里会报异常```而任务栏正确隐藏了```            }
            catch { }
        } 
它报的异常:检测到 PInvokeStackImabalance对 PInvoke 函数“WindowsApplication2!WindowsApplication2.Form1::ShowWindow”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。请各位高手帮忙看下。。
先谢了。。

解决方案 »

  1.   

    public static extern bool ShowWindow(IntPtr hWnd, long nCmdShow);
    改成
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
      

  2.   

    多谢yufenfeila大哥
    请教一下。。
    在此long 跟 int 有什么区别?
    还是我方法的声明本身就有错误??
      

  3.   

    一个32位整数,一个64位整数。类型不匹配会导致Interop marshal错误。所以出现“ PInvokeStackImabalance”直译就是pinvoke栈不对称。win32 api很少有需要传long的。
      

  4.   

    这里需要传入一个32位整数,在C#中,int类型是32位,而long是64位的
    而某些语言中,int是16位,long是32位的,所以用你那种写法