C++ 调用有驱动的USB小票打印机方法2

这种办法可以指定小票打印机,在里面执行POS58指令,但是必须安装驱动

#if 0
HANDLE OpenDevice(LPGUID pGuid, char *outNameBuf)
{
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;

hardwareDeviceInfo = SetupDiGetClassDevs(pGuid, NULL, NULL, (DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));

deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

if (SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, pGuid, 0, &deviceInfoData))
{
hOut = OpenOneDevice(hardwareDeviceInfo, &deviceInfoData, outNameBuf);

if (hOut == INVALID_HANDLE_VALUE)
return INVALID_HANDLE_VALUE;
}

DWORD reErr = GetLastError();

SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);

return hOut;
}

HANDLE OpenOneDevice(HDEVINFO HardwareDeviceInfo, PSP_INTERFACE_DEVICE_DATA DeviceInfoData, char *devName)
{
PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
HANDLE hOut = INVALID_HANDLE_VALUE;

// allocate a function class device data structure to receive the goods about this particular device.
SetupDiGetInterfaceDeviceDetail(HardwareDeviceInfo, DeviceInfoData, NULL, 0, &requiredLength, NULL);

predictedLength = requiredLength;
// sizeof (SP_FNCLASS_DEVICE_DATA) + 512;

functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength);
functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

// Retrieve the information from Plug and Play.
if (!SetupDiGetInterfaceDeviceDetail(
HardwareDeviceInfo,
DeviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL))
{
free(functionClassDeviceData);
return INVALID_HANDLE_VALUE;
}

strcpy(devName, functionClassDeviceData->DevicePath);
printf("Attempting to open %s\n ", devName);

hOut = CreateFile(functionClassDeviceData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);

if (INVALID_HANDLE_VALUE == hOut)
printf("FAILED to open %s\n ", devName);

free(functionClassDeviceData);

return hOut;
}


HANDLE hDEV = OpenDevice((LPGUID)&GUID_BUS_UMSS, completeDeviceName);
if (hDEV == INVALID_HANDLE_VALUE)
return;

DeviceIoControl(
hDEV,
IOCTL_Vender_VCMD_WRITE,
buf,
31, //CBW
buf,
13, // Data + CSW
(LPDWORD)&nBytes,
NULL
);
#endif


void padTo(std::string &str, const size_t num)
{
const char paddingChar = ' ';
if (num > str.size())
str.insert(0, num - str.size(), paddingChar);
}


void double2str(double num, string & resultstr)
{
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << num;
resultstr = stream.str();
}


i

 

int testprint(const char * posPrintername)
{
LPTSTR szPrinterName;
//LPBYTE lpDatainitial, lpBolder, lpdoubleheight, lpdoublewidth, lpreset;
LPBYTE lpData;

DWORD dwCount;
HANDLE hPrinter;
DOC_INFO_1 DocInfo;
DWORD dwJob;
DWORD dwBytesWritten;


wchar_t * posPrinternamew = new wchar_t[strlen(posPrintername) + 1];
mbstowcs_s(NULL, posPrinternamew, strlen(posPrintername) + 1, posPrintername, strlen(posPrintername));



std::string initialstr = "\x1b\x40";

std::string doubletimesfontsize = "\x1b\x21\x30";

std::string normalfontsize = "\x1b\x21\x01";


std::string resetstr = "\x00";

std::string printcontents = "测试测试测试测试测试测试\n ";


lpData = (LPBYTE)printcontents.c_str();
dwCount = strlen(printcontents.c_str());

// Need a handle to the printer.

szPrinterName = posPrinternamew;
if (!OpenPrinter(szPrinterName, &hPrinter, NULL))
{

return -1;
}

// Fill in the structure with info about this "document."
DocInfo.pDocName = TEXT("My Document");
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = TEXT("RAW");
// Inform the spooler the document is beginning.
if ((dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)) == 0)
{

ClosePrinter(hPrinter);
return -2;
}
// Start a page.
if (!StartPagePrinter(hPrinter))
{

EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -3;
}
// Send the data to the printer.
if (!WritePrinter(hPrinter, (LPBYTE)initialstr.c_str(), strlen(initialstr.c_str()), &dwBytesWritten))
{

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}

// Send the data to the printer.

 

if (!WritePrinter(hPrinter, (LPBYTE)doubletimesfontsize.c_str(), strlen(doubletimesfontsize.c_str()), &dwBytesWritten))
{

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}

// Send the data to the printer.
if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
{

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}


if (!WritePrinter(hPrinter, (LPBYTE)normalfontsize.c_str(), strlen(normalfontsize.c_str()), &dwBytesWritten))
{

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}

// Send the data to the printer.
if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
{

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}

if (!WritePrinter(hPrinter, (LPBYTE)resetstr.c_str(), strlen(resetstr.c_str()), &dwBytesWritten))
{

EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -4;
}
// End the page.
if (!EndPagePrinter(hPrinter))
{

EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
return -5;
}
// Inform the spooler that the document is ending.
if (!EndDocPrinter(hPrinter))
{

ClosePrinter(hPrinter);
return -6;
}
// Tidy up the printer handle.
ClosePrinter(hPrinter);
// Check to see if correct number of bytes were written.
if (dwBytesWritten != dwCount)
{

return -7;
}
return 0;
}

 

posted on 2021-01-08 14:58  redmondfans  阅读(530)  评论(0编辑  收藏  举报