100分,大家看这个程序错在哪儿了??????
wiler 2002-03-14 06:19:43 //请仔细看程序
int ret,i;
DWORD Errno,retNum=0,buf[2000],len=sizeof(PRINTER_INFO_4);
PRINTER_INFO_2 pinfo2;
FORM_INFO_1 *pinfo1;
DWORD retValue,cbNeed,NeedSize;
TCHAR Text[128]="Letter",PrinterName[128];
HANDLE Printer;
LPDEVMODE lpmod;
DEVMODE devmod;
PRINTDLG pda;
PRINTER_DEFAULTS pDefault;
memset(&pda,0,sizeof(pda));
pda.lStructSize = sizeof(pda);
pda.Flags = PD_RETURNDEFAULT|PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION;//PD_RETURNDEFAULT使之获得默认打印机的设置且不显示对话框
ret=::PrintDlg(&pda); // 获得缺省打印机设置
Errno=::GetLastError();
if(Errno==ERROR_SUCCESS){
if (pda.hDevMode !=NULL){
lpmod = (LPDEVMODE)GlobalLock(pda.hDevMode);
::MessageBox (NULL,(char *)lpmod->dmDeviceName ,"Name",MB_OK);
strcpy(PrinterName,(char *)lpmod->dmDeviceName );
::GlobalUnlock(pda.hDevMode);
}
}
else{
sprintf(Text,"Failed,errno=%d",Errno);
::MessageBox(NULL,Text,"Test",MB_OK);
}
memset(&pDefault,0,sizeof(pDefault));
memset(&devmod,0,sizeof(devmod));
memset(&pinfo2,0,sizeof(pinfo2));
pDefault.DesiredAccess =PRINTER_ALL_ACCESS;
ret=::OpenPrinter(PrinterName,&Printer,&pDefault);
Errno=::GetLastError();
if(Errno){
sprintf(Text,"Errno=%d",Errno );
::MessageBox (NULL,Text,"Failed",MB_OK);
}else
::MessageBox (NULL,"OpenPrinter Success!","OpenPrinter Sucess",MB_OK);
if(EnumForms(Printer,1,NULL,0,&cbNeed,&retValue))
{
if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
ClosePrinter( Printer );
MessageBox("Failed");
return;
}
}
NeedSize=cbNeed;
pinfo1=(FORM_INFO_1 *)malloc( NeedSize );
// 只要加入下面两个语句就可以成功,为什么?
//sprintf(Text,"Need=%d||retValue=%d",cbNeed,retValue );
//::MessageBox (NULL,Text,"Success",MB_OK);
EnumForms(Printer,1,(LPBYTE)pinfo1,NeedSize,&cbNeed,&retValue);
Errno=::GetLastError();
if(Errno){
sprintf(Text,"Errno=%d",Errno );
::MessageBox (NULL,Text,"Failed",MB_OK);
}else
{
MessageBox ("EnumForm Success!","Sucess",MB_OK);
for( i=0;i<(int)retValue;i++)
{
ToUpper(pinfo1[i].pName);
if(!strcmp("TEST",pinfo1[i].pName))
{
sprintf(Text,"index=%d",i+1);
MessageBox(pinfo1[i].pName,Text);
}
}
}
free(pinfo1);
ClosePrinter(Printer);