🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.comp.lang.vc
Article #1

[请教]关于GetDefaultPrint函数

#1
From: fd0129002
Date: Tue, 22 Apr 2008 04:37
18 lines
2580 bytes
请教各位朋友一个问题

以下是我调用GetDefaultPrint函数的代码摘要
现象是
1.当默认打印机名字中含有非ASCII字符时,free(pszDefaultPtr );失败。Debug发现,原因是第一次调用
GetDefaultPrint时返回的buffer大小(dwPrinterName)并不足以存放默认打印机名,于是在第二次调用的时候溢出了。我
第一次调用GetDefaultPrint的时候,GetLastError的返回值是ERROR_INSUFFICIENT_BUFFER。按照
MSDN的说法(The pszBuffer buffer is too small. The variable pointed to by
pcchBuffer contains the required buffer size, in characters. ),貌似不应该在第二
次调用的时候溢出的。
2.当默认打印机名字中只含有ASCII字符时,一切正常。

问题:这是不是GetDefaultPrint函数的一个BUG?或者是我调用的代码有问题?

#include <Winspool.h>
#pragma comment(lib, "Winspool.lib")

...
LPTSTR pszDefaultPtr = NULL;
DWORD dwPrinterName = 0;

...
GetDefaultPrinter(pszDefaultPtr, &dwPrinterName);
pszDefaultPtr = (LPTSTR)calloc(dwPrinterName, sizeof(TCHAR));
GetDefaultPrinter(pszDefaultPtr, &dwPrinterName);
...
free(pszDefaultPtr );
pszDefaultPtr  = NULL;
Technical Details
Message-ID: <3de7c89c-c685-4440-a59f-d6246f448e58@w5g2000prd.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!w5g2000prd.googlegroups.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: fe22c,9ef8096e33a0310b,start
X-Google-Attributes: gidfe22c,domainid0,public,usenet
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!w5g2000prd.googlegroups.com!not-for-mail
From: "fd0129002@gmail.com" <fd0129002@gmail.com>
Newsgroups: cn.comp.lang.vc
Subject: [请教]关于GetDefaultPrint函数
Date: Mon, 21 Apr 2008 21:37:35 -0700 (PDT)
Organization: http://groups.google.com
Lines: 18
Message-ID: <3de7c89c-c685-4440-a59f-d6246f448e58@w5g2000prd.googlegroups.com>
NNTP-Posting-Host: 202.216.167.162
Mime-Version: 1.0
Content-Type: text/plain; charset=GB2312
Content-Transfer-Encoding: base64
X-Trace: posting.google.com 1208839055 22512 127.0.0.1 (22 Apr 2008 04:37:35 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 22 Apr 2008 04:37:35 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: w5g2000prd.googlegroups.com; posting-host=202.216.167.162; 
 posting-account=6ek7OAoAAADhDNo_7ec0Nhm5awLwsBlJ
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) 
 Gecko/20080404 Firefox/2.0.0.14,gzip(gfe),gzip(gfe)