大概的方法是:在HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\下建立你的设备的一个主键,名字和你的sys文件名相同(去掉扩展名)。在加上Type、start、ErrorControl、Group等键值。然后再把你的sys文件copy到system32目录下,重启机器系统会自动加载。或者编写一个安装程序写那几个注册表项,然后调用CreateService函数,这样不用重启也可。以后系统重启时系统也会自动加载。
/////////////////////////////////////
直接修改注册表,也可以!
还不会在系统设备管理里面看到设备,对于那些对异物敏感的客户可能有效,呵呵! \Registry\Machine\System\CurrentControlSet\Services\ABCDEFG
Type = REG_DWORD 0x00000001
Start = REG_DWORD 0x00000002
DisplayName = "myfilename"
ErrorControl = REG_DWORD 0x0000001 这样就可以,自己写吧,只要把sys拷贝到 system32/drivers/即可
重新启动,哈哈,不是就ok了吗? 我已经照做了,可还是不行,请高手帮帮我啊!!
这个SYS是人家提供给我的,用他提供的安装程序安装后是可以正确使用的,但是我现在用它进行开发后,想将它和我的应用程序一起做成一个安装包,但就是装不上去。以上注册表项是它的安装程序建立的,另外还有一项,但是我用REGEDIT既不能删除,也不能导入。其值为:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_abcd]
"NextInstance"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_DLPORTIO\0000]
"Service"="abcd"
"Legacy"=dword:00000001
"ConfigFlags"=dword:00000000
"Class"="LegacyDriver"
"ClassGUID"="{8ECC055D-047F-11D1-A537-0000F8753ED1}"
"DeviceDesc"="abcd I/O Driver"
"Capabilities"=dword:00000000 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_DLPORTIO\0000\Control]
"DeviceReference"=dword:81888430
"ActiveService"="abcd" 不知此项对其加载有无影响,又是如何建立的?
/////////////////////////////////////
直接修改注册表,也可以!
还不会在系统设备管理里面看到设备,对于那些对异物敏感的客户可能有效,呵呵! \Registry\Machine\System\CurrentControlSet\Services\ABCDEFG
Type = REG_DWORD 0x00000001
Start = REG_DWORD 0x00000002
DisplayName = "myfilename"
ErrorControl = REG_DWORD 0x0000001 这样就可以,自己写吧,只要把sys拷贝到 system32/drivers/即可
重新启动,哈哈,不是就ok了吗? 我已经照做了,可还是不行,请高手帮帮我啊!!
这个SYS是人家提供给我的,用他提供的安装程序安装后是可以正确使用的,但是我现在用它进行开发后,想将它和我的应用程序一起做成一个安装包,但就是装不上去。以上注册表项是它的安装程序建立的,另外还有一项,但是我用REGEDIT既不能删除,也不能导入。其值为:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_abcd]
"NextInstance"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_DLPORTIO\0000]
"Service"="abcd"
"Legacy"=dword:00000001
"ConfigFlags"=dword:00000000
"Class"="LegacyDriver"
"ClassGUID"="{8ECC055D-047F-11D1-A537-0000F8753ED1}"
"DeviceDesc"="abcd I/O Driver"
"Capabilities"=dword:00000000 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_DLPORTIO\0000\Control]
"DeviceReference"=dword:81888430
"ActiveService"="abcd" 不知此项对其加载有无影响,又是如何建立的?
解决方案 »
- 求飞鸽较新版本的MFC源码,原版的,最好是中文版,谢谢!
- listctrl显示大量数据
- 整过图像exif信息的进!
- 串口接收数据?
- 大虾们,请教一个问题。。。
- 分多就先问
- wds315()进来,谢谢你的帮助3
- 下载的例程Debug版出错,Release版通过。不知是程序出错,还是VC(英文)与系统(中文)的MFC42XXX.DLL有冲突。(我很菜)
- 我想在CView以外写进CView的东西不会被擦掉!所以在CView里定义了一个Public变量CDC,所有的显示操作都在CDC里完成,但是有一个问题解决不
- VS2005在windows2008上编译DLL工程没有反应?
- 请问怎样利用API函数获得键盘字符(限于字母而已)/谢
- ###搞不懂:MFC的COM工程,如何使一个组件类支持多个IDispatch派生接口?
(不过建议你最好先找找驱动开发的入门级的书看看.)
OpenSCManager
CreateService
OpenService
StartService
CloseServiceHandle最好用.SYS附带的.DEF文件来安装这个设备驱动.这样虽然会在系统设备管理里面看到设备,但比较安全.不要直接修改注册表!
A。在Win98下,将.inf拷到<WINDIR>Inf下,.sys拷到<WINDIR>System32Drivers下,并且删除<WINDIR>Inf下的DRVIDX.BIN和DRVDATA.BIN,再插入硬件。Win2K下,用SetupCopyOEMInf将.inf文件拷到<WINDIR>Inf下,并且.inf中不能有拷贝.inf的句子,否则,当插入第二个硬件时,系统仍然会提示找不到.sys。
//I have tested this in installshield
[368enhanced]
device=[path]\abc.sys
#include <windows.h>
#include <winsvc.h>
#include <conio.h>
#include <stdio.h> int main(int argc, char* argv[])
{
HANDLE hWdm;
printf("Hello World!\n"); SC_HANDLE hServiceMgr, hServiceTwdm;
BOOL bRtn;
DWORD dwRtn, dwSize = 256;
char szDir[256];
GetCurrentDirectory( dwSize, szDir );//取当前目录
strcat( szDir, "\\NPF.sys" ); //取驱动程序的全路径
LPCTSTR lpszBinaryPathName = TEXT(szDir);
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); //打开服务控制管理器
if( hServiceMgr == NULL ) {
printf( "OpenSCManager() Faild %d ! \n", GetLastError() );
return 0;
}
else
{
printf( "OpenSCManager() ok ! \n" );
} hServiceTwdm = CreateService( hServiceMgr,
TEXT("NPF"), //SYSTEMCurrentControlSetServices 驱动程序的在注册表中的名字
TEXT("NPF"), // 注册表驱动程序的 DisplayName 值
SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限
SERVICE_KERNEL_DRIVER,// 表示加载的服务是驱动程序
2, // 注册表驱动程序的 Start 值
1, // 注册表驱动程序的 ErrorControl 值
lpszBinaryPathName, // 注册表驱动程序的 ImagePath 值
NULL,
NULL,
NULL,
NULL,
NULL);
if( hServiceTwdm == NULL )
{
dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
{
CloseServiceHandle( hServiceMgr );
printf( "CrateService() Faild %d ! \n", dwRtn );
return 0;
}
else
{
printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" );
} // 驱动程序已经加载,只需要打开
hServiceTwdm = OpenService( hServiceMgr, TEXT("NPF"), SERVICE_ALL_ACCESS );
if( hServiceTwdm == NULL )
{
dwRtn = GetLastError();
CloseServiceHandle( hServiceMgr );
printf( "OpenService() Faild %d ! \n", dwRtn );
return 0;
}
else
{
printf( "OpenService() ok ! \n" );
}
}
else
{
printf( "CrateService() ok ! \n" );
}
printf( "按任意键退出 !\n" );
getch();
return 0;
}
不过:
"// 驱动程序已经加载,只需要打开
hServiceTwdm = OpenService...."
此时驱动程序并没有加载,只是打开了操作系统的管理器.
要在OpenService之后StartService,再用CreateFile打开驱动程序的句柄.再通过这个句柄访问驱动程序.
OpenSCManager之后,在OpenService打不开的情况下,才用CreateService(通常是在该程序第一次运行的时候通过CreateService修改注册表.).给你一分源码参考.
int StartPctIODriver()
{
SC_HANDLE SchSCManager;
SC_HANDLE schService;
BOOL ret;
DWORD err;
if (hPctIODrv != NULL)
{
bStopHandle = false;
return 0;
}
else
{
bStopHandle = true;
} /* Open Handle to Service Control Manager */
SchSCManager = OpenSCManager(NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (SchSCManager == NULL)
{
if (GetLastError() == ERROR_ACCESS_DENIED)
{
/* We do not have enough rights to open the SCM, therefore we must */
/* be a poor user with only user rights. Check if we can open */
/* handle to driver */
if (OpenHandletoPctDriver())
{
//printf("PCT: The PCT driver is running. [USER RIGHTS]\n");
return 1; /* All done */
}
//printf("\nPCT: You do not have rights to access the Service Control Manager and\n");
//printf("PCT: the PCT driver is not installed or stated.\n");
return 2;
}
}
int count = 0;
do{
count++;
/* Open a Handle to the PCT Service Database */
schService = OpenService(SchSCManager, // handle to service control manager database
"PctDrv", // pointer to name of service to start
SERVICE_ALL_ACCESS // type of access to service
);
if (schService == NULL)
{
switch (GetLastError())
{
case ERROR_ACCESS_DENIED:
//printf("PCT: You do not have rights to the PCT service database\n");
return 3;
case ERROR_INVALID_NAME:
//printf("PCT: The specified service name is invalid.\n");
return 4;
case ERROR_SERVICE_DOES_NOT_EXIST:
InstallPctDriver();
break;
}
}
} while ((schService == NULL) && (count < 3));
/* Start the PCT Driver. Errors will occur here if */
/* .SYS file doesn't exist */
ret = StartService (schService, // service identifier
0, // number of arguments
NULL // pointer to arguments
);
if (ret)
{
//printf("PCT: The PCT driver has been successfully started.\n");
}
else
{
err = GetLastError();
if (err == ERROR_SERVICE_ALREADY_RUNNING)
{
//printf("PCT: The PCT driver is already running.\n");
}
else
{
//printf("PCT: Unknown error while starting PCT driver service.\n");
//printf("PCT: Does PctDrv.SYS exist in your \\System32\\Drivers Directory?\n");
CloseServiceHandle (schService);
return 5;
}
}
/* Now driver is loaded and started, Open handle to PCT Driver so we can talk to it */
if (!OpenHandletoPctDriver())
{
//printf("PCT: Couldn't create handle to PCT driver, Please ensure driver is loaded.\n");
CloseServiceHandle (schService);
return 6;
}
/* Close handle to Service Control Manager */
CloseServiceHandle (schService);
return 0;
}void StopPctIODriver()
{
if (bStopHandle)
{
CloseHandle (hPctIODrv);
}
bStopHandle = true;
}BOOL OpenHandletoPctDriver(void)
{
char ErrMsg[100];
char device[20];
sprintf(device,"\\\\.\\PCT_0"); hPctIODrv = CreateFile( device,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); if(hPctIODrv == INVALID_HANDLE_VALUE)
{
DWORD errCode = GetLastError();
sprintf(ErrMsg,"Open Handle to device %s Fail!\r\nError Code: %d",device,errCode);
MessageBox(NULL,ErrMsg,NULL,MB_OK|MB_ICONERROR);
return(FALSE);
}
else
{
return(TRUE);
}
}void InstallPctDriver(void)
{
SC_HANDLE SchSCManager;
SC_HANDLE schService;
DWORD err;
/* Open Handle to Service Control Manager */
SchSCManager = OpenSCManager(NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
/* Create Service/Driver - This adds the appropriate registry keys in */
/* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services - It doesn't */
/* care if the driver exists, or if the path is correct. */
schService = CreateService (SchSCManager, // SCManager database
"PctDrv", // name of service
"PctDrv", // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_KERNEL_DRIVER, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
"System32\\Drivers\\PctDrv.SYS", // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL // no password
);
if (schService == NULL)
{
err = GetLastError();
if (err != ERROR_SERVICE_EXISTS)
return;
}
/* Close Handle to Service Control Manager */
CloseServiceHandle (schService);
}
不过:
"// 驱动程序已经加载,只需要打开
hServiceTwdm = OpenService...."
此时驱动程序并没有加载,只是打开了操作系统的管理器.
要在OpenService之后StartService,再用CreateFile打开驱动程序的句柄.再通过这个句柄访问驱动程序.
OpenSCManager之后,在OpenService打不开的情况下,才用CreateService(通常是在该程序第一次运行的时候通过CreateService修改注册表.).给你一分源码参考.
int StartPctIODriver()
{
SC_HANDLE SchSCManager;
SC_HANDLE schService;
BOOL ret;
DWORD err;
if (hPctIODrv != NULL)
{
bStopHandle = false;
return 0;
}
else
{
bStopHandle = true;
} /* Open Handle to Service Control Manager */
SchSCManager = OpenSCManager(NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (SchSCManager == NULL)
{
if (GetLastError() == ERROR_ACCESS_DENIED)
{
/* We do not have enough rights to open the SCM, therefore we must */
/* be a poor user with only user rights. Check if we can open */
/* handle to driver */
if (OpenHandletoPctDriver())
{
//printf("PCT: The PCT driver is running. [USER RIGHTS]\n");
return 1; /* All done */
}
//printf("\nPCT: You do not have rights to access the Service Control Manager and\n");
//printf("PCT: the PCT driver is not installed or stated.\n");
return 2;
}
}
int count = 0;
do{
count++;
/* Open a Handle to the PCT Service Database */
schService = OpenService(SchSCManager, // handle to service control manager database
"PctDrv", // pointer to name of service to start
SERVICE_ALL_ACCESS // type of access to service
);
if (schService == NULL)
{
switch (GetLastError())
{
case ERROR_ACCESS_DENIED:
//printf("PCT: You do not have rights to the PCT service database\n");
return 3;
case ERROR_INVALID_NAME:
//printf("PCT: The specified service name is invalid.\n");
return 4;
case ERROR_SERVICE_DOES_NOT_EXIST:
InstallPctDriver();
break;
}
}
} while ((schService == NULL) && (count < 3));
/* Start the PCT Driver. Errors will occur here if */
/* .SYS file doesn't exist */
ret = StartService (schService, // service identifier
0, // number of arguments
NULL // pointer to arguments
);
if (ret)
{
//printf("PCT: The PCT driver has been successfully started.\n");
}
else
{
err = GetLastError();
if (err == ERROR_SERVICE_ALREADY_RUNNING)
{
//printf("PCT: The PCT driver is already running.\n");
}
else
{
//printf("PCT: Unknown error while starting PCT driver service.\n");
//printf("PCT: Does PctDrv.SYS exist in your \\System32\\Drivers Directory?\n");
CloseServiceHandle (schService);
return 5;
}
}
/* Now driver is loaded and started, Open handle to PCT Driver so we can talk to it */
if (!OpenHandletoPctDriver())
{
//printf("PCT: Couldn't create handle to PCT driver, Please ensure driver is loaded.\n");
CloseServiceHandle (schService);
return 6;
}
/* Close handle to Service Control Manager */
CloseServiceHandle (schService);
return 0;
}void StopPctIODriver()
{
if (bStopHandle)
{
CloseHandle (hPctIODrv);
}
bStopHandle = true;
}BOOL OpenHandletoPctDriver(void)
{
char ErrMsg[100];
char device[20];
sprintf(device,"\\\\.\\PCT_0"); hPctIODrv = CreateFile( device,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); if(hPctIODrv == INVALID_HANDLE_VALUE)
{
DWORD errCode = GetLastError();
sprintf(ErrMsg,"Open Handle to device %s Fail!\r\nError Code: %d",device,errCode);
MessageBox(NULL,ErrMsg,NULL,MB_OK|MB_ICONERROR);
return(FALSE);
}
else
{
return(TRUE);
}
}void InstallPctDriver(void)
{
SC_HANDLE SchSCManager;
SC_HANDLE schService;
DWORD err;
/* Open Handle to Service Control Manager */
SchSCManager = OpenSCManager(NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
/* Create Service/Driver - This adds the appropriate registry keys in */
/* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services - It doesn't */
/* care if the driver exists, or if the path is correct. */
schService = CreateService (SchSCManager, // SCManager database
"PctDrv", // name of service
"PctDrv", // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_KERNEL_DRIVER, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
"System32\\Drivers\\PctDrv.SYS", // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL // no password
);
if (schService == NULL)
{
err = GetLastError();
if (err != ERROR_SERVICE_EXISTS)
return;
}
/* Close Handle to Service Control Manager */
CloseServiceHandle (schService);
}
所以不能在98下用这种方法.
而且WIN2K的驱动程序不一定被98支持.驱动程序挺复杂的.
For details,还是建议你去WWW.DRIVERDEVELOP.COM问问.
比如是并口的,还是USB的,大致是实现什么功能的,等等.
OpenSCManager函数不被支持,请问,我该从什么地方入手解决这个问题?
用你的方法我已经实现了2000下的启动,但在98下出错?谢谢!请多指教!
关注。。
SetupDiGetClassDevs,// Get handle to relevant device information set
SetupDiEnumDeviceInterfaces,// Get interface data for the requested instance
SetupDiGetDeviceInterfaceDetail,// Get size of symbolic link name
SetupDiGetDeviceInterfaceDetail,// Get symbolic link name
CreateFile,// Open file
SetupDiDestroyDeviceInfoList,源代码供参考:
// GetDeviceViaInterface: Open a handle via a device interfaceHANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance)
{
// Get handle to relevant device information set
HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if(info==INVALID_HANDLE_VALUE)
{
printf("No HDEVINFO available for this GUID\n");
return NULL;
} // Get interface data for the requested instance
SP_INTERFACE_DEVICE_DATA ifdata;
ifdata.cbSize = sizeof(ifdata);
if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata))
{
printf("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n");
SetupDiDestroyDeviceInfoList(info);
return NULL;
} // Get size of symbolic link name
DWORD ReqLen;
SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
if( ifDetail==NULL)
{
SetupDiDestroyDeviceInfoList(info);
return NULL;
} // Get symbolic link name
ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
{
SetupDiDestroyDeviceInfoList(info);
delete ifDetail;
return NULL;
} printf("Symbolic link is %s\n",ifDetail->DevicePath);
// Open file
HANDLE rv = CreateFile( ifDetail->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( rv==INVALID_HANDLE_VALUE) rv = NULL; delete ifDetail;
SetupDiDestroyDeviceInfoList(info);
return rv;
}