我想写一个USB驱动的自动安装程序,inf和sys文件都以准备好,下面是自动安装程序,但我运行以后,CreateService 函数返回错误,我想可能是我那个参数给的不对吧!请大家帮忙执教!谢谢!int main(int argc, char* argv[])
{
HANDLE hWdm; SC_HANDLE hServiceMgr, hServiceQcusb;
BOOL bRtn;
DWORD dwRtn, dwSize = 256;
char szDir[256]; if( argc > 1 )
{
DelSvr( "qcusbmdm" );
return 0;
} GetCurrentDirectory( dwSize, szDir ); strcat( szDir, "\\qcusbmdm.sys" ); LPCTSTR lpszBinaryPathName = TEXT(szDir);
printf("address is : %s\n",lpszBinaryPathName); hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if( hServiceMgr == NULL )
{
printf( "OpenSCManager() Faild  %d ! \n", GetLastError() );
return 0;
}
else
{
printf( "OpenSCManager()  ok ! \n" );
} hServiceQcusb = CreateService( hServiceMgr,  //返回错误,
"qcusbmdm.sys",    //这个就是我要调用的驱动文件,放在程序根目录
"qcusbmdm.sys",   //这两个参数,我不知道用的对不对?
SERVICE_ALL_ACCESS, 
SERVICE_KERNEL_DRIVER, 
SERVICE_DEMAND_START, 
SERVICE_ERROR_IGNORE, 
lpszBinaryPathName, 
NULL, 
NULL, 
NULL, 
NULL, 
NULL); if( hServiceQcusb ==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" ); hServiceQcusb = OpenService( hServiceMgr, TEXT("qcusbmdm"), SERVICE_ALL_ACCESS ); if( hServiceQcusb ==NULL )
{
dwRtn = GetLastError();
CloseServiceHandle( hServiceMgr );
printf( "OpenService() Faild %d ! \n", dwRtn );
return 0;
}
else
{
printf( "OpenService() ok ! \n" );
} }
else
{
printf( "CrateService() ok  ! \n" );
}
bRtn = StartService( hServiceQcusb, NULL, NULL ); if( !bRtn )
{
//ERROR_PATH_NOT_FOUND
dwRtn = GetLastError(); if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING )
{
printf( "StartService() Faild  %d ! \n", dwRtn );

CloseServiceHandle( hServiceQcusb );
CloseServiceHandle( hServiceMgr ); return 0;
}
else
{
if( dwRtn != ERROR_IO_PENDING  )
printf( "StartService() Faild  ERROR_IO_PENDING ! \n"); else
printf( "StartService() Faild  ERROR_SERVICE_ALREADY_RUNNING ! \n"); }

hWdm = CreateFile("\\\\.\\qcusbmdm",
                         GENERIC_WRITE | GENERIC_READ,
                         0,
                         NULL,
                         OPEN_EXISTING,
                         0,
                         NULL
                         );
if( hWdm != INVALID_HANDLE_VALUE )
{
printf( "Open Driver Twdm ok ! \n" );
}
else
{
printf( "Open Driver Twdm faild %d ! \n", GetLastError() );
} CloseHandle( hWdm );
CloseServiceHandle( hServiceQcusb );
CloseServiceHandle( hServiceMgr ); printf( "按任意键 卸载驱动程序 !\n" );
getch(); DelSvr( "qcusbmdm" ); return 0;
}void DelSvr( char * szSvrName )
{ SC_HANDLE hServiceMgr, hServiceTwdm;
SERVICE_STATUS  SvrSta; hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if( hServiceMgr == NULL )
{
printf( "DelSvr::OpenSCManager() Faild  %d ! \n", GetLastError() );
return;
}
else
{
printf( "DelSvr::OpenSCManager()  ok ! \n" );
} hServiceTwdm = OpenService( hServiceMgr, TEXT(szSvrName), SERVICE_ALL_ACCESS ); if( hServiceTwdm ==NULL )
{
CloseServiceHandle( hServiceMgr );
printf( "DelSvr::OpenService() Faild %d ! \n", GetLastError() );
return;
}
else
{
printf( "DelSvr::OpenService() ok ! \n" );
}
if( !ControlService( hServiceTwdm, SERVICE_CONTROL_STOP , &SvrSta ) )
{
printf( "DelSvr::ControlService() Faild  %d !\n", GetLastError() );
}
else
{
printf( "DelSvr::ControlService() ok !\n" );
}
if( !DeleteService( hServiceTwdm ) )
{
printf( "DelSvr::DeleteSrevice() Faild  %d !\n", GetLastError() );
}
else
{
printf( "DelSvr::DeleteSrevice() ok !\n" );
} CloseServiceHandle( hServiceTwdm );
CloseServiceHandle( hServiceMgr ); return;}