34 #define DBG_PROGNAME "OPENCBM-XA1541.DLL"
35 #endif // #ifndef DBG_PROGNAME
49 #define OPENCBM_PLUGIN
71 cbm_get_default_port(VOID)
82 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
98 " value, setting 0."));
117 static HANDLE CancelEvent = NULL;
118 static HANDLE CancelCallbackEvent = NULL;
119 static ULONG InCancellableState = 0;
137 CancelEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
140 CancelCallbackEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
166 if (CancelEvent != NULL)
167 CloseHandle(CancelEvent);
183 if (InterlockedExchange(&InCancellableState, 0) != 0)
190 SetEvent(CancelEvent);
197 WaitForSingleObject(CancelCallbackEvent, INFINITE);
221 memset(Overlapped, 0,
sizeof(*Overlapped));
222 Overlapped->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
260 BOOL result = Result;
261 HANDLE handleList[2] = { CancelEvent, Overlapped->hEvent };
271 switch (GetLastError())
273 case ERROR_IO_PENDING:
281 tmp = InterlockedExchange(&InCancellableState, 1);
288 if (WaitForMultipleObjects(2, handleList, FALSE, INFINITE) == WAIT_OBJECT_0)
290 CancelIo(HandleDevice);
293 *BytesTransferred = 0;
295 SetEvent(CancelCallbackEvent);
303 if (InterlockedExchange(&InCancellableState, 0) == 0)
310 SetEvent(CancelCallbackEvent);
314 result = GetOverlappedResult(HandleDevice, Overlapped,
315 BytesTransferred, FALSE) ;
322 CloseHandle(Overlapped->hEvent);
350 static char driverName[] =
"\\\\.\\opencbm0";
361 portNumber = strtoul(Port, NULL, 10);
368 if (portNumber <= 10)
374 portNumber = cbm_get_default_port();
377 driverName[strlen(driverName)-1] = (portNumber ? portNumber-1 : 0) +
'0';
407 const char *driverName;
415 if (driverName == NULL)
419 *HandleDevice = INVALID_HANDLE_VALUE;
425 *HandleDevice = CreateFile(driverName,
426 GENERIC_READ | GENERIC_WRITE,
430 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
434 DBG_ASSERT(*HandleDevice != INVALID_HANDLE_VALUE);
460 CloseHandle(HandleDevice);
508 IN
char *TextControlCode,
510 IN PVOID InBuffer, IN ULONG InBufferSize, OUT PVOID OutBuffer, IN ULONG OutBufferSize)
512 OVERLAPPED overlapped;
513 DWORD dwBytesReturned;
523 returnValue = DeviceIoControl(HandleDevice, ControlCode, InBuffer, InBufferSize,
524 OutBuffer, OutBufferSize, &dwBytesReturned, &overlapped);
526 returnValue =
WaitForIoCompletion(returnValue, HandleDevice, &overlapped, &dwBytesReturned);
538 if (dwBytesReturned != OutBufferSize)
541 TextControlCode, OutBufferSize, dwBytesReturned));
566 SC_HANDLE schManager;
567 SC_HANDLE schService;
575 schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
577 if (schManager == NULL)
585 if (schService == NULL)
591 ret = StartService(schService, 0, NULL);
599 err = GetLastError();
601 if (err == ERROR_SERVICE_ALREADY_RUNNING)
617 CloseServiceHandle(schService);
621 CloseServiceHandle(schManager);
641 SERVICE_STATUS serviceStatus;
642 SC_HANDLE schManager;
643 SC_HANDLE schService;
648 schManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
652 if (schManager == NULL)
658 if (schService == NULL)
664 ret = ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus);
675 CloseServiceHandle(schService);
679 CloseServiceHandle(schManager);
736 OPENCBM_VERSION_SUBMINOR, OPENCBM_VERSION_DEVEL);
771 automaticStart = FALSE;
773 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
786 if (
RegGetDWORD(regKey,
"Start", &ret) != ERROR_SUCCESS)
792 if (ret == SERVICE_AUTO_START)
794 automaticStart = TRUE;
#define CBM_I_DRIVER_INSTALL_0_FAILED
CBMCTRL_I_INSTALL: The driver could not be opened.
void CBMAPIDECL opencbm_plugin_driver_close(CBM_FILE HandleDevice)
Closes the driver.
const char *CBMAPIDECL opencbm_plugin_get_driver_name(const char *const Port)
Get the name of the driver for a specific parallel port.
int CBMAPIDECL opencbm_plugin_driver_open(CBM_FILE *HandleDevice, const char *const Port)
Opens the driver.
#define FUNC_LEAVE_INT(_xxx)
VOID WaitForIoCompletionCancelAll(VOID)
Cancel any running WaitForIoCompletion()
Define makros for debugging purposes.
VOID WaitForIoCompletionInit(VOID)
Initialize WaitForIoCompletion()
#define CBM_I_DRIVER_INSTALL_0_IOCTL_FAILED
CBMCTRL_I_INSTALL: The IOCTL failed.
#define CBM_REGKEY_SERVICE
LONG RegGetDWORD(IN HKEY RegKey, IN char *SubKey, OUT LPDWORD Value)
Get a DWORD value from the registry.
#define OPENCBM_DRIVERNAME
Define the IOCTL codes for the opencbm driver.
#define CBM_REGKEY_SERVICE_DEFAULTLPT
BOOL cbm_driver_install(OUT PULONG Buffer, IN ULONG BufferLen)
Complete driver installation, "direct version".
#define CBMT_I_INSTALL_OUT_MAKE_VERSION(_x, _y, _z, _w)
#define FUNC_LEAVE_BOOL(_xxx)
#define CBMT_I_INSTALL_OUT_MAKE_VERSION_EX(_w)
#define DBG_SUCCESS(_xxx)
VOID WaitForIoCompletionConstruct(LPOVERLAPPED Overlapped)
Boilerplate code for asynchronous I/O requests.
Defining OpenCBM version.
BOOL cbm_ioctl(IN CBM_FILE HandleDevice, IN DWORD ControlCode, IN char *TextControlCode, IN PVOID InBuffer, IN ULONG InBufferSize, OUT PVOID OutBuffer, IN ULONG OutBufferSize)
Perform an ioctl on the driver.
#define FUNC_LEAVE_STRING(_xxx)
BOOL cbm_driver_stop(VOID)
Stop a device driver.
BOOL cbm_driver_start(VOID)
Start a device driver.
Internal API for opencbm installation.
VOID WaitForIoCompletionDeinit(VOID)
Uninitialize WaitForIoCompletion()
BOOL WaitForIoCompletion(BOOL Result, CBM_FILE HandleDevice, LPOVERLAPPED Overlapped, DWORD *BytesTransferred)
Wait for the completion of an I/O operation.
BOOL IsDriverStartedAutomatically(VOID)
Is the driver started automatically?