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);
...全文
62 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wiler 2002-03-15
  • 打赏
  • 举报
回复
To glxSearoc(黑土豆) :
你说得完全正确,100分不给你给谁,多谢了!
Kusk 2002-03-14
  • 打赏
  • 举报
回复
会不会是内存之类的错误?

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧