开发者

"SetupOpenInfFile" getting access violation error in Delphi2010

开发者 https://www.devze.com 2023-02-10 17:56 出处:网络
When using the following 开发者_运维技巧line code in Delphi 2010, a\'m getting an \"Access Violation\" error, but the same code working fine in VC++.

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.


Call LoadSetupAPI before using any methods in the SetupAPI.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 an import method. Those pointers need to somehow be initialized. Searching SetupAPI.pas (thanks google for providing a link, because I don't use JEDI libraries) I quickly found that it's being assigned from LoadSetupAPI. My first thought: isn't LoadSetupAPI called from the initialization section? It's not, so it needs to be called from code. Problem solved.


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 in SetupApi.pas.

0

精彩评论

暂无评论...
验证码 换一张
取 消