Sidon

I'm implementing print monitor and need ability to get computer name and/or user name who created the job.

I used OpenPrinter and GetJob functions to obtain JOB_INFO_2 structure witch has pMachineName and pUserName fields.

In my case pMachineName contains: ": Last patch for %s was skipped, no cache created."
And pUserName is empty.

Are these fields really what I think, or is there some misunderstand from my site.
And is it possible to get these two values in print monitor

Thanks.



Re: Visual C++ General Getting computer name from printjob

Ramkrishna Pawar

JOB_INFO_2 should give you the machine name, looks like the GetJob API returned you different structure, Are you passing the correct level




Re: Visual C++ General Getting computer name from printjob

Sidon

in StartDocPort

DWORD needed = 0;

LPBYTE buffer = NULL;;

HANDLE hPrinter = NULL;

BOOL opened = OpenPrinter(pPrinterName, &hPrinter, NULL);

if (opened && hPrinter != NULL)

{

GetJob(hPrinter, JobId, 2, buffer, 0, &needed);

buffer = new BYTE[needed];

GetJob(hPrinter, JobId, 2, buffer, 0, &needed);

PJOB_INFO_2 info = (PJOB_INFO_2)buffer;

Log(L"info->pMachineName: %s", info->pMachineName);

Log(L"info->pUserName: %s", info->pUserName);

delete buffer;

ClosePrinter(hPrinter);

}


I'm using level 2





Re: Visual C++ General Getting computer name from printjob

Ramkrishna Pawar

Check if the size of JOB_INFO_2 and needed are equal or not.

Also try following,

Code Snippet

DWORD needed = 0;

LPBYTE buffer = NULL;;

HANDLE hPrinter = NULL;

BOOL opened = OpenPrinter(pPrinterName, &hPrinter, NULL);

if (opened && hPrinter != NULL)

{

PJOB_INFO_2 info buffer=new JOB_INFO_2;

BOOL bRet = GetJob(hPrinter, JobId, 2, buffer, sizeof(JOB_INFO_2), &needed);

if(bRet == FALSE)

{

delete buffer; ClosePrinter(hPrinter);

return GetLastError(); // see what was the error..

}

Log(L"info->pMachineName: %s", buffer->pMachineName);

Log(L"info->pUserName: %s", buffer->pUserName);

delete buffer;

ClosePrinter(hPrinter);

}






Re: Visual C++ General Getting computer name from printjob

Sidon

Ah, my fault.
In second call of GetJob I used the zero size again, so the buffer stayed untached.

Code Snippet

DWORD needed = 0;

LPBYTE buffer = NULL;

HANDLE hPrinter = NULL;

BOOL opened = OpenPrinter(pPrinterName, &hPrinter, NULL);

if (opened && hPrinter != NULL)

{

GetJob(hPrinter, JobId, 2, buffer, 0, &needed);

buffer = new BYTE[needed];

if (GetJob(hPrinter, JobId, 2, buffer, needed, &needed))

{

PJOB_INFO_2 info = (PJOB_INFO_2)buffer;

Log(L"info->pMachineName: %s", info->pMachineName);

Log(L"info->pUserName: %s", info->pUserName);

}

else

Log(L"ERROR CODE: %d", GetLastError());


delete buffer;

ClosePrinter(hPrinter);

}



Thanks for help.





Re: Visual C++ General Getting computer name from printjob

Sidon

The size of JOB_INFO_2 and "needed" size is different.
sizeof is about 120 and needed is about 1200 bytes.

It's because, the buffer contains the struct and than also the strings that struct contains.