Skip to content

Instantly share code, notes, and snippets.

@kyab
Created April 11, 2021 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyab/47902d6022eef182bb7efc9ef47c50bb to your computer and use it in GitHub Desktop.
Save kyab/47902d6022eef182bb7efc9ef47c50bb to your computer and use it in GitHub Desktop.
EJL
// USBPrintApp.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#include <Windows.h>
#include <iostream>
#include <cstdlib>
#include <SetupAPI.h>
#include <wdmguid.h>
#include <strsafe.h>
#include <winusb.h>
GUID GUID_DEVINTERFACE_USBPRINT = {
0x28d78fad, 0x5a12, 0x11D1, {0xae,
0x5b, 0x00, 0x00, 0xf8, 0x03, 0xa8, 0xc2} };
BOOL GetDevicePath(LPGUID interface_guid, LPTSTR device_path, size_t buf_len)
{
BOOL result = FALSE;
HDEVINFO device_info;
SP_DEVICE_INTERFACE_DATA interface_data;
PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = NULL;
ULONG length;
ULONG required_length = 0;
HRESULT hr;
device_info = SetupDiGetClassDevs(interface_guid, NULL, NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
result = SetupDiEnumDeviceInterfaces(device_info, NULL, interface_guid, 0,
&interface_data);
SetupDiGetDeviceInterfaceDetail(device_info, &interface_data, NULL, 0,
&required_length, NULL);
detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED,
required_length);
if (detail_data == NULL) {
SetupDiDestroyDeviceInfoList(device_info);
return FALSE;
}
detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
length = required_length;
result = SetupDiGetDeviceInterfaceDetail(device_info, &interface_data,
detail_data, length,
&required_length, NULL);
if (result == FALSE) {
LocalFree(detail_data);
return FALSE;
}
hr = StringCchCopy(device_path, buf_len, detail_data->DevicePath);
if (FAILED(hr)) {
SetupDiDestroyDeviceInfoList(device_info);
LocalFree(detail_data);
return FALSE;
}
return result;
}
int main()
{
TCHAR devicePath[_MAX_PATH + 1];
BOOL ret = GetDevicePath(&GUID_DEVINTERFACE_USBPRINT, (LPTSTR)devicePath,
sizeof(devicePath) / sizeof(devicePath[0]));
if (!ret) {
printf("error\n");
return -1;
}
printf("%ls\n", devicePath);
HANDLE deviceHandle = CreateFile(
(LPTSTR)devicePath, GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL/* | FILE_FLAG_OVERLAPPED*/, NULL);
if (!deviceHandle) {
printf("error at CreateFile\n");
return -1;
}
DWORD written = 0;
char foo[1024];
/*
foo[0] = 0x00;
foo[1] = 0x00;
foo[2] = 0x00;
foo[3] = 0x1b;
foo[4] = 0x01;
memcpy(foo + 5, "@EJL ID\x0a", strlen("@EJL ID\x0a"));
ret = WriteFile(deviceHandle, (LPCVOID)foo,13, &written, NULL);
char foo2[1024];
ZeroMemory(foo2, 1024);
DWORD read = 0;
ret = ReadFile(deviceHandle, foo2, 1024, &read, NULL);
printf("%s\n", foo2);*/
written = 0;
foo[0] = 0x00;
foo[1] = 0x00;
foo[2] = 0x00;
foo[3] = 0x1b;
foo[4] = 0x01;
const char* command = "@EJL ID\x0a";
memcpy(foo/* + 5*/, command, strlen(command));
//memcpy(foo + 5, "@EJL 1284.4\x0a@EJL \x0a", strlen("@EJL 1284.4\x0a@EJL \x0a"));
//memcpy(foo + 5, "@EJL GET COPIES\x0d\x0a", strlen("@EJL GET COPIES\x0d\x0a"));
ret = WriteFile(deviceHandle, (LPCVOID)foo,/*5+ */strlen(command), &written, NULL);
char foo2[1024];
ZeroMemory(foo2, 1024);
DWORD read = 0;
ret = ReadFile(deviceHandle, foo2, 1024, &read, NULL);
printf("%s\n", foo2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment