使用接口 EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &size, &count);获取到的size和count参数是一样的,而且数值特别大,但是后面访问打印机名的时候出现异常crash,为什么?
DWORD size= 0;
DWORD count = 0;
::EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &size, &count);
BYTE* buffer = new BYTE[size];
::EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 2, buffer, size, &size, &count);
if (count != 0)
{
PRINTER_INFO_2* info = (PRINTER_INFO_2*)buffer;
for (DWORD i = 0; i < count; i++)
{
Printer printer;
printer.pName = createCopyString(info->pPrinterName); // Causes a crash here
++info;
}
查阅了官方文档,没有帮助