Main Page | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

service.c

Go to the documentation of this file.
00001 /*
00002  *  This program is free software; you can redistribute it and/or
00003  *  modify it under the terms of the GNU General Public License
00004  *  as published by the Free Software Foundation; either version
00005  *  2 of the License, or (at your option) any later version.
00006  *
00007  *  Copyright 2004 Spiro Trikaliotis
00008  *
00009  */
00010 
00021 #include <windows.h>
00022 #include <stdio.h>
00023 #include "cbmioctl.h"
00024 
00025 #include "instcbm.h"
00026 
00027 #include "i_opencbm.h"
00028 
00030 #define DBG_USERMODE
00031 
00033 #define DBG_PROGNAME "INSTCBM.EXE"
00034 
00035 #include "debug.h"
00036 
00053 VOID
00054 RegSetDWORD(HKEY RegKey, char *SubKey, DWORD Value)
00055 {
00056     DWORD rc;
00057 
00058     FUNC_ENTER();
00059 
00060     DBG_PRINT((DBG_PREFIX "Setting %s to %u", SubKey, Value));
00061 
00062     rc = RegSetValueEx(RegKey, SubKey, 0, REG_DWORD, (LPBYTE)&Value, 4);
00063 
00064     DBG_ASSERT(rc == ERROR_SUCCESS);
00065 
00066     FUNC_LEAVE();
00067 }
00068 
00085 VOID
00086 RegSetEXPANDSZ(HKEY RegKey, char *SubKey, IN LPCTSTR Value)
00087 {
00088     DWORD rc;
00089 
00090     FUNC_ENTER();
00091 
00092     rc = RegSetValueEx(RegKey, SubKey, 0, REG_EXPAND_SZ, Value, strlen(Value)+1);
00093 
00094     DBG_ASSERT(rc == ERROR_SUCCESS);
00095 
00096     FUNC_LEAVE();
00097 }
00098 
00099 
00112 BOOL
00113 CreateLogRegistryKeys(IN LPCTSTR ServiceExe)
00114 {
00115     DWORD dwDisposition;
00116     HKEY RegKey;
00117 
00118     FUNC_ENTER();
00119 
00120     // Open a registry key to HKLM<%REGKEY_EVENTLOG%>
00121 
00122     if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,                         
00123                        REGKEY_EVENTLOG,
00124                        0,
00125                        NULL,
00126                        REG_OPTION_NON_VOLATILE,
00127                        KEY_SET_VALUE,
00128                        NULL,
00129                        &RegKey,
00130                        &dwDisposition
00131                       )
00132        )
00133     {         
00134         FUNC_LEAVE_BOOL(FALSE);
00135     }  
00136 
00137     // Store the path to the file which contains the event service entries
00138 
00139     RegSetEXPANDSZ(RegKey, "EventMessageFile", ServiceExe);
00140 
00141     // The written "FACILITY" is 0x07 (FACILITY_PARALLEL_ERROR_CODE)
00142 
00143     RegSetDWORD(RegKey, "TypesSupported", 0x07);
00144 
00145     // We're done, close the registry handle.
00146 
00147     RegCloseKey(RegKey);
00148 
00149     FUNC_LEAVE_BOOL(TRUE);
00150 }
00151 
00183 static BOOL
00184 CreateDefaultRegistryKeys(IN ULONG DefaultLpt,
00185                           IN ULONG IecCableType,
00186                           IN ULONG PermanentlyLock,
00187                           IN BOOL DebugFlagsDriverPresent, IN ULONG DebugFlagsDriver,
00188                           IN BOOL DebugFlagsDllPresent, IN ULONG DebugFlagsDll)
00189 {
00190     BOOLEAN success;
00191     HKEY RegKey;
00192 
00193     FUNC_ENTER();
00194 
00195     success = FALSE;
00196 
00197     // Open a registry key to HKLM<%CBM_REGKEY_SERVICE%>
00198 
00199     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
00200                      CBM_REGKEY_SERVICE,
00201                      0,
00202                      KEY_ALL_ACCESS,
00203                      &RegKey) 
00204         != ERROR_SUCCESS)
00205 
00206     {
00207         DWORD error = GetLastError();
00208 
00209         DBG_WARN((DBG_PREFIX "COULD NOT OPEN HKLM\\" CBM_REGKEY_SERVICE " (0x%x) '%s'",
00210             error, FormatErrorMessage(error)));
00211         printf("WARNING: COULD NOT OPEN HKLM\\" CBM_REGKEY_SERVICE " (0x%x) '%s'\n",
00212             error, FormatErrorMessage(error));
00213     }
00214     else
00215     {
00216         // Set the value for DefaultLpt
00217 
00218         if (DefaultLpt != -1)
00219         {
00220             RegSetDWORD(RegKey, CBM_REGKEY_SERVICE_DEFAULTLPT, DefaultLpt);
00221         }
00222 
00223         if (IecCableType != -2)
00224         {
00225             RegSetDWORD(RegKey, CBM_REGKEY_SERVICE_IECCABLE, IecCableType);
00226         }
00227 
00228         if (PermanentlyLock != -1)
00229         {
00230             RegSetDWORD(RegKey, CBM_REGKEY_SERVICE_PERMLOCK, PermanentlyLock);
00231         }
00232 
00233 #if DBG
00234 
00235         // Set the value for the DebugFlags
00236 
00237         if (DebugFlagsDriverPresent)
00238         {
00239             RegSetDWORD(RegKey, CBM_REGKEY_SERVICE_DEBUGFLAGS, DebugFlagsDriver);
00240         }
00241 
00242         if (DebugFlagsDllPresent)
00243         {
00244             RegSetDWORD(RegKey, CBM_REGKEY_SERVICE_DLL_DEBUGFLAGS, DebugFlagsDll);
00245         }
00246 
00247 #endif // #if DBG
00248 
00249         // We're done, close the registry handle.
00250 
00251         RegCloseKey(RegKey);
00252 
00253         success = TRUE;
00254     }
00255 
00256     FUNC_LEAVE_BOOL(success);
00257 }
00258 
00259 
00280 BOOL
00281 CbmInstall(IN LPCTSTR DriverName, IN LPCTSTR ServiceExe, IN BOOL AutomaticStart)
00282 {
00283     SC_HANDLE scManager;
00284     SC_HANDLE scService;
00285     DWORD error;
00286     BOOL success;
00287 
00288     FUNC_ENTER();
00289 
00290     success = TRUE;
00291 
00292     scManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
00293 
00294     if (scManager)
00295     {
00296         // Create the service
00297 
00298         scService = CreateService(scManager, DriverName, DriverName, 
00299            SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, 
00300            AutomaticStart ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
00301            SERVICE_ERROR_NORMAL, ServiceExe, 
00302            "Extended base", NULL, "+Parallel arbitrator\0Parport\0",
00303            NULL, NULL);
00304 
00305         if (scService == NULL)
00306         {
00307             error = GetLastError();
00308 
00309             if (error == ERROR_SERVICE_EXISTS)
00310             {
00311                 //  This is not an error, so process it different from the others.
00312 
00313                 DBG_WARN((DBG_PREFIX "CreateService (0x%02x) '%s'",
00314                     error, FormatErrorMessage(error)));
00315                 printf("WARNING: opencbm is already installed!\n");
00316             }
00317             else
00318             {
00319                 DBG_ERROR((DBG_PREFIX "CreateService (0x%02x) '%s'",
00320                     error, FormatErrorMessage(error)));
00321                 printf("ERROR: CreateService (0x%02x) '%s'\n",
00322                     error, FormatErrorMessage(error));
00323 
00324                 success = FALSE;
00325             }
00326         }
00327         else
00328         {
00329             DBG_SUCCESS((DBG_PREFIX "CreateService"));
00330             CloseServiceHandle(scService);
00331         }
00332 
00333         // Create the registry setting for the default LPT port
00334 
00335         CreateDefaultRegistryKeys(-1, -2, -1, FALSE, 0, FALSE, 0);
00336 
00337         CloseServiceHandle(scManager);
00338 
00339         // Create the registry settings for being able to output to the
00340         // event service
00341 
00342         success = CreateLogRegistryKeys(ServiceExe);
00343 
00344         // If the driver is to be started automatically, start it now
00345 
00346         if (AutomaticStart)
00347         {
00348             cbm_i_driver_start();
00349         }
00350     }
00351     else
00352     {
00353         success = FALSE;
00354     }
00355 
00356     FUNC_LEAVE_BOOL(success);
00357 }
00358 
00359 
00390 BOOL
00391 CbmUpdateParameter(IN ULONG DefaultLpt,
00392                    IN ULONG IecCableType,
00393                    IN ULONG PermanentlyLock,
00394                    IN BOOL DebugFlagsDriverPresent, IN ULONG DebugFlagsDriver,
00395                    IN BOOL DebugFlagsDllPresent, IN ULONG DebugFlagsDll)
00396 {
00397     BOOL ret;
00398 
00399     FUNC_ENTER();
00400 
00401     ret = CreateDefaultRegistryKeys(DefaultLpt, IecCableType, PermanentlyLock,
00402                 DebugFlagsDriverPresent, DebugFlagsDriver,
00403                 DebugFlagsDllPresent, DebugFlagsDll);
00404 
00405     CbmInstallUpdate();
00406 
00407     FUNC_LEAVE_BOOL(ret);
00408 }
00409 
00410 
00423 BOOL
00424 CbmRemove(IN LPCTSTR DriverName)
00425 {
00426     SC_HANDLE scManager;
00427     SC_HANDLE scService;
00428     BOOL ret;
00429 
00430     FUNC_ENTER();
00431 
00432     // Make sure the driver is stopped before being unloaded
00433 
00434     cbm_i_driver_stop();
00435 
00436     scManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
00437 
00438     if (scManager)
00439     {
00440         scService = OpenService(scManager, DriverName, SERVICE_ALL_ACCESS);
00441 
00442         if (scService == NULL)
00443         {
00444             DWORD error = GetLastError();
00445 
00446             DBG_ERROR((DBG_PREFIX "OpenService (0x%02x) '%s'", error, FormatErrorMessage(error)));
00447             printf("ERROR: OpenService (0x%02x) '%s'\n", error, FormatErrorMessage(error));
00448             FUNC_LEAVE_BOOL(FALSE);
00449         }
00450 
00451         ret = DeleteService(scService);
00452 
00453         if (ret)
00454         {
00455             DBG_SUCCESS((DBG_PREFIX "DeleteService"));
00456             printf("DeleteService SUCCESS\n");
00457         }
00458         else
00459         {
00460             DWORD error = GetLastError();
00461 
00462             DBG_ERROR((DBG_PREFIX "DeleteService (0x%02x) '%s'", error, FormatErrorMessage(error)));
00463             printf("ERROR: DeleteService (0x%02x) '%s'\n", error, FormatErrorMessage(error));
00464         }
00465 
00466         CloseServiceHandle(scService);
00467 
00468 
00469         // Remove the registry settings which were created for the event service
00470 
00471         RegDeleteKey(HKEY_LOCAL_MACHINE, REGKEY_EVENTLOG);
00472 
00473         CloseServiceHandle(scManager);
00474     }
00475     else
00476     {
00477         ret = FALSE;
00478     }
00479 
00480     FUNC_LEAVE_BOOL(ret);
00481 }
00482 
00483 
00496 BOOL
00497 CbmCheckPresence(IN LPCTSTR DriverName)
00498 {
00499     SC_HANDLE scManager;
00500     SC_HANDLE scService;
00501 
00502     FUNC_ENTER();
00503 
00504     scService = NULL;
00505 
00506     scManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
00507 
00508     if (scManager)
00509     {
00510         scService = OpenService(scManager, DriverName, SERVICE_ALL_ACCESS);
00511 
00512         if (scService != NULL)
00513         {
00514             CloseServiceHandle(scService);
00515         }
00516 
00517         CloseServiceHandle(scManager);
00518     }
00519 
00520     FUNC_LEAVE_BOOL(scService ? TRUE : FALSE);
00521 }

Generated on Sun Apr 30 18:45:59 2006 for opencbm by  doxygen 1.4.2