Delphi-PRAXiS
Seite 1 von 3  1 23      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Treiber Handling (https://www.delphipraxis.net/145107-treiber-handling.html)

nanix 23. Dez 2009 23:43


Treiber Handling
 
Liste der Anhänge anzeigen (Anzahl: 2)
Ok i translated this from opensyslib which is an open source driver.I just cant figure out where the bug is becouse it wont do nothing. :oops:

I just can't find the bug.. :?

Delphi-Quellcode:
unit DriverFuncs;

interface

uses winsvc, Classes, SysUtils, Variants, Windows, driverConSTS;

function InstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath: string):
  Boolean;
function RemoveDriver(hSCManager: SC_HANDLE; DriverId: string): Boolean;
function StartDriver(hSCManager: SC_HANDLE; DriverId: string): Boolean;
function StopDriver(hSCManager: SC_HANDLE; DriverId: string): Boolean;
function SystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
  string): Boolean;
function IsSystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
  string): Boolean;
function ManageDriver(DriverId, DriverPath: string; _Function: Integer):
  Boolean;

function OpenDriver():Boolean;

implementation

var
 gHandle:THANDLE;

//-----------------------------------------------------------------------------
//
// Open Driver
//
//-----------------------------------------------------------------------------

function OpenDriver():boolean;
begin
   gHandle:= CreateFile(Driver_DRIVER_ID,
        GENERIC_READ or GENERIC_WRITE,
        0,
        nil,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0
        );


    if gHandle = INVALID_HANDLE_VALUE then
    begin
    result:=FALSE;
    end
    else
   result:=TRUE;
end;

//-----------------------------------------------------------------------------
//
// Manage Driver
//
//-----------------------------------------------------------------------------

function ManageDriver(DriverId, DriverPath: string; _Function: Integer):
  Boolean;
  var
  hSCManager: SC_HANDLE;
  rCode: Boolean;
  error: Dword;
begin

  hSCManager := 0;
  rCode := False;
  error := NO_ERROR;

  if (DriverId = '') and (DriverPath = '') then
  begin
    result := FALSE;
  end;

  hSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);

  if hSCManager = 0 then
  begin
    result := FALSE;
  end;



  if _Function=OLS_DRIVER_INSTALL then begin
        if InstallDriver(hSCManager, DriverId, DriverPath) then
        rCode := rCode = StartDriver(hSCManager, DriverId);
  end;

  if _Function=OLS_DRIVER_REMOVE then begin
      if IsSystemInstallDriver(hSCManager, DriverId,
      DriverPath)=false then
      begin
        StopDriver(hSCManager, DriverId);
        rCode := RemoveDriver(hSCManager, DriverId);
      end;
  end;


  if _Function=OLS_DRIVER_SYSTEM_INSTALL then begin
      if IsSystemInstallDriver(hSCManager, DriverId, DriverPath) then
      rCode := True

      else
      begin

        if OpenDriver()=false then
        begin
          StopDriver(hSCManager, DriverId);
          RemoveDriver(hSCManager, DriverId);

          if InstallDriver(hSCManager, DriverId, DriverPath) then
          begin
            StartDriver(hSCManager, DriverId);
          end;
          OpenDriver();
         end;

        rCode := SystemInstallDriver(hSCManager, DriverId, DriverPath);
      end;
   end;


   if _Function=OLS_DRIVER_SYSTEM_UNINSTALL then begin

    if IsSystemInstallDriver(hSCManager, DriverId, DriverPath)=false then
       rCode:=True
       else begin
          if not gHandle=INVALID_HANDLE_VALUE then begin
          CloseHandle(gHandle);
          gHandle:=INVALID_HANDLE_VALUE;
          end;

          if StopDriver(hSCManager, DriverId) then begin
          rCode:=RemoveDriver(hSCManager,DriverId);
          end;

          if not hSCManager=0 then begin
          CloseServiceHandle(hSCManager);
          end;

          result:=rCode;

       end;
     end;
   end;

//-----------------------------------------------------------------------------
//
// Install Driver
//
//-----------------------------------------------------------------------------

function InstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath: string):Boolean;
var
  hService: SC_HANDLE;
  rCode: Boolean;
  error: Dword;
begin
  hService := 0;
  rCode := False;
  error := NO_ERROR;

   hService:= CreateService(hSCManager,
                     pchar(DriverId),
                     pchar(DriverId),
                     SERVICE_ALL_ACCESS,
                     SERVICE_KERNEL_DRIVER,
                     SERVICE_DEMAND_START,
                     SERVICE_ERROR_NORMAL,
                     pchar(DriverPath),
                     Nil,
                     Nil,
                     Nil,
                     Nil,
                     Nil);


    if hService= 0 then
    error:= GetLastError();

  if error=ERROR_SERVICE_EXISTS then
     rCode:= TRUE
   else
        rCode:= TRUE;
      CloseServiceHandle(hService);
      result:=rCode;
end;

//-----------------------------------------------------------------------------
//
// System Install Driver
//
//-----------------------------------------------------------------------------

function SystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
  string): Boolean;
var
  hService:SC_HANDLE;
   rCode:boolean;
begin
  hService:=0;
  rCode:=False;

   hService:=OpenService(hSCManager, pchar(DriverId), SERVICE_ALL_ACCESS);

   if not hService= 0 then begin
      rCode:= ChangeServiceConfig(hService,
                     SERVICE_KERNEL_DRIVER,
                     SERVICE_AUTO_START,
                     SERVICE_ERROR_NORMAL,
                     pchar(DriverPath),
                     Nil,
                     Nil,
                     Nil,
                     Nil,
                     Nil,
                     Nil
                     );
      CloseServiceHandle(hService);
  end;

   result:=rCode;
end;

//-----------------------------------------------------------------------------
//
// Remove Driver
//
//-----------------------------------------------------------------------------

function RemoveDriver(hSCManager: SC_HANDLE; DriverId: string): Boolean;
var
 hService:SC_HANDLE;
 rCode:Boolean;
begin
hService:=0;
rCode:=False;

hService:= OpenService(hSCManager,pchar(DriverId), SERVICE_ALL_ACCESS);

    if hService=0 then begin
       rCode:= TRUE
       end
   else begin
      rCode := DeleteService(hService);
      CloseServiceHandle(hService);
   end;

   result:=rCode;
end;

//-----------------------------------------------------------------------------
//
// Start Driver
//
//-----------------------------------------------------------------------------

function StartDriver(hSCManager: SC_HANDLE; DriverId: string): Boolean;
var
  hService:SC_HANDLE;
  rCode:Boolean;
  error:DWORD;
  b:pchar;
begin
  b:=nil;
  hService:=0;
  rCode:=False;
  error:=NO_ERROR;

   hService:= OpenService(hSCManager, pchar(DriverId), SERVICE_ALL_ACCESS);

    if not hService=0 then begin
          if StartService(hService, 0, b) = false then
             error:= GetLastError();
             if error=ERROR_SERVICE_ALREADY_RUNNING then
                  rCode:=True
             else begin
             rCode:=True;
             CloseServiceHandle(hService);
             end;


  end;
 result:=rCode;
end;

//-----------------------------------------------------------------------------
//
// Stop Driver
//
//-----------------------------------------------------------------------------

function StopDriver(hSCManager: SC_HANDLE; DriverId: string): Boolean;
var
  hService:SC_HANDLE;
  rCode:Boolean;
  error:DWORD;
  serviceStatus:_SERVICE_STATUS;
begin
  hService:=0;
  rCode:=False;
  error:=NO_ERROR;

   hService:= OpenService(hSCManager, pchar(DriverId), SERVICE_ALL_ACCESS);

    if not hService=0 then begin
      rCode:= ControlService(hService, SERVICE_CONTROL_STOP,serviceStatus);
      error:= GetLastError();
    CloseServiceHandle(hService);
    end;
    result:=rCode;
end;

//-----------------------------------------------------------------------------
//
// IsSystemInstallDriver
//
//-----------------------------------------------------------------------------

function IsSystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
  string): Boolean;
var
  ServiceConfig: PQueryServiceConfig;
  hService : SC_HANDLE;
  Ss   : TServiceStatus;
  dw_size:dword;
  MemToFree: integer;

begin
  result:=true;
  exit;


      hService := OpenService(hSCManager, PChar(DriverId), SERVICE_ALL_ACCESS);

      if not hService = 0 then
      begin
      Exit;
      end;

      try
        QueryServiceConfig(hService, nil, 0, dw_size); // Get Buffer Length
        GetMem(ServiceConfig, dw_size + 1);
        MemToFree := dw_size + 1;
        try
          if not QueryServiceConfig(hService, ServiceConfig, dw_size + 1, dw_size) then // Get Buffer Length
          begin
            Exit;
          end;
       
        finally
        FreeMem(ServiceConfig);
        end;

        if ServiceConfig.dwServiceType=SERVICE_AUTO_START then
        result:=True;

      finally
        CloseServiceHandle(hService);
        CloseServiceHandle(hSCManager);
    end;
  end;


  end.

himitsu 24. Dez 2009 08:22

Re: Treiber Handling
 
Classes, SysUtils, Variants and the DelphiMemoryManager (String) do not belong in a driver.

Only use low level funktions (kernel mode) and no fuctions from user mode.
And the original System.dcu is also "highly" developed. That lends itself seems to be a limited version.

http://www.google.de/search?q=%22Treiber+in+Delphi%22

wicht 24. Dez 2009 09:33

Re: Treiber Handling
 
The source posted here does not seem to belong to a driver.
It looks more like an application the user runs to install/start/stop/remove a driver.

nanix 24. Dez 2009 10:14

Re: Treiber Handling
 
Who said this is an driver?This is a unit to install and uninstall the driver :zwinker:

How would this look in delphi?

Zitat:

NTSTATUS WritePciConfig(
void *lpInBuffer,
ULONG nInBufferSize,
void *lpOutBuffer,
ULONG nOutBufferSize,
ULONG *lpBytesReturned
);

Delphi-Quellcode:
Function WritePCIConfig(.......

himitsu 24. Dez 2009 10:32

Re: Treiber Handling
 
probably
Delphi-Quellcode:
function WritePciConfig(lpInBuffer: Pointer; nInBufferSize: LongWord; lpOutBuffer: Pointer;
  nOutBufferSize: LongWord; lpBytesReturned: PLongWord): LongWord; StdCall;

// or

function WritePciConfig(lpInBuffer: Pointer; nInBufferSize: LongWord; lpOutBuffer: Pointer;
  nOutBufferSize: LongWord; var lpBytesReturned: LongWord): LongWord; StdCall;
Here there is nothing difficult, and it should all easily translate itself.

nanix 24. Dez 2009 10:37

Re: Treiber Handling
 
Is there a tool to make this a bit easier? :stupid:

It should be function
Delphi-Quellcode:
WritePciConfig(lpInBuffer: Pointer; nInBufferSize: LongWord; lpOutBuffer: Pointer;
  nOutBufferSize: LongWord; lpBytesReturned: PLongWord): NTSTATUS; StdCall;

JamesTKirk 24. Dez 2009 20:07

Re: Treiber Handling
 
Try h2pas which is included with Free Pascal. It allows you to (mostly) convert C declarations to Pascal ones. Don't forget to study its options. I may not translate everything you feed it with, but it's rather helpful.

Regards,
Sven

Edit: Where did you find this OpenSysLib? I'm curious. :mrgreen:

nanix 24. Dez 2009 20:17

Re: Treiber Handling
 
Contact me and ill tell you everything you need to know aswell which functions you should translate. :)

I am nearly done translating it then ill just need to modify it a bit for FPC compiler :-D

JamesTKirk 24. Dez 2009 20:29

Re: Treiber Handling
 
Zitat:

Zitat von nanix
Contact me and ill tell you everything you need to know aswell which functions you should translate. :)

I meant "It may not translate everything (...)" :oops:

Zitat:

Zitat von nanix
I am nearly done translating it then ill just need to modify it a bit for FPC compiler :-D

The converted code can also be used with Delphi. You may remember this tool in the future, because it's really helpful ;)

Regards,
Sven

nanix 24. Dez 2009 21:01

Re: Treiber Handling
 
I mean the system functions like WRMSR/RDMSR CPUID functions and similar.Don't go too crazy on it though.

ntddk.cpp is like 400kb translating all of it would be craaazyyy.But would be perfect and mad at the same time. :gruebel:


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:51 Uhr.
Seite 1 von 3  1 23      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz