windows 使用 INF 文件 C++ 以编程方式安装驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6473096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing a driver programmatically using INF file c++
提问by newdev1
Could someone here please let me know how to install 3rd party device drivers programmatically if all the required files i.e. inf file, .sys etc are provided. The minimum operating system this solution SHOULD work on is Windows2000.
如果提供了所有必需的文件,即 inf 文件、.sys 等,这里有人可以告诉我如何以编程方式安装 3rd 方设备驱动程序。此解决方案应该适用的最低操作系统是 Windows2000。
I tried copying the .inf
file into the Win Folder\INF folder and the sys file
into the Win folder\system32\drivers but each time plug in the device, windows
pops up Found New Hardware user interface which is what i am trying to avoid.
我尝试将.inf
文件复制到 Win Folder\INF 文件夹中,并将 sys 文件复制到 Win 文件夹\system32\drivers 中,但每次插入设备时,Windows 都会弹出 Found New Hardware 用户界面,这是我试图避免的。
Below is something i tried but the function returns error 87 (The parameter is incorrect)
.
下面是我尝试过但函数返回的内容error 87 (The parameter is incorrect)
。
HINF HInf;
UINT ErrorLine;
BOOL bRes = FALSE;
PBOOL FileWasInUse = FALSE;
LPCSTR szSourceFileName = _T("C:\Drivers_HypercomP1320\hypvcpusb.inf");
LPCSTR szInfFileName = _T("hypvcpusb.inf");
PVOID Context = NULL;
HInf = SetupOpenInfFile ( szSourceFileName, NULL, INF_STYLE_WIN4, &ErrorLine);
LPCSTR SourceFile = ("hypvcp.sys");
LPCSTR SourcePathRoot = _T("C:\Drivers_HypercomP1320");
LPCSTR DestinationName = _T("C:\WINDOWS\system32\drivers\hypvcp.sys");
bRes = SetupInstallFileEx ( HInf, NULL, SourceFile, SourcePathRoot, DestinationName, SP_COPY_FORCE_IN_USE,
(PSP_FILE_CALLBACK)CopyMsgHandler, Context, FileWasInUse);
DWORD dwVal = GetLastError();
SetupCloseInfFile(HInf);
// Callback function
UINT CopyMsgHandler (UINT Context, UINT Notification,UINT_PTR Param1, UINT_PTR Param2)
{
UINT rtnValue = NO_ERROR;
return rtnValue;
}
Thanks.
谢谢。
回答by Luke
You can use InstallHinfSection.
您可以使用InstallHinfSection。
回答by engf-010
It might be your use of
这可能是你的用途
PBOOL FileWasInUse = FALSE;
PBOOL FileWasInUse = FALSE;
. You should change it in
. 你应该改变它
BOOL FileWasInUse = FALSE;
BOOL FileWasInUse = FALSE;
and use it in the function-call with &FileWasInUse(note the &-character).
并在带有&FileWasInUse的函数调用中使用它(注意& 字符)。
回答by Michael Haephrati
Yes. You start by calling
是的。你开始打电话
SC_HANDLE manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (manager)
{
wprintf(L"Opened SC Manager\n");
}
else
{
wprintf(L"Open SC Manager failed\n");
return;
}
Then having the .inf file stored in szInfFileName you call:
然后将 .inf 文件存储在您调用的 szInfFileName 中:
HInf = SetupOpenInfFile(szInfFileName.c_str(), NULL, INF_STYLE_WIN4, &ErrorLine);
Then you call
然后你打电话
if (SetupInstallFileEx(HInf, NULL, SourceFile, SourcePathRoot, DestinationName, SP_COPY_NEWER_OR_SAME, NULL, Context, &FileWasInUse) == NULL)
SourceFile= the driver file name (ending with .sys) SourcePathRoot= the location of the driver file (would be the path where your program runs from) DestinationName= full path of the driver to be installed (for example:
SourceFile= 驱动程序文件名(以 .sys 结尾) SourcePathRoot= 驱动程序文件的位置(应该是程序运行的路径) DestinationName= 要安装的驱动程序的完整路径(例如:
c:\windows\system32\drivers\yourdriver.sys
Then there is the Registry. You need to add an entry for your driver under
然后是注册表。您需要在下为您的驱动程序添加一个条目
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\
this entry (key) should have: Driver name, display name, description, ErrorControl and Group.
此条目(键)应具有:驱动程序名称、显示名称、描述、错误控制和组。
Next step, you start the driver using:
下一步,您使用以下命令启动驱动程序:
SC_HANDLE service = CreateService(manager,
DRIVER_NAME,
DRIVER_NAME,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
KeyName,
NULL, NULL, NULL, NULL, NULL);
When KeyName is the driver's path under System32 as appeared in the Registry entry. For example:
当 KeyName 是注册表项中出现的 System32 下的驱动程序路径时。例如:
system32\drivers\yourdriver.sys
Last step:
最后一步:
BOOL result = StartService(service, 0, NULL);
and cleaning up
和清理
CloseServiceHandle(manager)