我想写一个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;}
{
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;}
解决方案 »
- CListCtrl设置选中状态问题~~
- 这边的兄弟谁有《COM组件技术内幕》示例代码
- 谁用过或写过或哪里有像CORALDRAW里面的颜色拾取器的类或控件下载?
- 请问怎么通过编程强制把UDP包分成两段发送出去?
- 如何使用vc调用别的语言开发的dll!!!!!.......急
- 请问如何实现程序保留最近访问过的路径,并且以访问的时间排序,最近访问的靠前.
- 如何在屏幕上画一个圆心在正弦曲线上运动的圆?我用gdi弄的效果很差。
- vc++ 有关dll调用dll的问题
- 为什么有些人编译的生成的程序会放在bin文件夹内?有什么讲究吗?我一般都放在Release中
- 论 [类],[对象]和[实例]!!
- TCP中的超时重传之间不同的时间差的倍数关系(比如第一次重传的时间是6秒,然后第2次是12秒)是怎么回事( TCP/IP详解)?
- 冲击波病毒
"qcusbmdm.sys", //这两个参数,我不知道用的对不对?这两个是服务的名字和显示的名字。不是驱动文件路径。
介个才是驱动文件的路径
lpszBinaryPathName