Question

J'ai une DLL du moniteur de port, que j'instalais par appel d'appel AddMonitor du spouleur. Mais quand je veux uninstallation de ce moniteur, la fonction Deletemonitor Return ErrorCode 3008 - "Le moniteur d'impression spécifié est actuellement utilisé". Comment puis-je libérer ma DLL de moniteur?



    [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());
        }
    }

Était-ce utile?

La solution

Vous ne pourrez pas supprimer un moniteur de port via l'appel Deletemonitor s'il existe un ou plusieurs objets d'imprimante à l'aide d'un port de ce type.

Cela vous laisse avec plusieurs options:

  • Échangez le port de toutes les imprimantes affectées vers un autre port. (Mieux vaut utiliser quelque chose comme LPT1: puisque c'est toujours là).
  • Supprimez toutes les imprimantes à l'aide du port.
  • Arrêtez le service de spouleur et supprimez les entrées appropriées du registre (HKLM System CurrentControlset Control print moniteurs) puis redémarrez le spouleur. Cela laissera les imprimantes affectées là-bas, mais elles seront inutilisables.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top