|
Try the SetPrinter API with the PRINTER_CONTROL_PURGE flag.
|
|
|
|
|
Hi Santosh,
Thank you so much for your reply. Even I tried using SetPrinter API with the related parameters.It doesn't worked. "SetPrinter(hPrinter,0,0,PRINTER_CONTROL_PURGE);"..This is what I have used in my program. Is it correct..?
|
|
|
|
|
That is perfect.
I have used it and it worked for me.
You must have administrative privileges to use this API.
Maybe that is where the problem is.
|
|
|
|
|
Hi Santosh,
I don't think so that I got some restriction on these API's. Well, is it a OS dependent.?
|
|
|
|
|
If you're running on Vista or higher, try right clicking on the EXE and select Run as administrator .
|
|
|
|
|
Hi Santosh,
I am using windows XP. IF its working fine in your machine and not in mine means there may be some mistake in my program. So I have attached the code below. I have used "Borland C++ builder" for this program. Can you have a look at it.? Let me know if there is any mistake....
#include <vcl.h>
#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <winspool.h>void thebuffer(HANDLE hPrinter)
{
FILE * pFile;
long lSize;
char * buffer;
DWORD dwBytesWritten = 0;
size_t result;
LPPRINTER_DEFAULTS pDefault;
LPPRINTER_DEFAULTS DesiredAccess;
pFile = fopen ( "reafile.txt" , "rb" );
buffer = (char*) malloc (lSize);
WritePrinter(hPrinter,buffer,lSize,&dwBytesWritten);
fclose (pFile);
free (buffer);
EndPagePrinter( hPrinter );
EndDocPrinter( hPrinter );
ClosePrinter( hPrinter );
return ;
}
void dbmain()
{
HANDLE hPrinter;
char name[256]="Canon MF4320-4350";
OpenPrinter(name, &hPrinter,0);
DWORD x;
DOC_INFO_1 DocInfo;
DocInfo.pDocName="reafile.txt";
DocInfo.pOutputFile=NULL;
DocInfo.pDatatype="RAW";
x=StartDocPrinter(hPrinter,1,(LPBYTE)&DocInfo);
cout<<"JobID: "<<x<<endl;
BOOL z=StartPagePrinter(hPrinter);
if(!z)
{
cout<<"it succeds"<<endl;
}
else{}
thebuffer(hPrinter);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
dbmain();
ShowMessage("Data is sent to printer");
exit( 0);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
HANDLE hPrinter;
char name[256]="Canon MF4320-4350";
OpenPrinter(name, &hPrinter,0);
SetPrinter(hPrinter,0,NULL,PRINTER_CONTROL_PURGE);
ShowMessage("Reset is done");
ClosePrinter( hPrinter );
exit( 0);
}
|
|
|
|
|
Put in some error checking.
Check return values of APIs.
This will help you identify where exactly it fails.
|
|
|
|
|
Hi Santosh,
Ok,, let me do that.. Did you find any mistake in the code..?
|
|
|
|
|
Hi Santosh
I tried with all the possibilities. Its working fine for everything. But SetPrinter API is not functioning properly though it compiles without any error or warning. I am confused.....
|
|
|
|
|
Please reread the replies you've already got.
So, the function didn;t do what you want.
Did it return any status?
Did GetLastError give you any useful error codes?
Win32 is hard sometimes, but it does give clues when it doesn't "work".
Iain.
I am one of "those foreigners coming over here and stealing our jobs". Yay me!
|
|
|
|
|
Hi Iain Clarke,
I didn't get any error codes though I used GetLastError. It is compiling fine,but the API is not working properly..
|
|
|
|
|
Hi Iain Clarke,
I tried with GetLastError. In response it gave me the number 6. This invalid Error handler, right..? So this means is there any wrong in Handler which I used.?
|
|
|
|
|
Hi Santosh,
I tried with GetLastError. In response it gave me the number 6. This invalid Error handler, right..? So this means is there any wrong in Handler which I used.?
|
|
|
|
|
I believe the parameter you're passing to OpenPrinter is wrong and so you're getting an invalid handle.
See the documentation of OpenPrinter for the syntax of the printer name to pass.
|
|
|
|
|
Hi Superman,
When you asked me to change some part in the code it gave lot of runtime exception. And I couldn't solve it, so without any option I changed my code. This code is not giving any kind of exception when I tried to compile and it is printing fine. But if I try to delete the job its not doing that. To find that I tried to trace out the error using GetLastError(). It is giving 6. If am not wrong that means handler is invalid or might have closed. But till my knowledge is concerned am doing all the operations before I close the handler. And I don't think it is invalid since I used the same handler to print the job and it is working fine. So thats why I attached entire code here so that you can find the things easily. Speaking about the program, I used Borland C++ builder to build the project where I have used two Buttons in the GUI part. One to print and other is to delete the job. Sorry for taking much of your time but I need this program badly. Please help me........
#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <winspool.h>
#include <vcl.h>
#pragma hdrstop
#include "reset.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void prinfun(bool val)
{
HANDLE hPrinter = 0;
DWORD dwBytesWritten=0;
string strDocName;
DWORD dwFileLength = 0;
char szData[512];
DWORD dwBufsize=0;
strcpy(szData,"reafile.txt");
OpenPrinter("Canon MF4320-4350", &hPrinter, NULL);
DOC_INFO_1 DocInfo;
DocInfo.pDocName = "reafile.txt";
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = _T("RAW");
StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
StartPagePrinter(hPrinter);
if(val == 0)
{
WritePrinter(hPrinter, (void*)szData, strlen(szData) , &dwBytesWritten);
}
else if(val == 1)
{
GetPrinter(hPrinter, 2, NULL, 0, &dwBufsize);
SetPrinter(hPrinter, 0, NULL, PRINTER_CONTROL_PURGE);
}
free(hPrinter);
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
}
void __fastcall TForm1::printButton1Click(TObject *Sender)
{
int temp = GetLastError();
char buf[300];
prinfun(0);
ShowMessage("Everything is done successfully");
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
}
void __fastcall TForm1::deleteButton2Click(TObject *Sender)
{
int temp = GetLastError();
char buf[300];
prinfun(1);
ShowMessage(" printer jobs deleted");
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
}
|
|
|
|
|
Hi Superman,
Please do reply.. Am in very much need of your help. Please help me..........
|
|
|
|
|
Ananth304 wrote: pFile = fopen ( "reafile.txt" , "rb" );
pFile is not being used.
Ananth304 wrote: buffer = (char*) malloc (lSize);
At this point, lSize is uninitialized.
Ananth304 wrote: WritePrinter(hPrinter,buffer,lSize,&dwBytesWritten);
Writing to buffer , which may or may not have been properly assigned a block of memory.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi DavidCrow,
When you asked me to change some part of the things in the code it gave lot of runtime exception. And I couldn't solve it, so without any option I changed my code. This code is not giving any kind of exception when I tried to compile and it is printing fine. But if I try to delete the job its not doing that. To find that I tried to trace out the error using GetLastError(). It is giving 6. If am not wrong that means handler is invalid or might have closed. But till my knowledge is concerned am doing all the operations before I close the handler. And I don't think it is invalid since I used the same handler to print the job and it is working fine. So thats why I attached entire code here so that you can find the things easily. Speaking about the program, I used Borland C++ builder to build the project where I have used two Buttons in the GUI part. One to print and other is to delete the job. Sorry for taking much of your time but I need this program badly. Please help me........
//---------------------------------------------------------------------------
#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <winspool.h>
#include <vcl.h>
#pragma hdrstop
#include "reset.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void prinfun(bool val)
{
HANDLE hPrinter = 0;
DWORD dwBytesWritten=0;
string strDocName;
DWORD dwFileLength = 0;
char szData[512];
DWORD dwBufsize=0;
//int temp = GetLastError();
//char buf[300];
//strcpy(szData,"TEST TEST TEST TEST TEST");
strcpy(szData,"reafile.txt");
//if (OpenPrinter(szPrinterName, &hPrinter, NULL)) {
//if (OpenPrinter("Canon MF4320-4350", &hPrinter, NULL)) {
OpenPrinter("Canon MF4320-4350", &hPrinter, NULL);
DOC_INFO_1 DocInfo;
DocInfo.pDocName = "reafile.txt";
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = _T("RAW");
//if (StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)) {
//if (StartPagePrinter(hPrinter)) {
StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
StartPagePrinter(hPrinter);
if(val == 0)
{
//BOOL bRet = WritePrinter(hPrinter, (void*)szData, strlen(szData) , &dwBytesWritten);
WritePrinter(hPrinter, (void*)szData, strlen(szData) , &dwBytesWritten);
}
else if(val == 1)
{
GetPrinter(hPrinter, 2, NULL, 0, &dwBufsize);
SetPrinter(hPrinter, 0, NULL, PRINTER_CONTROL_PURGE);
}
free(hPrinter);
//bRet = SetPrinter(hPrinter,0,0,PRINTER_CONTROL_PURGE);
//sprintf(buf,"%d",temp);
//Form1->RichEdit1->Lines->Add(buf);
EndPagePrinter(hPrinter);
//}
EndDocPrinter(hPrinter);
//}
ClosePrinter(hPrinter);
//}
}
// commented this function
/*void prindel()
{
HANDLE hPrinter;
char name[300] = "Canon MF4320-4350";
char szData[512];
int temp = GetLastError();
char buf[300];
DWORD dwBufsize=0;
OpenPrinter("Canon MF4320-4350", &hPrinter, NULL);
GetPrinter(hPrinter, 2, NULL, 0, &dwBufsize);
BOOL bRetc = SetPrinter(hPrinter,0,0,PRINTER_CONTROL_PURGE);
if ( SetPrinter(hPrinter, 0, 0, PRINTER_CONTROL_PURGE)==0 )
{
ShowMessage("SetPrinter call failed");
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
}
else
{
ShowMessage("Number of printer jobs deleted");
}
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
ClosePrinter(hPrinter);
}*/
void __fastcall TForm1::printButton1Click(TObject *Sender)
{
int temp = GetLastError();
char buf[300];
prinfun(0);
ShowMessage("Everything is done successfully");
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
//exit(0);
/*HANDLE hPrinter;
char name[256]="Canon MF4320-4350";
int temp = GetLastError();
char buf[300];
OpenPrinter(name, &hPrinter,0);
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
SetPrinter(hPrinter,0,NULL,PRINTER_CONTROL_PURGE);
ShowMessage("Reset is done");
ClosePrinter( hPrinter );
return; */
}
//---------------------------------------------------------------------------
void __fastcall TForm1::deleteButton2Click(TObject *Sender)
{
int temp = GetLastError();
char buf[300];
prinfun(1);
ShowMessage(" printer jobs deleted");
sprintf(buf,"%d",temp);
Form1->RichEdit1->Lines->Add(buf);
}
|
|
|
|
|
1) It's a lot easier to read code that has been enclosed with <pre> tags. You can easily modify your post with this change.
2) The function prindel() , and parts of prinfun() and printButton1Click() are commented out so should those parts be removed so as to avoid confusion?
Ananth304 wrote: But if I try to delete the job its not doing that.
Probably because there is no job to delete. Unless you are sending many pages to the printer, the job will not exist long enough to delete.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
He should see it on the return code of the API.
I have also some trouble with printers. The drivers of different devices giving different status callbacks in my program. Such a mess
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
Hello all,
I have a class CChildFrame : public CMDIChildWnd : public CFrameWnd
i have declared a new item in sysMenu called "Map Info"
CMenu *pSysMenu = pFrm->GetSystemMenu(FALSE);
ASSERT(pSysMenu != NULL);
pSysMenu->AppendMenu(MF_STRING, IDI_SYS_MENU_MAP_INFO, _T("&Map Info"));
how do i handle the event of clicking that new item in the sysmenu ??
i have looked for something like it on the web but all i found is talking about CDialog and the WM_ON_SYSCOMMAND which doesn't seem to work for me.
anyone ?
Thank's
Eyal
|
|
|
|
|
Create a dialog based application and see how the About menu is added and handled.
It is indeed the ON_WM_SYSCOMMAND macro and the OnSysCommand method.
|
|
|
|
|
But isn't the OnSysCommand used only for CDialog while im using already a CFrame ?
|
|
|
|
|
I just tried it and it works perfectly.
I added the menu from within my frame class.
Added the following in OnCreate -
CMenu *pSysMenu = GetSystemMenu(FALSE);
ASSERT(pSysMenu != NULL);
pSysMenu->AppendMenu(MF_STRING, 10101, _T("&About ..."));
Added ON_WM_SYSCOMMAND() in between the message map macros.
Added the following method -
void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
if (nID == 10101)
MessageBox(L"About me");
CFrameWnd::OnSysCommand(nID, lParam);
}
|
|
|
|
|
I've tried to take ur example and plant it in my code.
the thing is that for some reason, in my Class Wizard i couldn't find WM_SYSCOMMAND message, so i implemented it manually like you said in between the message map macros.
but still, when i click on the "About" item, nothing happen's.. no trigger pumps the event.
maybe something im missing ?
|
|
|
|
|