WinSpool.Drv SetPort function fails

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




When calling SetPort function implemented in WinSpool.Drv within .NET, you may get following error:

0x0000007C - The system call level is not correct.

After intensive digging in internet I didn’t find anything related to this error. The problem is that obviously nobody has ever called it within .NET. This is reasonable, because this function is designed to be called within one port monitor, and port monitor is always written in C++ as user mode driver. Till now.

The error above has no logical meaning. In my case indicates stack corruption, because .NET code didn’t marshal few parameters as expected. The reason for this is that all interop definitions of SetPort function, which you can found in internet  are just wrong. They probably have been generated automatically.


The right definition is for sure:

[DllImport(DllNames.WinSpool, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetPort(

[In, MarshalAs(UnmanagedType.LPWStr)] string pName,
[In, MarshalAs(UnmanagedType.LPWStr)] string portName,
uint dwLevel,
IntPtr pData);


And the ultimate call to SetPort looks like:

IntPtr handle;
PORT_INFO_3 pInf = new PORT_INFO_3();
pInf.dwSeverity = WinSpooler.PORT_STATUS_TYPE_INFO;
pInf.dwStatus = WinSpooler.PORT_STATUS_OFFLINE;
pInf.pszStatus = null; // This works with OS Ver. >= 6

uint level = 3;

IntPtr pData = Marshal.AllocHGlobal(Marshal.SizeOf(pInf));

Marshal.StructureToPtr(pInf, pData, true);

bool res = WinSpooler.SetPort( null, "DOT4_002", level, pData);


Posted Feb 09 2010, 05:10 PM by Damir Dobric is a .Net Community Blog powered by daenet GmbH.