首页 文章

编组访问违规

提问于
浏览
0

正确的函数声明是:

[DllImport("user32.dll")]
static extern int SetScrollInfo (IntPtr hwnd, int n, ref SCROLLINFO lpcScrollInfo, bool b);

我声明它是这样的:

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
static extern int SetScrollInfo (IntPtr hwnd, int n, SCROLLINFO lpcScrollInfo, bool b);

这可能是访问冲突异常的原因吗?

这是我得到的例外:

UI线程中出现未处理的异常System.AccessViolationException:尝试读取或写入受保护的内存 . 这通常表明其他内存已损坏 . 在System.Drawing.Printing.Printer.Printer.Printer上的System.Drawing.Printing.PrinterSettings.GetDefaultPrinterName()处的System.Drawing.SafeNativeMethods.PrintDlg(PRINTDLGX86 lppd)处于System.Drawing.Printing.PrinterSettings.get_PrinterName()的System.Drawing.Printing.PrinterSettings.get_PrinterNameInternal()

2 回答

  • 0

    您没有发布SCROLLINFO结构定义 . 在您发布的代码中,我看到bool参数类型不正确:将其定义为int . Win32 BOOL是32位值,它与.NET中的int匹配 .

    发布完整代码:PInvoke定义并调用SetScrollInfo以获取更多信息 .

  • 0

    结构声明:

    [StructLayout(LayoutKind.Sequential)]
        public class SCROLLINFO
        {
            public int cbSize;
            public int fMask;
            public int nMin;
            public int nMax;
            public int nPage;
            public int nPos;
            public int nTrackPos;
            public SCROLLINFO()
            {
                cbSize = Marshal.SizeOf(typeof(SCROLLINFO));
            }
            public SCROLLINFO(int mask, int min, int max, int page, int pos)
            {
                cbSize = Marshal.SizeOf(typeof(SCROLLINFO));
                fMask = mask;
                nMin = min;
                nMax = max;
                nPage = page;
                nPos = pos;
            }
        }
    

    调用:SCROLLINFO scrollinfo1 = new SCROLLINFO(); SetScrollInfo(new HandleRef(this,Handle),0,scrollinfo1,true);

相关问题