📄 loaddrv.cpp
字号:
/*
loaddrv.c - Dynamic driver install/start/stop/remove
Ho modificato l'originale in poche parti.
*/
#include "stdafx.h"
#include "loaddrv.h"
#include <winsvc.h>
GiveIOWrapper::GiveIOWrapper()
{
// apre il service control manager
hSCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
bDriverInstalled = FALSE;
bOK = FALSE;
GiveIO();
}
GiveIOWrapper::~GiveIOWrapper()
{
if( bDriverInstalled)
GiveIO();
if( hSCMan != NULL)
CloseServiceHandle( hSCMan);
}
GiveIOWrapper::GiveIO()
{
char szPath[1024];
char szDriver[] = "giveio";
OSVERSIONINFO ovi;
HANDLE h;
bOK = FALSE;
// trova SO attuale
ovi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO);
::GetVersionEx( &ovi);
if( ovi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
bOK = TRUE;
return 1;
}
else if( ovi.dwPlatformId != VER_PLATFORM_WIN32_NT)
return 0;
// costruisce il path del driver
CString path;
CString cmdline = ::GetCommandLine();
cmdline.TrimLeft();
cmdline.TrimRight();
if( cmdline[0] == '\"')
{
CString pippo = cmdline.Mid(1);
int idx = pippo.Find('\"');
if( idx == -1)
path = pippo;
else
path = pippo.Left( idx);
}
else
{
int idx = cmdline.Find( ' ');
if( idx == -1)
path = cmdline;
else
path = cmdline.Left( idx);
}
/* LPTSTR cmdline = ::GetCommandLine();
if( cmdline[0] == '\"')
cmdline++;
char szDrive[16], szDir[512];
_splitpath( cmdline, szDrive, szDir, NULL, NULL);*/
char szDir[512], szDrive[16];
_splitpath( LPCTSTR(path), szDrive, szDir, NULL, NULL);
_makepath( szPath, szDrive, szDir, szDriver, "sys");
/*
AfxGetApp()->m_pMainWnd->MessageBox( cmdline, NULL);
AfxGetApp()->m_pMainWnd->MessageBox( szPath, NULL);
*/
if( hSCMan == NULL)
{
if( (hSCMan = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
return 0;
}
// Disinstalla drivers precedenti
DriverStop( szDriver);
DriverRemove( szDriver);
if( !bDriverInstalled)
{
// installa il driver
if( DriverInstall( szPath, szDriver) != ERROR_SUCCESS)
return 0;
// carica il driver
if( DriverStart( szDriver) != ERROR_SUCCESS)
{
DriverRemove( szDriver);
return 0;
}
// OK, accedi all'hardware in liberta'!
if( (h = CreateFile("\\\\.\\giveio", GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
== INVALID_HANDLE_VALUE)
{
DriverStop( szDriver);
DriverRemove( szDriver);
return 0;
}
CloseHandle( h);
bDriverInstalled = TRUE;
}
else
{
bDriverInstalled = FALSE;
}
bOK = TRUE;
return 1;
}
/*
DriverInstall
Installa il driver
*/
DWORD GiveIOWrapper::DriverInstall(LPSTR lpPath, LPSTR lpDriver)
{
BOOL dwStatus = 0;
HANDLE hService = NULL;
// add to service control manager's database
if ((hService = CreateService(hSCMan, lpDriver,
lpDriver, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, lpPath,
NULL, NULL, NULL, NULL, NULL)) == NULL)
dwStatus = GetLastError();
else CloseServiceHandle(hService);
// aggiunta di Captain Crunch
if( dwStatus == ERROR_SERVICE_EXISTS)
dwStatus = 0;
return dwStatus;
} // DriverInstall
/*
DriverStart
Fa partire il driver
*/
DWORD GiveIOWrapper::DriverStart(LPSTR lpDriver)
{
BOOL dwStatus = 0;
HANDLE hService = NULL;
// get a handle to the service
if ((hService = OpenService(hSCMan, lpDriver,
SERVICE_ALL_ACCESS)) != NULL)
{
// start the driver
if (!StartService(hService, 0, NULL))
dwStatus = GetLastError();
} else dwStatus = GetLastError();
if (hService != NULL) CloseServiceHandle(hService);
return dwStatus;
} // DriverStart
/*
DriverStop
Stoppa il driver
*/
DWORD GiveIOWrapper::DriverStop(LPSTR lpDriver)
{
BOOL dwStatus = 0;
HANDLE hService = NULL;
SERVICE_STATUS serviceStatus;
// get a handle to the service
if ((hService = OpenService(hSCMan, lpDriver,
SERVICE_ALL_ACCESS)) != NULL)
{
// stop the driver
if (!ControlService(hService, SERVICE_CONTROL_STOP,
&serviceStatus))
dwStatus = GetLastError();
} else dwStatus = GetLastError();
if (hService != NULL) CloseServiceHandle(hService);
return dwStatus;
} // DriverStop
/*
DriverRemove
Rimuove il driver
*/
DWORD GiveIOWrapper::DriverRemove(LPSTR lpDriver)
{
BOOL dwStatus = 0;
HANDLE hService = NULL;
// get a handle to the service
if ((hService = OpenService(hSCMan, lpDriver,
SERVICE_ALL_ACCESS)) != NULL)
{
// remove the driver
if (!DeleteService(hService))
dwStatus = GetLastError();
} else dwStatus = GetLastError();
if (hService != NULL) CloseServiceHandle(hService);
return dwStatus;
} // DriverRemove
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -