首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

加载驱动时出现的有关问题

2013-09-11 
加载驱动时出现的问题我最近在学一些关于驱动方面的知识,但有一个问题老是解决不了代码如下这是一段加载驱

加载驱动时出现的问题
我最近在学一些关于驱动方面的知识,但有一个问题老是解决不了代码如下
这是一段加载驱动的代码,我在我的64位CPU,64位win7系统下老下运行出错,但在32位系统上或32位cpu配64位win7却能正常运行,上面出错原因是
StartService faild() 2!这好像是路径问题,但是我的路径可以很确定没有问题的,有没有高人帮我看下哪里出问题了
#include<windows.h>
#include<winsvc.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

LPCWSTR DRIVER_NAME={"GENERICDRV"};
LPCWSTR DRIVER_PATH={"amifldrv64.sys"};


//加载驱动的程序,但是这个程序在win7下可能需要管理员权限才能
//打开SCManager或加载驱动
BOOL LoadNTDriver(LPCWSTR lpszDriverName,LPCWSTR lpszDriverPath)
{
DWORD dwRtn;
LPWSTR szDriverImagePath[MAX_PATH];
BOOL bRet = FALSE;

SC_HANDLE hServiceMgr = NULL;
SC_HANDLE hServiceDDK = NULL;
     GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);

hServiceMgr = OpenSCManager( 
NULL,         // SCM database 
        NULL,
        SC_MANAGER_ALL_ACCESS// name of service 
);

if( hServiceMgr == NULL )
{
//OpenSCManager
printf( "OpenSCManager() Faild %d ! \n", GetLastError() );
getchar();
bRet = FALSE;
goto BeforeLeave;
}
else
{
////OpenSCManager
printf( "OpenSCManager() ok ! \n" );
}


hServiceDDK = CreateService( hServiceMgr,
lpszDriverName,
lpszDriverName,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
szDriverImagePath,
NULL,
NULL,
NULL,
NULL,
NULL);




if( hServiceDDK == NULL )
{
dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
{

printf( "CrateService() Faild %d ! \n", dwRtn );
getchar();
bRet = FALSE;
goto BeforeLeave;
}
else
{

printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" );
}


hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );
if( hServiceDDK == NULL )
{

dwRtn = GetLastError();
printf( "OpenService() Faild %d ! \n", dwRtn );


getchar();
bRet = FALSE;
goto BeforeLeave;
}
else
{
printf( "OpenService() ok ! \n" );
}
}
else
{
printf( "CrateService() ok ! \n" );
}


bRet= StartService(hServiceDDK, NULL, NULL);
if(!bRet)
{
DWORD dwRtn = GetLastError();
if(dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING)
{
printf( "StartService() Faild %d ! \n", dwRtn );

bRet = FALSE;
goto BeforeLeave;
}
else
{
if(dwRtn == ERROR_IO_PENDING)
{

printf("StartService() Faild ERROR_IO_PENDING ! \n");
getchar();
bRet = FALSE;
goto BeforeLeave;
}
else
{

printf("StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! \n");
bRet = TRUE;
goto BeforeLeave;
}
}
}
bRet = TRUE;
//DeleteFile(szDriverImagePath);

BeforeLeave:
if(hServiceDDK)
{
CloseServiceHandle(hServiceDDK);
}
if(hServiceMgr)
{
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
谢谢了。 win7 64位 驱动加载 驱动开发
[解决办法]
参考http://stackoverflow.com/questions/13545675/startservice-and-sc-start-return-error-file-not-found

When it's running in WOW64 it goes to Windows\SysWOW64 not system32.

Once I copied the 32 bit Service .exe into Windows\SysWOW64 and ran it the Service started!

[解决办法]
在64位Windows下:
64位exe和dll在目录c:\windows\system32目录下;
32位exe和dll在目录c:\windows\syswow64目录下;

热点排行