Delphi中的Windows API参数-传递带或不带@运算符的var参数? [英] Windows API Parameters in Delphi - passing var parameter with or without @ operator?

查看:143
本文介绍了Delphi中的Windows API参数-传递带或不带@运算符的var参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个调用带有Delphi 7的SetupDiGetDeviceRegistryProperty
该调用来自示例函数 SetupEnumAvailableComPorts 。看起来像这样:

I'm trying to use a function that calls SetupDiGetDeviceRegistryProperty with Delphi 7. The call is from the example function SetupEnumAvailableComPorts. It looks like this:

SetupDiGetDeviceRegistryProperty(
  DevInfoHandle,
  DeviceInfoData,
  RegProperty,
  @PropertyRegDataType,
  nil,
  0,
  @RequiredSize
);

在@PropertyRegDataType参数上出现错误实际参数和形式参数的类型必须相同,和@RequiredSize。这些参数声明为:

I get the error "Types of actual and formal parameters must be identical" on the parameters @PropertyRegDataType, and @RequiredSize. These parameters are declared:

var
  RequiredSize: Cardinal;
  PropertyRegDataType: DWORD;

MSDN将这些参数描述为: RequiredSize [out,optional]指向变量的指针DWORD类型,其接收保存所请求属性的数据所需的PropertyBuffer缓冲区的大小(以字节为单位)。此参数是可选的,可以为NULL。 PropertyRegDataType [out ,可选]指向变量的指针,该变量接收要检索的属性的数据类型。这是标准注册表数据类型之一。此参数是可选的,可以为NULL。

MSDN describes these parameters as: "RequiredSize [out, optional] A pointer to a variable of type DWORD that receives the required size, in bytes, of the PropertyBuffer buffer that is required to hold the data for the requested property. This parameter is optional and can be NULL." and "PropertyRegDataType [out, optional] A pointer to a variable that receives the data type of the property that is being retrieved. This is one of the standard registry data types. This parameter is optional and can be NULL."

SetupDiGetDeviceRegistryProperty的声明(在SetupAPI.pas中来自 JVCL )如下:

The declaration of SetupDiGetDeviceRegistryProperty (in SetupAPI.pas from JVCL) looks like:

function SetupDiGetDeviceRegistryProperty(
  DeviceInfoSet: HDEVINFO;
  const DeviceInfoData: TSPDevInfoData; 
  Property_: DWORD;
  var PropertyRegDataType: DWORD; 
  PropertyBuffer: PBYTE; 
  PropertyBufferSize: DWORD;
  var RequiredSize: DWORD
): BOOL; stdcall; {$EXTERNALSYM SetupDiGetDeviceRegistryProperty}

由于PropertyRegDataType和RequiredSize是var参数,因此无需传递它们就可以运营商。实际上,如果我从函数调用参数中删除@运算符,则代码会编译,但由于访问冲突(读取地址0)而崩溃。 原始代码是为Delphi 7编写的,所以为什么他们要使用@这些参数上的运算符?我缺少什么?

Since PropertyRegDataType and RequiredSize are var parameters, they should be able to be passed without the @ operator. In fact, if I remove the @ operators from the function call parameters, the code compiles, but crashes with an access violation (read of address 0). The original code was written for Delphi 7, so why would they use the @ operator on these parameters? What am I missing?

推荐答案

两个变量 PropertyRegDataType RequiredSize 应该声明为 DWORD 。您实际上将 RequiredSize 声明为 Cardinal ,尽管这是等效的。

Both of your variables PropertyRegDataType and RequiredSize should be declared as DWORD. You actually declare RequiredSize as Cardinal although that is equivalent.

您是正确的,因为它们是 var 参数,因此不应包含 @ 地址运算符。

You are correct that since they are var parameters you should not include the @ address operator.

很难说为什么收到错误消息而实际上不知道您为其他参数传递了什么。

It's hard to say why you are getting the error message without actually knowing what you pass for the other parameters.

EDIT

正如评论员指出的那样,JEDI翻译不正确,并且 PropertyRegDataType 是因为是一个可选参数,应按值传递,并键入为 PDWORD ,以便您可以传递 nil

As commentators have pointed out, the JEDI translation is incorrect and PropertyRegDataType, because it is an optional parameter, should be passed by value and typed as PDWORD so that you are able to pass nil.

这篇关于Delphi中的Windows API参数-传递带或不带@运算符的var参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆