“安装程序打开InfFile” Delphi2010 出现访问冲突错误
在 Delphi 2010 中使用以下行代码时,出现“访问冲突”错误,但相同的代码在 VC++ 中工作正常。
Delphi 2010 代码是
var
hMyInf : HINF;
begin
hMyInf := SetupOpenInfFile('.\\DIGIMHID.INF','Mouse', INF_STYLE_WIN4,Nil);
VC++ 代码是
hMyInf = SetupOpenInfFile(".\\DigimHID.inf", "Mouse", INF_STYLE_WIN4, NULL);
请帮我解决这个问题。 谢谢大家。
When using the following line code in Delphi 2010, a'm getting an "Access Violation" error, but the same code working fine in VC++.
The Delphi 2010 code is
var
hMyInf : HINF;
begin
hMyInf := SetupOpenInfFile('.\\DIGIMHID.INF','Mouse', INF_STYLE_WIN4,Nil);
The VC++ code is
hMyInf = SetupOpenInfFile(".\\DigimHID.inf", "Mouse", INF_STYLE_WIN4, NULL);
Please help me to solve this issue.
Thanks All.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用
SetupAPI.pas
Edit 中的任何方法之前调用
LoadSetupAPI
,以提供一些背景信息:正如 David 在他的回答中和我同时写的那样在我的评论中,该错误可能是由于调用未初始化的方法指针引起的。对我来说,第一个提示是错误消息,即访问冲突:如果相当于访问冲突的内容来自 Windows 本身,则称为运行时错误 216
。代码非常简单,只使用常量和方法调用。常量无法生成 AV,因此错误必须来自方法本身,或者来自调用该方法。由于提供的 Delphi 声明显示了“函数类型”,我怀疑
SetupOpenInfFile
实际上是一个方法指针,而不是import
方法。这些指针需要以某种方式进行初始化。搜索SetupAPI.pas
(感谢 google 提供链接,因为我不使用 JEDI 库)我很快发现它是从LoadSetupAPI
分配的。我的第一个想法:不是从initialization
部分调用LoadSetupAPI
吗?事实并非如此,因此需要从代码中调用它。 问题已解决。Call
LoadSetupAPI
before using any methods in theSetupAPI.pas
Edit, to provide some background: As simultaneously wrote by David in his answer and by me in my comment, the error is probably caused by calling an uninitialized method pointer. For me the first tip was the error message, an Access Violation: If the equivalent of an Access Violation came from Windows itself, it'd be called a
Runtime Error 216
. The code is very simple, only uses constants and a method call. Constants can't generate AV's so the error had to come from the method itself, or from calling the method.Since the Delphi declaration supplied showed a "function type", I suspected
SetupOpenInfFile
is actually an method pointer, not animport
method. Those pointers need to somehow be initialized. SearchingSetupAPI.pas
(thanks google for providing a link, because I don't use JEDI libraries) I quickly found that it's being assigned fromLoadSetupAPI
. My first thought: isn'tLoadSetupAPI
called from theinitialization
section? It's not, so it needs to be called from code. Problem solved.Delphi 版本中你的文件名是错误的。在 Delphi 中你不能转义 \ ,一个就可以了。但这不会导致访问冲突。
我的猜测是您的 GetProcAddress 调用失败。但这只是猜测。我想查看更多代码和完整的错误消息。
编辑
看来我们走在正确的轨道上。 Cosmin的回答将为您解决问题。另一种方法是通过删除
SetupApi.pas
中条件SETUPAPI_LINKONREQUEST
的定义来切换到隐式链接。Your filename is wrong in the Delphi version. You don't escape \ in Delphi, a single one will do. But that wouldn't lead to an access violation.
My guess is that your GetProcAddress call is failing. But that is a guess. I'd like to see more code and the full error message.
EDIT
It seems that we were on the right track. Cosmin's answer will solve the problem for you. An alternative would be to switch to inplicit linking by removing the definition of the condition
SETUPAPI_LINKONREQUEST
inSetupApi.pas
.