home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d2345 / ZLPORTIO.ZIP / ddkint.pas next >
Pascal/Delphi Source File  |  2001-06-30  |  8KB  |  252 lines

  1. { -----------------------------------------------------------------------------}
  2. { Copyright 2000-2001, Zloba Alexander.  All Rights Reserved.                  }
  3. { This unit can be freely used and distributed in commercial and private       }
  4. { environments, provided this notice is not modified in any way.               }
  5. { -----------------------------------------------------------------------------}
  6. { Feel free to contact me if you have any questions, comments or suggestions at}
  7. {   zal@specosoft.com (Zloba Alexander)                                        }
  8. { You can always find the latest version of this unit at:                      }
  9. {   http://www.specosoft.com                                                   }
  10.  
  11. { -----------------------------------------------------------------------------}
  12. { Date last modified:  06/05/2001                                              }
  13. { -----------------------------------------------------------------------------}
  14. { Description:                                                                 }
  15. {   This unit include service function to work with NT drivers and some        }
  16. {    constant from ntddk.h                                                     }
  17. {------------------------------------------------------------------------------}
  18. { Revision History:                                                            }
  19. { 1.00:  + First public release                                                }
  20. {------------------------------------------------------------------------------}
  21.  
  22. unit ddkint;
  23.  
  24. interface
  25. uses windows,winsvc;
  26. function CTL_CODE(const DeviceType,Func,Method,Access:Cardinal):cardinal;
  27.  
  28. const
  29.  FILE_DEVICE_BEEP               = $00000001;
  30.  FILE_DEVICE_CD_ROM             = $00000002;
  31.  FILE_DEVICE_CD_ROM_FILE_SYSTEM = $00000003;
  32.  FILE_DEVICE_CONTROLLER         = $00000004;
  33.  FILE_DEVICE_DATALINK           = $00000005;
  34.  FILE_DEVICE_DFS                = $00000006;
  35.  FILE_DEVICE_DISK               = $00000007;
  36.  FILE_DEVICE_DISK_FILE_SYSTEM   = $00000008;
  37.  FILE_DEVICE_FILE_SYSTEM        = $00000009;
  38.  FILE_DEVICE_INPORT_PORT        = $0000000a;
  39.  FILE_DEVICE_KEYBOARD           = $0000000b;
  40.  FILE_DEVICE_MAILSLOT           = $0000000c;
  41.  FILE_DEVICE_MIDI_IN            = $0000000d;
  42.  FILE_DEVICE_MIDI_OUT           = $0000000e;
  43.  FILE_DEVICE_MOUSE              = $0000000f;
  44.  FILE_DEVICE_MULTI_UNC_PROVIDER = $00000010;
  45.  FILE_DEVICE_NAMED_PIPE         = $00000011;
  46.  FILE_DEVICE_NETWORK            = $00000012;
  47.  FILE_DEVICE_NETWORK_BROWSER    = $00000013;
  48.  FILE_DEVICE_NETWORK_FILE_SYSTEM= $00000014;
  49.  FILE_DEVICE_NULL               = $00000015;
  50.  FILE_DEVICE_PARALLEL_PORT      = $00000016;
  51.  FILE_DEVICE_PHYSICAL_NETCARD   = $00000017;
  52.  FILE_DEVICE_PRINTER            = $00000018;
  53.  FILE_DEVICE_SCANNER            = $00000019;
  54.  FILE_DEVICE_SERIAL_MOUSE_PORT  = $0000001a;
  55.  FILE_DEVICE_SERIAL_PORT        = $0000001b;
  56.  FILE_DEVICE_SCREEN             = $0000001c;
  57.  FILE_DEVICE_SOUND              = $0000001d;
  58.  FILE_DEVICE_STREAMS            = $0000001e;
  59.  FILE_DEVICE_TAPE               = $0000001f;
  60.  FILE_DEVICE_TAPE_FILE_SYSTEM   = $00000020;
  61.  FILE_DEVICE_TRANSPORT          = $00000021;
  62.  FILE_DEVICE_UNKNOWN            = $00000022;
  63.  FILE_DEVICE_VIDEO              = $00000023;
  64.  FILE_DEVICE_VIRTUAL_DISK       = $00000024;
  65.  FILE_DEVICE_WAVE_IN            = $00000025;
  66.  FILE_DEVICE_WAVE_OUT           = $00000026;
  67.  FILE_DEVICE_8042_PORT          = $00000027;
  68.  FILE_DEVICE_NETWORK_REDIRECTOR = $00000028;
  69.  FILE_DEVICE_BATTERY            = $00000029;
  70.  FILE_DEVICE_BUS_EXTENDER       = $0000002a;
  71.  FILE_DEVICE_MODEM              = $0000002b;
  72.  FILE_DEVICE_VDM                = $0000002c;
  73.  FILE_DEVICE_MASS_STORAGE       = $0000002d;
  74.  FILE_DEVICE_SMB                = $0000002e;
  75.  FILE_DEVICE_KS                 = $0000002f;
  76.  FILE_DEVICE_CHANGER            = $00000030;
  77.  FILE_DEVICE_SMARTCARD          = $00000031;
  78.  FILE_DEVICE_ACPI               = $00000032;
  79.  FILE_DEVICE_DVD                = $00000033;
  80.  FILE_DEVICE_FULLSCREEN_VIDEO   = $00000034;
  81.  FILE_DEVICE_DFS_FILE_SYSTEM    = $00000035;
  82.  FILE_DEVICE_DFS_VOLUME         = $00000036;
  83.  FILE_DEVICE_SERENUM            = $00000037;
  84.  FILE_DEVICE_TERMSRV            = $00000038;
  85.  FILE_DEVICE_KSEC               = $00000039;
  86.  
  87.  FILE_DEVICE_KRNLDRVR           = $80ff;
  88.  
  89.  METHOD_BUFFERED   =              0;
  90.  METHOD_IN_DIRECT  =              1;
  91.  METHOD_OUT_DIRECT =              2;
  92.  METHOD_NEITHER    =              3;
  93.  
  94.  FILE_ANY_ACCESS     =            0;
  95.  FILE_SPECIAL_ACCESS =   (FILE_ANY_ACCESS);
  96.  FILE_READ_ACCESS    =      ( $0001 );    // file & pipe
  97.  FILE_WRITE_ACCESS   =      ( $0002 );    // file & pipe
  98.  
  99. function driverstart(const name:pchar):integer;
  100. function driverstop(const name:pchar):integer;
  101.  
  102. // for this function must have Administrators or Power users rigths
  103. function driverinstall(const path,name:pchar):integer;
  104. function driverremove(const name:pchar):integer;
  105.  
  106.  
  107. // exlpanation function 
  108. function messagestring(const error:integer):string;
  109.  
  110. implementation
  111.  
  112. function CTL_CODE(const DeviceType,Func,Method,Access:Cardinal):cardinal;
  113. begin
  114.  Result := DeviceType shl 16 or Access shl 14 or Func shl 2 or Method;
  115. end;
  116.  
  117.  
  118. function driverinstall(const path,name:pchar):integer;
  119. var hService: SC_HANDLE;
  120.     hSCMan          : SC_HANDLE;
  121. begin
  122.  
  123.   Result := 0;
  124.  
  125.   hSCMan := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  126.   if hSCMan = 0 then  begin
  127.    result := getlasterror;
  128.    exit;
  129.   end;
  130.  
  131.   hService := 0;
  132.  
  133.   hService := CreateService(hSCMan, name,name,
  134.               SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START,
  135.               SERVICE_ERROR_NORMAL, path,
  136.               nil, nil, nil, nil, nil);
  137.   if (hService = 0) then begin
  138.    // fuck
  139.     result := getlasterror;
  140.     CloseServiceHandle(hSCMan);
  141.     hSCMan := 0;
  142.     exit;
  143.   end
  144.   else
  145.     CloseServiceHandle(hService);
  146.   CloseServiceHandle(hSCMan);
  147. end;
  148.  
  149. function driverstart(const name:pchar):integer;
  150. var
  151.   hService: SC_HANDLE;
  152.   hSCMan          : SC_HANDLE;
  153.   args:pchar;
  154. begin
  155.  
  156.   Result := 0;
  157.  
  158.   hSCMan := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
  159.   if hSCMan = 0 then  begin
  160.    result := getlasterror;
  161.    exit;
  162.   end;
  163.  
  164.   hService := 0;
  165.  
  166.   // get a handle to the service
  167.   hService := OpenService(hSCMan, name, SERVICE_START);
  168.   if hService <> 0 then Begin
  169.      // start the driver
  170.     args := nil;
  171.     if integer(StartService(hService, 0, args )) = 0 then
  172.       result := getlasterror;
  173.     CloseServiceHandle(hService);
  174.   end
  175.   else
  176.     result := getlasterror;
  177.   CloseServiceHandle(hSCMan);
  178. end;
  179.  
  180. function driverstop(const name:pchar):integer;
  181. Var
  182.   serviceStatus: TServiceStatus;
  183.   hService: SC_HANDLE;
  184.   hSCMan          : SC_HANDLE;
  185. begin
  186.  
  187.   Result := 0;
  188.  
  189.   hSCMan := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
  190.   if hSCMan = 0 then  begin
  191.    result := getlasterror;
  192.    exit;
  193.   end;
  194.  
  195.   hService := 0;
  196.  
  197.  
  198.   // get a handle to the service
  199.   hService := OpenService(hSCMan, Name, SERVICE_STOP);
  200.   if hService <> 0 then Begin
  201.      // start the driver
  202.     if integer(ControlService(hService, SERVICE_CONTROL_STOP, serviceStatus)) = 0 then
  203.       result := getlasterror;
  204.     CloseServiceHandle(hService);
  205.   end
  206.   else
  207.     result := getlasterror;
  208.   CloseServiceHandle(hSCMan);
  209.  
  210. end;
  211.  
  212. function driverremove(const name:pchar):integer;
  213. Var
  214.   serviceStatus: TServiceStatus;
  215.   hService: SC_HANDLE;
  216.   hSCMan          : SC_HANDLE;
  217. begin
  218.  
  219.   Result := 0;
  220.  
  221.   hSCMan := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  222.   if hSCMan = 0 then  begin
  223.    result := getlasterror;
  224.    exit;
  225.   end;
  226.  
  227.   hService := 0;
  228.  
  229.   // get a handle to the service
  230.   hService := OpenService(hSCMan, Name, SERVICE_ALL_ACCESS);
  231.   if hService <> 0 then Begin
  232.      // start the driver
  233.     if integer( DeleteService(hService)) = 0 then
  234.       result := getlasterror;
  235.     CloseServiceHandle(hService);
  236.   end
  237.   else
  238.     result := getlasterror;
  239.   CloseServiceHandle(hSCMan);
  240. end;
  241.  
  242. function messagestring(const error:integer):string;
  243. var p:pchar;
  244. begin
  245.    getmem(p,200);
  246.    formatmessage(FORMAT_MESSAGE_FROM_SYSTEM,nil,error,0,p,199,nil);
  247.    Result := p;
  248.    freemem(p,200);
  249. end;
  250.  
  251. end.
  252.