Delphi load driver code demonstration

Source: Internet
Author: User

On the Internet, you can see a source code that can load the driver... ...

 

 

Code
**************************************** *******************************}
{Module name: mydriver}
{Module function: load/uninstall driver}
{Version No.: v1.0.0}
{Date}
{*************************************** ********************************}
Unit mydriver;

Interface

Uses
Windows, sysutils, tlhelp32, winsvc;
{Function: load the driver
Parameter: szthedrivername: Path of the driver.
Szsvrname: Driver name .}
Function installdriver (szthedrivername, szsvrname: string): Boolean;
{Function: uninstall the driver.
Parameter: szsvrname: Driver name .}
Function uninstalldriver (szsvrname: string): Boolean;

Implementation

Function installdriver (szthedrivername, szsvrname: string): Boolean;
VaR
Hservicemgr, hservicetwdm: SC _handle;
Szdir: array [0 .. 1023] of char;
Lpszthedrivername, P: pchar;
Begin
Zeromemory (@ szdir, 1024 );
Strcopy (szdir, pchar (szthedrivername ));
Lpszthedrivername: = @ szdir;
{Open service control manager}
Hservicemgr: = openscmanager (nil, nil, SC _manager_all_access );

If hservicemgr = 0 then
Begin
{Openscmanager () faild .}
Result: = false;
Exit;
End;

Hservicetwdm: = createservice (hservicemgr,
Pchar (szsvrname), {name of the System \ CurrentControlSet \ Services driver in the Registry}
Pchar (szsvrname), {displayname value of the registry driver}
Service_all_access, {permission to load the driver}
Service_kernel_driver, {indicates that the loaded service is a driver}
Service_demand_start, {start value of the registry driver}
Service_error_ignore, {errorcontrol value of the registry driver}
Lpszthedrivername, {registry driver's ImagePath value}
Nil, nil, nil );

If hservicetwdm = 0 then
Begin
If getlasterror () = error_service_exists then
Begin
{Service exists}
Hservicetwdm: = openservice (hservicemgr, pchar (szsvrname), service_all_access );
If hservicetwdm = 0 then
Begin
Closeservicehandle (hservicemgr );
Result: = false;
Exit;
End;
End
Else
Begin
Closeservicehandle (hservicemgr );
Result: = false;
Exit;
End;
End;

{Start the drivers}
If hservicetwdm <> 0 then
Begin
If startservice (hservicetwdm, 0, P) = false then
Begin
If error_service_already_running = getlasterror () then
Begin
{No real problem}
End
Else
Begin
Closeservicehandle (hservicemgr );
Closeservicehandle (hservicetwdm );
Result: = false;
Exit;
End;
End;

Closeservicehandle (hservicemgr );
Closeservicehandle (hservicetwdm );
End;

Result: = true;
End;

Function uninstalldriver (szsvrname: string): Boolean;
VaR
Hservicemgr, hservicetwdm: SC _handle;
Svrsta: service_status;
Begin
Hservicemgr: = openscmanager (nil, nil, SC _manager_all_access );
If hservicemgr = 0 then
Begin
{Openscmanager () faild .}
Result: = false;
Exit;
End;

Hservicetwdm: = openservice (hservicemgr, pchar (szsvrname), service_all_access );
If hservicetwdm = 0 then
Begin
{Openservice () faild .}
Closeservicehandle (hservicemgr );
Result: = false;
Exit;
End;

{Stop the driver. If the stop fails, the driver can be dynamically loaded only after it is restarted .}
If controlservice (hservicetwdm, service_control_stop, svrsta) = false then
Begin
{Controlservice () faild .}
Closeservicehandle (hservicetwdm );
Closeservicehandle (hservicemgr );
Result: = false;
Exit;
End;
{Dynamically uninstall the driver .}
If deleteservice (hservicetwdm) = false then
Begin
{Deletesrevice () faild .}
Closeservicehandle (hservicetwdm );
Closeservicehandle (hservicemgr );
Result: = false;
Exit;
End;

Closeservicehandle (hservicetwdm );
Closeservicehandle (hservicemgr );
Result: = true;
End;

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.