开发者

Access violation on marshaling

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()


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.


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);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜