1
我有一個端口監視器的DLL,我通過調用後臺打印程序的AddMonitor函數來安裝。但是當我想要卸載此監視器時,DeleteMonitor函數將返回錯誤代碼3008 - 「指定的打印監視器當前正在使用中」。我如何可以釋放我的顯示器DLL?虛擬打印機端口監視器安裝
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private class MONITOR_INFO_2
{
[MarshalAs(UnmanagedType.LPStr)]
public string pName;
[MarshalAs(UnmanagedType.LPStr)]
public string pEnvironment;
[MarshalAs(UnmanagedType.LPStr)]
public string pDLLName;
}
[DllImport("winspool.Drv", EntryPoint = "AddMonitorA", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool AddMonitor(
[MarshalAs(UnmanagedType.LPStr)] string Name,
Int32 Level,
[In, MarshalAs(UnmanagedType.LPStruct)] MONITOR_INFO_2 mi2);
[DllImport("winspool.Drv", EntryPoint = "DeleteMonitorA", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool DeleteMonitor(
[MarshalAs(UnmanagedType.LPStr)] string pNullServerName,
[MarshalAs(UnmanagedType.LPStr)] string pNullEnvironment,
[MarshalAs(UnmanagedType.LPStr)] string MonitorName);
private unsafe void InstallMonitor(string monitorName, string dllName)
{
MONITOR_INFO_2 mi2 = new MONITOR_INFO_2();
mi2.pName = monitorName;
mi2.pEnvironment = null;
mi2.pDLLName = dllName;
try
{
bool bRet = AddMonitor(null, 2, mi2);
if (!bRet)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
catch (Exception e)
{
if (!DeleteMonitor(null, null, monitorName))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
bRet = AddMonitor(null, 2, mi2);
if (!bRet)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}