AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Treiber Handling

Ein Thema von nanix · begonnen am 24. Dez 2009 · letzter Beitrag vom 30. Dez 2009
Antwort Antwort
Seite 1 von 3  1 23      
nanix
(Gast)

n/a Beiträge
 
#1

Treiber Handling

  Alt 24. Dez 2009, 00:43
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.

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.
Angehängte Dateien
Dateityp: h driver_139.h (630 Bytes, 6x aufgerufen)
Dateityp: cpp driver_168.cpp (7,6 KB, 7x aufgerufen)
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.184 Beiträge
 
Delphi 12 Athens
 
#2

Re: Treiber Handling

  Alt 24. Dez 2009, 09:22
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
$2B or not $2B
  Mit Zitat antworten Zitat
Benutzerbild von wicht
wicht

Registriert seit: 15. Jan 2006
Ort: Das schöne Enger nahe Bielefeld
809 Beiträge
 
Delphi XE Professional
 
#3

Re: Treiber Handling

  Alt 24. Dez 2009, 10:33
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.
http://streamwriter.org

"I make hits. Not the public. I tell the DJ’s what to play. Understand?"
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#4

Re: Treiber Handling

  Alt 24. Dez 2009, 11:14
Who said this is an driver?This is a unit to install and uninstall the driver

How would this look in delphi?

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

Function WritePCIConfig(.......
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.184 Beiträge
 
Delphi 12 Athens
 
#5

Re: Treiber Handling

  Alt 24. Dez 2009, 11:32
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.
$2B or not $2B
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#6

Re: Treiber Handling

  Alt 24. Dez 2009, 11:37
Is there a tool to make this a bit easier?

It should be function
Delphi-Quellcode:
WritePciConfig(lpInBuffer: Pointer; nInBufferSize: LongWord; lpOutBuffer: Pointer;
  nOutBufferSize: LongWord; lpBytesReturned: PLongWord): NTSTATUS; StdCall;
  Mit Zitat antworten Zitat
Benutzerbild von JamesTKirk
JamesTKirk

Registriert seit: 9. Sep 2004
Ort: München
604 Beiträge
 
FreePascal / Lazarus
 
#7

Re: Treiber Handling

  Alt 24. Dez 2009, 21:07
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.
Sven
[Free Pascal Compiler Entwickler]
this post is printed on 100% recycled electrons
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#8

Re: Treiber Handling

  Alt 24. Dez 2009, 21:17
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
  Mit Zitat antworten Zitat
Benutzerbild von JamesTKirk
JamesTKirk

Registriert seit: 9. Sep 2004
Ort: München
604 Beiträge
 
FreePascal / Lazarus
 
#9

Re: Treiber Handling

  Alt 24. Dez 2009, 21:29
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 (...)"

Zitat von nanix:
I am nearly done translating it then ill just need to modify it a bit for FPC compiler
The converted code can also be used with Delphi. You may remember this tool in the future, because it's really helpful

Regards,
Sven
Sven
[Free Pascal Compiler Entwickler]
this post is printed on 100% recycled electrons
  Mit Zitat antworten Zitat
nanix
(Gast)

n/a Beiträge
 
#10

Re: Treiber Handling

  Alt 24. Dez 2009, 22:01
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.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 3  1 23      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:01 Uhr.
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