2016-09-26 90 views
-1

我有項目,其中包括與打印機後臺處理程序工作,但QT Creator的鏈接器無法添加系統庫,因此我有錯誤LNK2019。 我需要添加到.pro文件以添加庫?如何鏈接系統庫的qt?

在QT Creatro最新版本中工作。

這是錯誤: main.obj:-1:錯誤:LNK2019:ссылкананеразрешенныйвнешнийсимвол_OpenPrinterW @ 12вфункции 「INT __cdecl ListJobsForPrinter(wchar_t的*,INT)」(ListJobsForPrinter @@ YAHPA_WH @ Z?)

#include <QCoreApplication> 
#include <Windows.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <locale> 
#include <iostream> 
#include <conio.h> 
#include <QString> 
#include <winspool.h> 

struct PrintData{ 
    LPTSTR printerName; 
    int lastID; 
    int lastPages; 
    int totalPages; 
    bool noJob; 
}; 

PrintData *printers; 

BOOL ListJobsForPrinter(LPTSTR szPrinterName, int counter) 
{ 
    HANDLE   hPrinter; 
    DWORD   dwNeeded, dwReturned, i; 
    JOB_INFO_1  *pJobInfo; 

    // You need a printer handle, open the printer 
    //хэндлер принтера, открытие принтера 
    if (!OpenPrinter(szPrinterName, &hPrinter, NULL)) 
     return FALSE; 

    // First you call EnumJobs() to find out how much memory you need 
    //вызов перечисления работ? для вычисления требуемого количества памяти 
    if (!EnumJobs(hPrinter, 0, 0xFFFFFFFF, 1, NULL, 0, &dwNeeded, 
     &dwReturned)) 
    { 
     // It should have failed, but if it failed for any reason other 
     // than "not enough memory", you should bail out 
     if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 
     { 
      ClosePrinter(hPrinter); 
      return FALSE; 
     } 
    } 
    // Allocate enough memory for the JOB_INFO_1 structures plus 
    // the extra data - dwNeeded from the previous call tells you 
    // the total size needed 
    if ((pJobInfo = (JOB_INFO_1 *)malloc(dwNeeded)) == NULL) 
    { 
     ClosePrinter(hPrinter); 
     return FALSE; 
    } 


    // Call EnumJobs() again and let it fill out our structures 
    if (!EnumJobs(hPrinter, 0, 0xFFFFFFFF, 1, (LPBYTE)pJobInfo, 
     dwNeeded, &dwNeeded, &dwReturned)) 
    { 
     ClosePrinter(hPrinter); 
     free(pJobInfo); 
     return FALSE; 
    } 
    // You're done with the printer handle, close it 
    ClosePrinter(hPrinter); 

    // dwReturned tells how many jobs there are 
    // Here, you'll simply display the number of jobs found 
    //printf("%d jobs\n", dwReturned); 
    // It's easy to loop through the jobs and access each one 
    printers[counter].noJob = true; 
    for (i = 0; i<dwReturned; i++) 
    { 
     printers[counter].noJob = false; 
     // pJobInfo[i] is a JOB_INFO_1 struct for that job 
     // so here you could do whatever you want for each job 
     printf("Job ID: [%d]\n", pJobInfo[i].JobId); 
     printf("Total Pages: [%d]\n", pJobInfo[i].TotalPages); 
     printf("Pages Printed: [%d]\n", pJobInfo[i].PagesPrinted); 
     printf("Status: [%d]\n", pJobInfo[i].Status); 
     //if ((printers[counter].lastID != pJobInfo[i].JobId) && (printers[counter].lastPages != 0)) 
     if (printers[counter].lastID != pJobInfo[i].JobId) 
     { 
      //printf("ID: %d", printers[counter].lastID); 
      printf("upload\n"); 
      //printers[counter].totalPages += printers[counter].lastPages; 
      printers[counter].lastPages = 0; 
      printers[counter].lastID = pJobInfo[i].JobId; 


     } 
     else 
     { 
      printf("add\n"); 
      //printf("ID: %d", printers[counter].lastID); 
      printers[counter].lastPages = pJobInfo[i].TotalPages; 
     } 

     //printf("Total Pages: [%d]\n", pJobInfo[i].TotalPages); 
     //printf("Total Pages: [%d]\n", pJobInfo[i].TotalPages); 
     //pJobInfo[i]. 
    } 

    // Clean up 
    free(pJobInfo); 
    return TRUE; 
} 




int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 

    bool single = true; 
     int counter = 0; 
     DWORD dwFlags = PRINTER_ENUM_FAVORITE | PRINTER_ENUM_LOCAL | PRINTER_ENUM_NETWORK; 
     DWORD cbNeeded; 
     DWORD cReturned; 
     EnumPrintersA(dwFlags, 0, 2, 0, 0, &cbNeeded, &cReturned); 
     std::cout << cbNeeded << std::endl; 
     BYTE* buf = new BYTE[cbNeeded]; 
     EnumPrintersA(
      dwFlags 
      , 0 
      , 2 
      , buf 
      , cbNeeded 
      , &cbNeeded 
      , &cReturned 
      ); 
     std::cout << cReturned << std::endl; 
     printers = (PrintData*)malloc(sizeof(PrintData)); 
     LPPRINTER_INFO_2A pPrinters = (LPPRINTER_INFO_2A)buf; 
     for (DWORD i(0); i < cReturned; ++i) 
     { 
      std::cout << pPrinters[i].pPrinterName << std::endl; 
      printf("Need to info about this printer? y/n \n"); 
      if (getch() == 'y') 
      { 
       if (single == true) 
       { 
        printers[counter].printerName = (LPTSTR)pPrinters[i].pPrinterName; 
        printers[counter].lastPages = 0; 
        printers[counter].noJob = true; 
        printers[counter].totalPages = 0; 
        printers[counter].lastID = -1; 
        single = false; 
       } 
       else 
       { 
        counter++; 
        printers[counter].printerName = (LPTSTR)pPrinters[i].pPrinterName; 
        printers[counter].noJob = true; 
        printers[counter].lastPages = 0; 
        printers[counter].totalPages = 0; 
        printers[counter].lastID = -1; 
       } 
      } 
     } 


     while (1){ 
      if (counter==0) 
      { 
       ListJobsForPrinter(printers[counter].printerName, counter); 
       if (printers[counter].noJob == true) 
       { 

        printers[counter].totalPages += printers[counter].lastPages; 
        printers[counter].lastPages = 0; 
       } 
       printf("Total pages for printer "); 
       puts((const char*)printers[counter].printerName); 
       printf("%d\n", printers[counter].totalPages); 
       printf("ID: %d\n", printers[counter].lastID); 
      } 
      else 
      { 
       for (int i = 0; i <= counter; i++) 
       { 
        ListJobsForPrinter(printers[i].printerName, counter); 
        if (printers[i].noJob == true) 
        { 
         printers[i].totalPages += printers[i].lastPages; 
         printers[i].lastPages = 0; 
        } 
        printf("Total pages for printer "); 
        puts((const char*)printers[i].printerName); 
        printf("%d\n", printers[i].totalPages); 
        printf("ID: %d\n", printers[i].lastID); 
       } 
      } 
      Sleep(60); 
      system("cls"); 
     } 
     delete[] buf; 

    return a.exec(); 
} 

回答

1

添加

LIBS += -lwinspool 

.pro文件。

並重新運行qmake,然後嘗試編譯。

+0

沒有幫助。我的.pro看起來是:_italic_ ** **大膽'QT + =核心 QT - = GUI CONFIG + = C++ 11 TARGET = SQL_Console_test CONFIG + =控制檯 CONFIG - = app_bundle LIBS + = -lwinspool TEMPLATE = app SOURCES + = main.cpp' –

+0

感覺就像編譯器事件沒有嘗試編譯。只是錯誤,就是這樣。 –

+1

你試過重新運行qmake嗎? – krzaq