做了一个虚拟打印机的程序,卸载时使用了DeletePrinter来删除打印机后再使用DeletePrinterDriver来删除驱动时,总是提示打印机正在使用中无法删除驱动。可是我此时并没有使用该打印机呀!DeleteMonitor来删除打印机监视器时也是一样提示使用中求高手指教!谢谢!我使用的顺序是DeletePriner,DeletePrinterDriver,DeleteMonitor。

解决方案 »

  1.   

    DeletePriner后我用了GetPrinter函数发现打印机一直处于【正在删除】状态,等多久都不完
      

  2.   

    在MSDN中是这样说的

    Res
    The caller must have the SeLoadDriverPrivilege. The DeletePrinterDriver function does not delete the associated files, it merely removes the driver name from the list returned by the EnumPrinterDrivers function. ”提升一下进程权限看看
    HANDLE hToken;
      
      if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
      {
        LUID huid;
        if(LookupPrivilegeValue(NULL, "SeLoadDriverPrivilege", &huid))
        {
          LUID_AND_ATTRIBUTES priv;
          priv.Attributes = SE_PRIVILEGE_ENABLED;
          priv.Luid = huid;
          
          TOKEN_PRIVILEGES tp;
          tp.PrivilegeCount = 1;
          tp.Privileges[0] = priv;
          
          if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL))
          {
            return TRUE;
          }
        }
      }
      

  3.   

    DeletePrinter 后得调用closeprinter
    Res
    If there are print jobs remaining to be processed for the specified printer, DeletePrinter s the printer for pending deletion, and then deletes it when all the print jobs have been printed. No print jobs can be added to a printer that is ed for pending deletion. A printer ed for pending deletion cannot be held, but its print jobs can be held, resumed, and restarted. If the printer is held and there are jobs for the printer, DeletePrinter fails with ERROR_ACCESS_DENIED. Note that DeletePrinter does not close the handle that is passed to it. Thus, the application must still call ClosePrinter.