- 1 year ago
GetCommTimeouts returns error 87 for network printer
The error code 87 returned by the GetCommTimeouts
function usually indicates an invalid parameter. However, it's important to note that GetCommTimeouts
is designed to work with serial communication ports (COM ports) and is not intended for network printers or other network devices.
To interact with a network printer in Windows, you'll need to use different methods and APIs specific to printer management. The Windows API provides various functions and structures for managing printers, such as OpenPrinter
, StartDocPrinter
, WritePrinter
, etc.
Here's a simple example of how you can retrieve the configuration of a network printer using the Windows API in C++:
cpp#include <Windows.h>
#include <Winspool.h>
void GetPrinterTimeouts(const wchar_t* printerName)
{
HANDLE printerHandle;
PRINTER_DEFAULTS printerDefaults = { nullptr, nullptr, PRINTER_ACCESS_USE };
if (OpenPrinter(const_cast<wchar_t*>(printerName), &printerHandle, &printerDefaults))
{
PRINTER_INFO_2* printerInfo = nullptr;
DWORD bytesNeeded;
// Get the required buffer size
GetPrinter(printerHandle, 2, nullptr, 0, &bytesNeeded);
if (bytesNeeded > 0)
{
printerInfo = reinterpret_cast<PRINTER_INFO_2*>(malloc(bytesNeeded));
if (printerInfo && GetPrinter(printerHandle, 2, reinterpret_cast<LPBYTE>(printerInfo), bytesNeeded, &bytesNeeded))
{
// Access the printer configuration
// Example: printerInfo->pDevMode, printerInfo->pSecurityDescriptor, etc.
// Access the timeouts
COMMTIMEOUTS timeouts = printerInfo->pDevMode->dmDeviceName;
// Example: timeouts.ReadIntervalTimeout, timeouts.WriteTotalTimeoutConstant, etc.
// Output the timeouts or perform further operations
// Clean up
free(printerInfo);
}
}
ClosePrinter(printerHandle);
}
}
int main()
{
const wchar_t* printerName = L"\\\\server\\printer"; // Replace with your network printer name
GetPrinterTimeouts(printerName);
return 0;
}
Remember to replace "\\\\server\\printer"
with the appropriate network printer name or UNC path.
It's essential to consult the Microsoft documentation and familiarize yourself with the Windows API functions and structures related to printer management for further customization and advanced operations.