我寫一個Visual C++程序獲取打印作業的詳細信息。 代碼如下所示:調用API WINSPOOL問題GetJOB
HANDLE hPrinter;
DWORD needed, returned, byteUsed,level;
JOB_INFO_2 *pJobStorage1=NULL;
level = 2;
GetJob(hPrinter, jobId, level, NULL, 0, &needed);
if (GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
cout << "GetJobs failed with error code"
<< GetLastError() << endl;
pJobStorage1 = (JOB_INFO_2 *)malloc(needed);
ZeroMemory(pJobStorage1, needed);
cout << GetJob(hPrinter, jobId, level, (LPBYTE)pJobStorage1, needed, (LPDWORD)&byteUsed) << endl;
cout << pJobStorage1[0].pPrinterName<<endl;
按照documentation,pJobStorage1的輸出不是數組,但是,當我更改IDE報錯
pJobStorage1[0].pPrinterName
到
pJobStorage1.pPrinterName
所以,我想知道發生了什麼。
它不是一個數組。你將它正確地聲明爲一個指針,並且做了內存管理權(不要忘記free(),讓我們不要對新的vs malloc進行挑剔),你只需要' - >'來解引用它。荷蘭荷蘭。 –