在 VBA 中使用 Alias 关键字声明函数
我有 VBA MS Access 表单代码,在其中键入以下函数声明:
Public Declare Function GetUserName Lib "advapi32.dll" () Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
但是,我在 Alias
上收到错误。我是否必须添加一些参考文献才能使用它?
I have VBA MS Access form code, where I type the following function declaration:
Public Declare Function GetUserName Lib "advapi32.dll" () Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
However I'm getting an error on Alias
. Do I have to add some references in order to use this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要,使用
Alias
不需要特殊的库;这一切都内置于语言中。但你的声明是错误的。在
Alias
之前放置了一组额外的括号,这会让编译器感到困惑。除了纯语法之外,第二个参数 (
nSize
) 实际上是一个指向Long
的指针,这意味着您需要将其传递给VBA 中的 ByRef
。因此,修改后的声明将如下所示:
如果函数成功,则返回值为 1;如果失败,则返回值为 0。
No, there are no special libraries are required to use
Alias
; this is all built into the language.But your declaration is wrong. You have an extra set of parentheses placed just before
Alias
that are confusing the compiler.Beyond pure syntax, the second parameter (
nSize
) is actually a pointer to aLong
, which means that you need to pass itByRef
in VBA.So the revised declaration would look like this, instead:
The return value will be 1 if the function succeeds, or 0 if it fails.