ref: 92c9d5c388ab91ade416539438fb0b8da9cd50bb
parent: 77ea97c6277cc7261e332deec5d48fbddd88821a
author: Simon Howard <fraggle@gmail.com>
date: Sat Feb 27 14:11:24 EST 2010
Use wide-character versions of Win32 API functions. Clean up properly if it was not possible to start the ioperm service. Subversion-branch: /branches/opl-branch Subversion-revision: 1875
--- a/opl/ioperm_sys.c
+++ b/opl/ioperm_sys.c
@@ -35,8 +35,10 @@
#include <errno.h>
-#define IOPERM_FILE "\\\\.\\ioperm"
+#include "ioperm_sys.h"
+#define IOPERM_FILE L"\\\\.\\ioperm"
+
#define IOCTL_IOPERM \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0xA00, METHOD_BUFFERED, FILE_ANY_ACCESS)
@@ -59,8 +61,8 @@
DWORD BytesReturned;
BOOL r;
- h = CreateFile(IOPERM_FILE, GENERIC_READ, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ h = CreateFileW(IOPERM_FILE, GENERIC_READ, 0, NULL,
+ OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE)
{
@@ -97,12 +99,12 @@
int error;
int result = 1;
- scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
+ scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (scm == NULL)
{
error = GetLastError();
- fprintf(stderr, "IOperm_InstallDriver: OpenSCManager failed (%i)\n",
+ fprintf(stderr, "IOperm_InstallDriver: OpenSCManager failed (%i).\n",
error);
return 0;
}
@@ -134,12 +136,12 @@
if (error != ERROR_SERVICE_EXISTS)
{
fprintf(stderr,
- "IOperm_InstallDriver: Failed to create service (%i)\n",
+ "IOperm_InstallDriver: Failed to create service (%i).\n",
error);
}
else
{
- svc = OpenService(scm, TEXT("ioperm"), SERVICE_ALL_ACCESS);
+ svc = OpenServiceW(scm, L"ioperm", SERVICE_ALL_ACCESS);
if (svc == NULL)
{
@@ -146,7 +148,7 @@
error = GetLastError();
fprintf(stderr,
- "IOperm_InstallDriver: Failed to open service (%i)\n",
+ "IOperm_InstallDriver: Failed to open service (%i).\n",
error);
}
}
@@ -162,31 +164,37 @@
service_was_created = 1;
}
- if (!StartService(svc, 0, NULL))
+ // Start the service. If the service already existed, it might have
+ // already been running as well.
+
+ if (!StartServiceW(svc, 0, NULL))
{
error = GetLastError();
if (error != ERROR_SERVICE_ALREADY_RUNNING)
{
- fprintf(stderr, "IOperm_InstallDriver: Failed to start service (%i)\n",
+ fprintf(stderr, "IOperm_InstallDriver: Failed to start service (%i).\n",
error);
+
result = 0;
}
else
{
- printf("IOperm_InstallDriver: ioperm driver already running\n");
+ printf("IOperm_InstallDriver: ioperm driver already running.\n");
}
}
else
{
- printf("IOperm_InstallDriver: ioperm driver installed\n");
+ printf("IOperm_InstallDriver: ioperm driver installed.\n");
service_was_started = 1;
}
+ // If we failed to start the driver running, we need to clean up
+ // before finishing.
+
if (result == 0)
{
- CloseServiceHandle(svc);
- CloseServiceHandle(scm);
+ IOperm_UninstallDriver();
}
return result;
@@ -215,7 +223,7 @@
else
{
fprintf(stderr,
- "IOperm_UninstallDriver: Failed to stop service (%i)\n",
+ "IOperm_UninstallDriver: Failed to stop service (%i).\n",
error);
result = 0;
}
@@ -231,14 +239,14 @@
error = GetLastError();
fprintf(stderr,
- "IOperm_UninstallDriver: DeleteService failed (%i)\n",
+ "IOperm_UninstallDriver: DeleteService failed (%i).\n",
error);
result = 0;
}
- else
+ else if (service_was_started)
{
- printf("IOperm_InstallDriver: ioperm driver uninstalled.\n");
+ printf("IOperm_UnInstallDriver: ioperm driver uninstalled.\n");
}
}