void CTestPlace2008Dlg::OnBnClickedUsb()
{
// TODO: Add your control notification handler code here
// Try to get the devices on the USB. If found try to communicate
// with it. Skip the Host Controller enumeration, finding root
// hubs and then look for devices.
/*
Use this block if I have to enumerate Host controllers, find root
hubs and then look for devices.
char HCDName[16];
int HCDNum;
HANDLE hHCDev;
*/
HDEVINFO devs;
SP_DEVINFO_DATA devinfo;
SP_DEVICE_INTERFACE_DATA devinterface;
GUID intfce;
PSP_DEVICE_INTERFACE_DETAIL_DATA interface_detail;
ULONG index;
ULONG requiredLength;
intfce = GUID_DEVINTERFACE_USBPRINT;
devs = SetupDiGetClassDevs(&intfce, //&GUID_DEVINTERFACE_USBPRINT,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_INVALID_FLAGS
|| devs == INVALID_HANDLE_VALUE) {
SetupDiDestroyDeviceInfoList(devs);
return;
}
ZeroMemory(&devinterface, sizeof(SP_DEVICE_INTERFACE_DATA));
devinterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
devinterface.Flags = 0;
for (index = 0;
SetupDiEnumDeviceInterfaces(devs,
0,
&intfce, //&GUID_DEVINTERFACE_USBPRINT,
index,
&devinterface);
index++)
{
// Clear out error list
GetLastError();
char* interfacename;
// Allocate space
interfacename = (char*) malloc(2048);
// Zero out buffer
ZeroMemory(interfacename, 2048);
requiredLength = 0;
if (!SetupDiGetDeviceInterfaceDetail(devs,
&devinterface,
NULL,
0,
&requiredLength,
NULL)) {
if (GetLastError() != 122){ // If not wrong size
char* buf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
0,
(LPTSTR) &buf,
0,
NULL);
char* myerrmsg;
myerrmsg = (char*) malloc(128);
wsprintf(myerrmsg,"Error # = %d\n%s", GetLastError(), buf);
AfxMessageBox(myerrmsg, MB_OK, 0);
return;
}
}
// interface_detail = (SP_DEVICE_INTERFACE_DETAIL_DATA*) malloc(requiredLength);
interface_detail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) calloc(1, requiredLength);
if (interface_detail == NULL)
AfxMessageBox("Memory allocation failed!",MB_OK,0);
else {
ZeroMemory(interface_detail, sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA));
interface_detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
devinfo.cbSize = sizeof(SP_DEVINFO_DATA);
SetupDiGetDeviceInterfaceDetail(devs,
&devinterface,
interface_detail,
requiredLength,
&requiredLength,
&devinfo);
//interfacename = interface_detail->DevicePath;
strcpy_s(interfacename, 0x800, interface_detail->DevicePath);
}
// strcpy_s(interfacename, sizeof("\\\\.\\USB001"), "\\\\.\\USB001");
HANDLE BradyUSB = CreateFile(interfacename,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN | FILE_FLAG_OVERLAPPED,
NULL);
if (BradyUSB == INVALID_HANDLE_VALUE){
char* mybuf;
mybuf = (char*) malloc(128);
wsprintf(mybuf, "Did not open Brady printer");
AfxMessageBox(mybuf, MB_OK | MB_ICONEXCLAMATION, 0);
free(mybuf);
} else {
// Write to the printer
OVERLAPPED olWrite = { 0 };
olWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
char cCommand1, cCommand2, cTermChar, cAllCommand[5];
DWORD dwBytesWritten;
DWORD dwBytesToWrite;
bool writeSuccess;
cCommand1 = 0x01;
cCommand2 = '#';
cTermChar = '\n';
dwBytesToWrite = 3;
if(!WriteFile(BradyUSB, cAllCommand, dwBytesToWrite, &dwBytesWritten, &olWrite)) {
// Wait until write operation is complete
bool done = false;
while (!done) {
WaitForSingleObject(olWrite.hEvent, INFINITE);
if (!GetOverlappedResult(BradyUSB, &olWrite, &dwBytesWritten, FALSE)) {
if (!(GetLastError() == ERROR_IO_PENDING)) {
// Get out of loop and check for bytes written
done = true;
} // IO Pending
} else // GetOverlappedResult
done = true;
} // while loop
} // write file
if (dwBytesWritten == 0){
char* mywriteerrorbuf;
mywriteerrorbuf = (char*) malloc(64);
wsprintf(mywriteerrorbuf, "Write error!");
AfxMessageBox(mywriteerrorbuf, MB_OK | MB_ICONEXCLAMATION, 0);
free(mywriteerrorbuf);
writeSuccess = false;
} else
writeSuccess = true;
if (writeSuccess) { // Write was a success, let's read
// Clear the error
SetLastError(0);
ResetEvent(olWrite.hEvent);
char myreadbuf[10];
OVERLAPPED olRead = { 0 };
olRead.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
DWORD dwBytesRead;
myreadbuf[9] = '\0';
if (!ReadFile(BradyUSB, &myreadbuf, 0x100, &dwBytesRead, &olRead)) {
// Wait for read operation to complete
int retval = WaitForSingleObject(olRead.hEvent,5000);
}
if (dwBytesRead == 0) {
// Nothing was read.
char* myreaderrorbuf;
myreaderrorbuf = (char*) malloc(64);
wsprintf(myreaderrorbuf, "Nothing to read!");
AfxMessageBox(myreaderrorbuf, MB_OK | MB_ICONEXCLAMATION, 0);
free(myreaderrorbuf);
}
} // Write success
} // Printer handle
if (BradyUSB != INVALID_HANDLE_VALUE)
CloseHandle(BradyUSB);
} // for loop
SetupDiDestroyDeviceInfoList(devs);
}
The CreateFile gives a valid handle. The interface name from the device detail devicepath is:
\\?\usb#vid_0b0b&pid_106e#5&11dad59d&0&1#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}.
Thanks,
HyperEngineer

New Topic/Question
Reply




MultiQuote





|