5 #include "windows.bi"#include "windows.bi"
6 #include "win/winioctl.bi"#include "win/winioctl.bi"
7 #include "../fbportio.h"#include "../fbportio.h"
9 #define INFO(message)
print __FUNCTION__ &
"(): " & message
13 dim as DWORD e = GetLastError()
14 print *
parent &
"(): " & *func &
" failed, GetLastError(): &h" &
hex(e) &
" / " & e
16 dim as zstring ptr p =
NULL
17 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
or FORMAT_MESSAGE_FROM_SYSTEM, _
32 dim as SC_HANDLE service = OpenService(
manager,
"fbportio", SERVICE_ALL_ACCESS)
33 if (service =
NULL)
then
34 APIFAILED(
"OpenService()")
37 INFO(
"Marking the fbportio service for deletion...")
38 INFO(
"(This will fail if the service is already marked for deletion)")
39 INFO(
"(If the service is running, it will be deleted after it stops)")
41 if (DeleteService(service) = 0)
then
42 APIFAILED(
"DeleteService()")
45 CloseServiceHandle(service)
49 dim as SC_HANDLE service = OpenService(
manager,
"fbportio", SERVICE_ALL_ACCESS)
50 if (service =
NULL)
then
51 APIFAILED(
"OpenService()")
54 INFO(
"Stopping the fbportio service...")
56 dim as SERVICE_STATUS status
57 if (ControlService(service, SERVICE_CONTROL_STOP, @status) = 0)
then
58 APIFAILED(
"ControlService(SERVICE_CONTROL_STOP)")
61 CloseServiceHandle(service)
65 INFO(
"Copying fbportio.sys into the system's driver directory...")
67 dim as zstring * (MAX_PATH+1) filename
68 dim as integer needed = GetSystemDirectory(@filename, MAX_PATH)
69 if ((needed = 0)
or (needed > MAX_PATH))
then
70 APIFAILED(
"GetSystemDirectory()")
73 filename +=
"\Drivers\fbportio.sys"
75 if (CopyFile(
"fbportio.sys", filename, FALSE) = 0)
then
76 APIFAILED(
"CopyFile()")
81 INFO(
"Creating fbportio service...")
83 dim as SC_HANDLE service = _
88 SERVICE_KERNEL_DRIVER, _
89 SERVICE_DEMAND_START, _
90 SERVICE_ERROR_NORMAL, _
91 "system32\drivers\fbportio.sys", _
98 if (service =
NULL)
then
99 APIFAILED(
"CreateService()")
102 CloseServiceHandle(service)
106 dim as SC_HANDLE service = OpenService(
manager,
"fbportio", SERVICE_ALL_ACCESS)
107 if (service =
NULL)
then
108 APIFAILED(
"OpenService()")
111 INFO(
"Starting fbportio service...")
113 if (StartService(service, 0,
NULL) = 0)
then
114 APIFAILED(
"StartService()")
117 CloseServiceHandle(service)
123 byval code
as DWORD, _
124 byval pin
as any ptr, _
125 byval insize
as uinteger, _
126 byval pout
as any ptr, _
127 byval outsize
as uinteger _
130 INFO(
"Accessing driver...")
132 dim as DWORD outwritten = 0
133 if (DeviceIoControl(h, code, pin, insize, pout, outsize, @outwritten,
NULL) = 0)
then
134 APIFAILED(
"DeviceIoControl()")
139 INFO(
"Initializing driver...")
141 static as integer initialized = FALSE
142 if (initialized)
then
146 INFO(
"Opening '\\.\fbportio'...")
148 dim as HANDLE driver = _
149 CreateFile(
"\\.\fbportio", _
150 GENERIC_READ
or GENERIC_WRITE, _
154 FILE_ATTRIBUTE_NORMAL, _
156 if (driver = INVALID_HANDLE_VALUE)
then
157 APIFAILED(
"CreateFile()")
160 INFO(
"Asking driver for version number...")
161 dim as WORD version = 0
163 if (version <> FBPORTIO_VERSION)
then
164 INFO(
"Driver version mismatch, aborting...")
169 INFO(
"Asking driver to grant our pid i/o ports access...")
170 dim as DWORD pid = GetCurrentProcessId()
207 INFO(
"Opening service control manager...")
210 APIFAILED(
"OpenSCManager()")
211 INFO(
"Opening service control manager in read only mode...")
214 APIFAILED(
"OpenSCManager()")
219 dim as integer show_help = (
__FB_ARGC__ < 2)
221 for i
as integer = 1
to (
__FB_ARGC__ - 1)
222 dim as zstring ptr arg =
__FB_ARGV__[i]
229 case "-i",
"--install"
233 case "-u",
"--uninstall"
244 if (i >=
__FB_ARGC__)
then exit for
245 dim as zstring ptr port =
__FB_ARGV__[i-1]
246 dim as zstring ptr dat =
__FB_ARGV__[i]
252 if (i >=
__FB_ARGC__)
then exit for
253 dim as zstring ptr port =
__FB_ARGV__[i]
257 print "Data recieved: " +
str(dat)
260 print "Unexpected command line argument: '" + *arg +
"'"
272 print "-i, --install"
273 print "-u, --uninstall"
276 print "{-w|--write} <port> <data>"
277 print "{-r|--read} <port>"