{$A+,B-,C-,E-,F-,G+,H+,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1}
{$APPTYPE CONSOLE}
program installdriver;

uses
  windows, winsvc;

var
  OSInfo      : TOSVersionInfo;
  ServiceStat : TServiceStatus;
  HService,
  HSCManager  : SC_Handle;
  StrLaenge   : Integer;
  DrvFullName : string;
  DummyParams : PChar;

begin
  OSInfo.dwOSVersionInfoSize := sizeof(OSInfo);
  if GetVersionEx(OSInfo) then
  begin
    if OSInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then
    begin
      HSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
      if HSCManager <> 0 then
      begin
        HService := OpenService(HSCManager, 'EXPRO', SERVICE_ALL_ACCESS);
        if HService <> 0 then
        begin
          ControlService(HService, SERVICE_CONTROL_STOP, ServiceStat);
          DeleteService(HService);
          CloseServiceHandle(HService);
        end;
        {......................................................................}
        SetLength(DrvFullName, 100);
        GetEnvironmentVariable('SYSTEMROOT', pchar(DrvFullName), 100);
        StrLaenge := pos(#0, DrvFullName);
        if StrLaenge > 1 then
        begin
          DrvFullName := copy(DrvFullName, 1, StrLaenge - 1) +
                         '\system32\drivers\expro.sys';
          {....................................................................}
          HService := CreateService(HSCManager,            // SCManager database
                                    'EXPRO',               // name of service
                                    'EXPRO-60/80',         // name to display
                                    SERVICE_ALL_ACCESS,    // desired access
                                    SERVICE_KERNEL_DRIVER, // service type
                                    SERVICE_AUTO_START,    // start type
                                    SERVICE_ERROR_NORMAL,  // error control type
                                    pchar(drvfullname),    // service's binary
                                    nil,                   // no load ordering group
                                    nil,                   // no tag identifier
                                    nil,                   // no dependencies
                                    nil,                   // LocalSystem account
                                    nil);                  // no password
          if HService <> 0 then
            CloseServiceHandle(HService);
          {....................................................................}
          HService := OpenService(HSCManager, 'EXPRO', SERVICE_ALL_ACCESS);
          if HService <> 0 then
          begin
            StartService(HService, 0, DummyParams);
            CloseServiceHandle(HService);
          end;
        end;
        CloseServiceHandle(HSCManager);
      end;
    end;
  end;
end.
