Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 787
  • Last Modified:

pausing a job

Dear experts

I am plying about with the WMI settings on C++

What i am trying to do is resume a paused job. However I cant do it with the SetJob command

It deletes the job but it doesnt resume

On both i am getting a result of 1 which indicates Setjob occured successfully

any idea how to fix this

I have set the printer to pause using SetPrinter
if thats the reason for my problems how do i pause a print job if the default settings of a printer are not set to pause?
INT MonitorPrinter(char *pPrinter)
{
	int iIndex = 0;
	DWORD dwNeeded, dwRet, dwPrinterStatus;
	HANDLE hPrinter=NULL; 
	HANDLE hWait;


	JOB_INFO_2  *pJobs=NULL;
	int iJobs, iTempIndex;
	int jobid=4;
	int c;
//	int id1=atoi(id);







	PRINTER_DEFAULTS pd;
	pd.pDatatype = NULL;
	pd.pDevMode = NULL;
	pd.DesiredAccess = PRINTER_ALL_ACCESS;


	if(!OpenPrinter(pPrinter,&hPrinter,&pd)) 
	{
		DisplayError("OpenPrinter Failed");
		return FALSE;
	}


	GetJobs(hPrinter,       
		&pJobs, /* Pointer to be filled.  */ 
		&iJobs,            /* Count of jobs filled.  */ 
	
		&dwPrinterStatus);         /* Print Queue status.    */ 

for (iIndex = 0; iIndex < iJobs; iIndex++)
	{

	int check =pJobs[iIndex].JobId;

		//if (id1 == check)
		
		{
	char *user="working";
	pJobs[0].pUserName=user;	
//	pJobs[0].pStatus=JOB_STATUS_PRINTING;
	printf("Username is %s\n", pJobs[0].pUserName);
	
	
	c =	SetJob(hPrinter, pJobs[iIndex].JobId, 2, (LPBYTE) pJobs,JOB_CONTROL_RESUME);

	printf("Result is %d\n",c);
}
}


	return (0);
}

Open in new window

0
samelamin
Asked:
samelamin
  • 12
  • 7
1 Solution
 
jkrCommented:
'JOB_CONTROL_RELEASE' seems to be the problem - try
	c =	SetJob(hPrinter, pJobs[iIndex].JobId, 2, (LPBYTE) pJobs,JOB_CONTROL_RESUME);

Open in new window

0
 
samelaminAuthor Commented:
Ive tried that too
Its why i changed it to JOB_CONTROL_RESUME, and ive updated the code
0
 
ZoppoCommented:
Hi samelamin,

is the job still deleted with 'JOB_CONTROL_RESUME'? Is it the same if you don't set a user name? What is 'GetJobs'? Is it a function you wrote? Seems not to be a GDI function ...

ZOPPO

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
samelaminAuthor Commented:
It is yes
GetJobs will fill the Job_INFO_2 structure with the given jobs in the printer.

What is odd is i am using SetPrinter to change the default settings to pause, but when i print to it the job status is just "Ready" instead of paused

Even if i set aprint job to pause using the windows console and use SETJOB to change it to resume it still does not work

Ami not pausing the printer properly?
if (command==1)
	{
	
    SetPrinter(hPrinter, 0, NULL, PRINTER_CONTROL_PAUSE);

	printf("Setting printer %s to pause\n",Printer);
	}
	else if(command==2)
	{
	SetPrinter(hPrinter, 0, NULL, PRINTER_CONTROL_RESUME);
	printf("Setting printer %s to resume\n",Printer);

Open in new window

0
 
ZoppoCommented:
I'm not sure if I understand correctly, but if so I think you mix up printer and job status - if the printer is PAUSED a new job may even be READY - pausing and resuming the job won't do anything as long as the printer is PAUSED.
0
 
samelaminAuthor Commented:
I thought so

So how do i set the printer to be paused to allow all jobs given to it to be paused?
0
 
samelaminAuthor Commented:
IF i understand your comment Zoppe the underlying setting for a printer should be resume.

Given that how do i set all jobs given to it to be paused?
0
 
ZoppoCommented:
Hm - I'm not sure if there's an easy way - only thing I can think of is to set the printer to PAUSED, then set all jobs to be PAUSED, the re-set the printer to READY ... but thus you cannot ensure that i.e. a new printer job is started while the printer is READY - maybe there are ways to somehow implement that your app gets notified if a new print job is started, but unfortunateley I haven't such deep knowledge of the print/spooler stuff ...
0
 
samelaminAuthor Commented:
I did that too but what i thought was once i set the printer to ready all the jobs will resume
0
 
samelaminAuthor Commented:
OK i have a question

How do i pause a printer, because it isnt really paused its shown as ready, how do i make the status change to paused

Also Zoppo I tried your solution unfortunately when i pause a printer then print
the moment i change it to resume it resumes the first job in the queue

So it doesnt allow me to choose when i want to resume the job.
0
 
ZoppoCommented:
Hm - that's strange - IMO pausing/resuming the printer should work.

I attach the code of a small command line program I wrote - this simply toggles the PAUSED state of my local printer 'PDFCreator' (you can replace the printer name in 'main') - if I run it the state of the printer is shown correctly.

> when i pause a printer then print the moment i change it to resume it resumes the first job in the queue
IMO this should only happen if the print job is READY - it shouldn't happen if for all jobs the state is PAUSED ...

ZOPPO
#include <windows.h>
#include <iostream>

bool TogglePrinterState( char* pszPrinter )
{
	HANDLE hPrinter = NULL;
	PRINTER_DEFAULTS pd;
	pd.pDatatype = NULL;
	pd.pDevMode = NULL;
	pd.DesiredAccess = PRINTER_ALL_ACCESS;


	if ( FALSE == OpenPrinter( pszPrinter, &hPrinter, &pd ) )
	{
		std::cout << "Error: cannot open printer '" << pszPrinter << "' (ec: " << GetLastError() << ")" << std::endl;
		return false;
	}

	bool bRet = true;

	DWORD dwLen = 0;
	PRINTER_INFO_2* pInfo = NULL;

	if ( FALSE == GetPrinter( hPrinter, 2, NULL, 0, &dwLen ) )
	{
		if ( GetLastError() == 122 && dwLen > 0 )
		{
			pInfo = (PRINTER_INFO_2*)new BYTE[ dwLen ];

			if ( FALSE == GetPrinter( hPrinter, 2, (LPBYTE)pInfo, dwLen, &dwLen ) )
			{
				std::cout << "Error calling 'GetPrinter' (ec: " << GetLastError() << ")" << std::endl;
				bRet = false;

				goto clean_up;
			}
		}
	}

	if ( 0 != ( pInfo->Status & PRINTER_STATUS_PAUSED ) )
	{
		if ( FALSE != SetPrinter( hPrinter, 0, NULL, PRINTER_CONTROL_RESUME ) )
		{
			std::cout << "Resumed printer '" << pszPrinter << "' ..." << std::endl;
		}
	}
	else
	{
		if ( FALSE != SetPrinter( hPrinter, 0, NULL, PRINTER_CONTROL_PAUSE ) )
		{
			std::cout << "Paused printer '" << pszPrinter << "' ..." << std::endl;
		}
	}

clean_up:
	delete [] pInfo;

	CloseHandle( hPrinter );

	return bRet;
}

int main(int argc, char* argv[])
{
	TogglePrinterState( "PDFCreator" );
	return 0;
}

Open in new window

0
 
samelaminAuthor Commented:
Thanks for this

But this will toggle the pause, when you toggle the resume do all jobs start resuming or do they stay paused?>
0
 
samelaminAuthor Commented:
Yup it starts resuming everything as soon as i toggle it back so thats not going to work
0
 
ZoppoCommented:
When the printer's state is resumed using this code only those jobs which aren't paused start printing - the paused ones still keep as 'Paused' in the job list ...
0
 
samelaminAuthor Commented:
oh I think i get you
You mean i have to:

1) Toggle printer to pause
2) print document then set status to pause
3)resume the printer

Is that what you mean?
0
 
ZoppoCommented:
Yes, exactly - changing printer's state doesn't anyhow affect the job's states, so you have to control them both ...
0
 
samelaminAuthor Commented:
OK when i set a jobs status to pause it still doesnt chage its actual status from ready to pause

here is how i am changing the status
c =	SetJob(hPrinter, pJobs[iIndex].JobId, 2, (LPBYTE) pJobs,JOB_CONTROL_PAUSE);

Open in new window

0
 
samelaminAuthor Commented:
Got it working

It was due to the fact i had pJobs refrenced. It should be NULL

Anyways thank you Zoppo

Its annoying to switch it to paused,print document and pause it, then again resume the printer

But oh well thank you
0
 
ZoppoCommented:
Fine, I'm glad I could help you so far. But, as told, there might be better solutions which I just don't know - i.e. there's a "Print Monitor API" which might give you the ability to intercept print-spooler calls, but I simply don't know if this can be used for your purpose since I never used it.

BTW: Are the print jobs started by your application or by third party applications? If it's yours you should be able to simply start the jobs in a resumed state ...

Have a nice day,

best regards,

ZOPPO
0
 
samelaminAuthor Commented:
Well for testing purposes im just printing Test Pages so i suppose it is me who starts them

But in future it may be started by someone else but my application will always have admin rights
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 12
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now