我想获取打印机的状态,于是我就用了如下的代码
    openPrinter(...);//成功返回 
    GetPrinter(hPrinter,   2,   pPrinterInfo,   dwNeeded,   &dwNeeded);  
现在的问题是不管打印实际处于什么状态,pPrinterInfo^.status总是为0,这让我十分的郁闷, 
打印机型号:三星   ML-2010   端口是USB001。请各位大侠帮帮忙。

解决方案 »

  1.   

    http://blog.sina.com.cn/s/blog_4b44e1c00100ge82.html
      

  2.   

    谢谢 关注 他用的方法跟我用的一样都是GetPrinter函数的调用 然后取得Status的值,现在的问题关键是这个值他总是0
      

  3.   

    DWORD cByteNeeded,cByteUsed;
    if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
     {
      DWORD dwErrorCode = ::GetLastError();
     } PRINTER_INFO_2 *pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
     
     if (!GetPrinter(hPrinter,
      2,
      (LPBYTE)pPrinterInfo,
      cByteNeeded,
      &cByteUsed))
     {  
      DWORD dwErrorCode = ::GetLastError();
     } 
      

  4.   

    没有出错,函数成功调用,pPrinterInfo里的其他属性的值都是正常的跟打印机的属性相符,唯独这个status出问题
      

  5.   

     HANDLE hPrinter = NULL;  
        DWORD dwNeeded = 0;  
        PRINTER_INFO_2 *pi2 = NULL;  
        DEVMODE *pDevMode = NULL;  
        PRINTER_DEFAULTS pd;  
        BOOL bFlag;  
        LONG lFlag;  
       
      //   Open   printer   handle   (on   Windows   NT,   you   need   full-access   because   you  
      //   will   eventually   use   SetPrinter)...  
        ZeroMemory(&pd, sizeof(pd));  
        pd.DesiredAccess = PRINTER_ALL_ACCESS;  
        bFlag = OpenPrinter(lpPrinterName, &hPrinter, &pd);  
        if(!bFlag || (hPrinter == NULL))  {
    AfxMessageBox( "无法打开指定的打印机!" ) ;
           return FALSE;  
        }
        //   The   first   GetPrinter   tells   you   how   big   the   buffer   should   be   in    
        //   order   to   hold   all   of   PRINTER_INFO_2.   Note   that   this   should   fail   with    
        //   ERROR_INSUFFICIENT_BUFFER.     If   GetPrinter   fails   for   any   other   reason    
        //   or   dwNeeded   isn't   set   for   some   reason,   then   there   is   a   problem...  
        SetLastError(0);  
        bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);  
        if((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))  
    {  
           ClosePrinter(hPrinter);  
       AfxMessageBox( "Cannot get the size of the DEVMODE structure" ) ;
           return FALSE;  
    }  
     
         //   Allocate   enough   space   for   PRINTER_INFO_2...  
        pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);  
        if(pi2 == NULL)  
    {  
           ClosePrinter(hPrinter);  
           return  FALSE;  
    }  
        //   The   second   GetPrinter   fills   in   all   the   current   settings,   so   all   you  
        //   need   to   do   is   modify   what   you're   interested   in...  
        bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);  
        if (!bFlag)  
    {  
          GlobalFree(pi2);  
          ClosePrinter(hPrinter);  
          return FALSE;  
      

  6.   


     HANDLE hPrinter = NULL;  
        DWORD dwNeeded = 0;  
        PRINTER_INFO_2 *pi2 = NULL;  
        DEVMODE *pDevMode = NULL;  
        PRINTER_DEFAULTS pd;  
        BOOL bFlag;  
        LONG lFlag;  
       
      //   Open   printer   handle   (on   Windows   NT,   you   need   full-access   because   you  
      //   will   eventually   use   SetPrinter)...  
        ZeroMemory(&pd, sizeof(pd));  
        pd.DesiredAccess = PRINTER_ALL_ACCESS;  
        bFlag = OpenPrinter(lpPrinterName, &hPrinter, &pd);  
        if(!bFlag || (hPrinter == NULL))  {
    AfxMessageBox( "无法打开指定的打印机!" ) ;
           return FALSE;  
        }
        //   The   first   GetPrinter   tells   you   how   big   the   buffer   should   be   in    
        //   order   to   hold   all   of   PRINTER_INFO_2.   Note   that   this   should   fail   with    
        //   ERROR_INSUFFICIENT_BUFFER.     If   GetPrinter   fails   for   any   other   reason    
        //   or   dwNeeded   isn't   set   for   some   reason,   then   there   is   a   problem...  
        SetLastError(0);  
        bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);  
        if((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))  
    {  
           ClosePrinter(hPrinter);  
       AfxMessageBox( "Cannot get the size of the DEVMODE structure" ) ;
           return FALSE;  
    }  
     
         //   Allocate   enough   space   for   PRINTER_INFO_2...  
        pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);  
        if(pi2 == NULL)  
    {  
           ClosePrinter(hPrinter);  
           return  FALSE;  
    }  
        //   The   second   GetPrinter   fills   in   all   the   current   settings,   so   all   you  
        //   need   to   do   is   modify   what   you're   interested   in...  
        bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);  
        if (!bFlag)  
    {  
          GlobalFree(pi2);  
          ClosePrinter(hPrinter);  
          return FALSE;  
      

  7.   

    应该这样:

            ::GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded);
    if(0 < dwNeeded) {
                 if (::GetPrinter(hPrinter, 2,  pPrinterInfo, dwNeeded, &dwNeeded)){
    // here you will get  "pPrinterInfo"
    } }
      

  8.   

    xueer8835
     
    (小妖) 
    是正确的!!!
      

  9.   

    昨天由于下班所以没能及时回帖描述情况,谢谢各位关注小弟的情况。
    现在的情况是运行代码,PRINTER_INFO_2结构的结果如下:
    (“\\test”, “\\test\Samsung ML-2010 Series”, “SamsungM”, “USB001”, “Samsung ML-2010 Series”, “”, “”, $B8CE04, “”, “WinPrint”, “RAW”, “”, $B8CD04, 2584, 1, 0, 0, 0, 0, 0, 0).                      ↓                                          ↓
                            内存地址                                    内存地址各位大侠在给分析分析。是不是哪里出错了
      

  10.   

    还有个重要情况忘了说了,这个打印机是共享的打印机,并不是跟我的主机相连,刚才又看了下Getprinter函数,上面有这么句话:只有在发出调用的应用程序有足够的权限时,PRINTER_INFO_x结构中的一些字段才能够被读取。这种权限由系统当前的安全设置决定。我感觉是不是权限不够啊。