Marshal.StructureToPtr fails in module ntdll.dll

2

I'll start with a bit of history: The issue I'm facing currently appeared suddenly without any changes to the code. Then disappeared in the same way after 3 days. Now it came back in a week and doesn't want to go away =).

I have code that works with printer - settings it's printer preferences to print documents in the specified way. I use local printer which points to TCP address of the network printer (it is common approach for such tasks as I know).

Here follows the code I use to load printer configuration from binary file (previously saved in the similar way). I give all source code with declarations of unmanaged functions calls, because have no idea what could cause the issue. The line that is braking my application (Windows Service):

Marshal.StructureToPtr(pInfo, pPInfo, true); //THIS LINE FAILS

and here is the whole code of a function:

public static bool LoadSettings(string printerName, string filepath)
{
    Logger.GetLog().WriteInformation(string.Format("Loading printer settings '{0}' for printer '{1}'", filepath, printerName), "PrinterSettingsStorage.LoadSettings()");
    bool success = false;
    try
    {
        if (!File.Exists(filepath))
        {
            return false;
        }

        IntPtr hPrinter;
        int bytes = 0;
        IntPtr pPInfo;
        IntPtr pDevMode;
        PRINTER_INFO_2 pInfo = new PRINTER_INFO_2();

        PRINTER_DEFAULTS PrinterValues = new PRINTER_DEFAULTS();
        PrinterValues.pDatatype = 0;
        PrinterValues.pDevMode = 0;
        PrinterValues.DesiredAccess = PRINTER_ALL_ACCESS;

        //retrieve the devmode from file
        using (FileStream fs = new FileStream(filepath, FileMode.Open))
        {
            int length = Convert.ToInt32(fs.Length);
            pDevMode = GlobalAlloc(0, length);
            for (int i = 0; i < length; i++)
            {
                Marshal.WriteByte(pDevMode, i, (byte)fs.ReadByte());
            }
        }

        //get printer handle
        OpenPrinter(printerName, out hPrinter, ref PrinterValues);

        //get bytes for printer info structure and allocate memory
        GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out bytes);
        if (bytes == 0)
        {
            throw new Exception("Get Printer Failed");
        }
        pPInfo = GlobalAlloc(0, bytes);

        //set pointer to printer info
        GetPrinter(hPrinter, 2, pPInfo, bytes, out bytes);

        //place the printer info structure
        pInfo = (PRINTER_INFO_2)Marshal.PtrToStructure(pPInfo, typeof(PRINTER_INFO_2));

        //insert the new devmode
        pInfo.pDevMode = pDevMode;
        pInfo.pSecurityDescriptor = IntPtr.Zero;

        //set pointer to new printer info
        Marshal.StructureToPtr(pInfo, pPInfo, true); //THIS LINE FAILS

        //update
        SetPrinter(hPrinter, 2, pPInfo, 0);

        //free resources
        GlobalFree(pPInfo);
        GlobalFree(pDevMode);
        ClosePrinter(hPrinter);

        success = true;
    }
    catch (COMException ce)
    {
        Logger.GetLog().WriteError(string.Format("COM error loading printer settings to printer: {0}", Marshal.GetLastWin32Error()), ce.StackTrace, "PrinterSettingsStorage.LoadSettings()");
    }
    catch (Exception e)
    {
        Logger.GetLog().WriteError(string.Format("Unknown error loading printer settings to printer"), e.StackTrace, "PrinterSettingsStorage.LoadSettings()");
    }
    finally
    {
        Logger.GetLog().WriteInformation(string.Format("Finish loading printer settings '{0}' for printer '{1}'. Success: {2}", printerName, filepath, success), "PrinterSettingsStorage.LoadSettings()");
    }
    return success;
}

[StructLayout(LayoutKind.Sequential)]
internal struct PRINTER_DEFAULTS
{
    public int pDatatype;
    public int pDevMode;
    public int DesiredAccess;
}

[StructLayout(LayoutKind.Sequential)]
internal struct PRINTER_INFO_2
{
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pServerName;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pPrinterName;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pShareName;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pPortName;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pDriverName;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pComment;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pLocation;
    public IntPtr pDevMode;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pSepFile;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pPrintProcessor;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pDatatype;
    [MarshalAs(UnmanagedType.LPStr)]
    public readonly string pParameters;
    public IntPtr pSecurityDescriptor;
    public readonly Int32 Attributes;
    public readonly Int32 Priority;
    public readonly Int32 DefaultPriority;
    public readonly Int32 StartTime;
    public readonly Int32 UntilTime;
    public readonly Int32 Status;
    public readonly Int32 cJobs;
    public readonly Int32 AveragePPM;
}

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GlobalFree(IntPtr handle);

[DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true,
    CharSet = CharSet.Ansi, ExactSpelling = true,
    CallingConvention = CallingConvention.StdCall)]
private static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel,
                                        IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);

[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA",
    SetLastError = true, CharSet = CharSet.Ansi,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool
    OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,
                out IntPtr hPrinter, ref PRINTER_DEFAULTS pd);

[DllImport("winspool.drv", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int Command);

[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true,
    ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool ClosePrinter(IntPtr hPrinter);

Symptoms:

  • Service simply stops immediately exiting at the referenced line. No finally block execution, no exception caught.

  • Windows event log contains error

    Faulting module name: ntdll.dll, version: 6.1.7601.17725, time stamp: 0x4ec49b8f Exception code: 0xc0000374 Fault offset: 0x000ce6c3 Faulting process id: 0x12dc Faulting application start time: 0x01cdc71b9bf9b661 Faulting application path: C:\Program Files (x86)\MyServiceExePath.exe Faulting module path: C:\Windows\SysWOW64\ntdll.dll Report Id: 0eb45111-330f-11e2-be8e-005056975a30

My OS: Windows Server 2008 R2.

As I said, it reproduces constantly for some period of time and then once went away without any changes in the code. I have no idea what causes this issue and why is it so unstable. Hope there are people here, more expirienced with unmanaged code than me =).

Any ideas?

c#
marshalling
unmanaged
asked on Stack Overflow Nov 20, 2012 by Sasha • edited Nov 20, 2012 by Sasha

1 Answer

0
        //insert the new devmode
        pInfo.pDevMode = pDevMode;
        pInfo.pSecurityDescriptor = IntPtr.Zero;

        //Add by me
        Marshal.StructureToPtr(pInfo, pPInfo, false);
        //set pointer to new printer info 
        Marshal.StructureToPtr(pInfo, pPInfo, true);

        //update
        SetPrinter(hPrinter, 2, pPInfo, 0);
        //Add by me
        Marshal.DestroyStructure(pPInfo, typeof(PRINTER_INFO_2));

        //free resources
        GlobalFree(pPInfo);
        GlobalFree(pDevMode);
        ClosePrinter(hPrinter);
answered on Stack Overflow Dec 3, 2018 by Lo Sala • edited Dec 4, 2018 by Sasha

User contributions licensed under CC BY-SA 3.0