如何链接qt的系统库?

时间:2016-09-26 22:45:13

标签: c++ qt winapi

我的项目包括使用打印机假脱机程序,但QT Creator的链接器无法添加系统库,因此我有错误LNK2019。 我需要添加到.pro文件中以添加库吗?

在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 个答案:

答案 0 :(得分:2)

添加

LIBS += -lwinspool

到您的.pro文件。

然后重新运行qmake,然后尝试编译。