I have written an application that interfaces with the winspool print driver, and its been working just fine for months. I need to move my projects from .NET Framework 3.5 to 4.0 to include a coworkers assemblies, but doing this (and only doing this) causes one of my .dll imported method calls to fail when executing from the VS 2010 IDE. Both the debug and release binaries still work if I run them outside of the Visual Studio 2010 environment. Below is the line that now fails after moving to .NET 4.0:
// This call works just fine immediately before switching projects to .NET 4.0
// and begins failing immediately after.
WinSpool.ClosePrinter(m_PrinterHandle);
Below is my definition for this imported method:
public static class WinSpool
{
/// <summary>
/// http://msdn.microsoft.com/en-us/library/dd183446%28v=vs.85%29.aspx
/// </summary>
[DllImport(
"winspool.Drv",
EntryPoint = "ClosePrinter",
SetLastError = true,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
}
Below is the exact text of the exception I'm getting:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception
Keep in mind, I can restore the project .NET 3.5 and it works fine when executing from the Visual Studio 2010 IDE. But with .NET 4.0 as the target framework I get the exception when running from the IDE, but when executing the binaries directly out of windows explorer they work fine. What the heck is going on here.