2012-02-22 161 views
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()); 
     } 
    } 

回答

0

您將無法通過DeleteMonitor調用刪除端口監視器如果當前使用該類型的端口的一個或多個打印機對象。

這給你留下幾個選項:

  • 交換所有受影響的打印機到另一個端口的端口。 (最好使用LPT1之類的東西,因爲它始終存在)。
  • 刪除使用該端口的所有打印機。
  • 停止後臺打印程序服務並從註冊表中刪除相應的條目(HKLM \ SYSTEM \ CurrentControlSet \ Control \ Print \ Monitors),然後重新啓動後臺打印程序。這將使受影響的打印機在那裏,但它們將無法使用。