编组访问冲突

发布于 2024-09-16 13:12:46 字数 793 浏览 12 评论 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.SafeNativeMethods.PrintDlg(PRINTDLGX86 lppd) 处 System.Drawing.Printing.PrinterSettings.GetDefaultPrinterName() 位于 System.Drawing.Printing.PrinterSettings.get_PrinterNameInternal()
在 System.Drawing.Printing.PrinterSettings.get_PrinterName()

The correct function declaration is:

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

I declared it like:

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

Can this be the reason for the access violation exception?

This is the exception I get:

Unhandled exception occured in UI thread
System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.
at System.Drawing.SafeNativeMethods.PrintDlg(PRINTDLGX86 lppd) at
System.Drawing.Printing.PrinterSettings.GetDefaultPrinterName() at
System.Drawing.Printing.PrinterSettings.get_PrinterNameInternal()
at System.Drawing.Printing.PrinterSettings.get_PrinterName()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-09-23 13:12:46

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

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

You didn't post the SCROLLINFO structure definition. In the code you posted, I see that bool parameter type is incorrect: define it as int. Win32 BOOL is 32-bits value, it matches int in .NET.

Post full code: PInvoke definitions and call to SetScrollInfo to get more information.

若无相欠,怎会相见 2024-09-23 13:12:46

结构声明:

   [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 滚动信息1 = 新的 SCROLLINFO();
SetScrollInfo(new HandleRef(this, Handle), 0,scrollinfo1, true);

structure declaration:

   [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;
        }
    }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文