Getting InteropServices.SEHException on DllImport routine during debug after migrating project from .NET Framework 3.5 to 4.0

ⅰ亾dé卋堺 提交于 2019-12-24 01:55:25

问题


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.

来源:https://stackoverflow.com/questions/8838957/getting-interopservices-sehexception-on-dllimport-routine-during-debug-after-mig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!