在 VBA 中使用 Alias 关键字声明函数

发布于 2024-11-03 01:28:39 字数 255 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2024-11-10 01:28:39

不需要,使用 Alias 不需要特殊的库;这一切都内置于语言中。

但你的声明是错误的。在 Alias 之前放置了一组额外的括号,这会让编译器感到困惑。

除了纯语法之外,第二个参数 (nSize) 实际上是一个指向 Long指针,这意味着您需要将其传递给 VBA 中的 ByRef

因此,修改后的声明将如下所示:

Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
        (ByVal lpBuffer As String, ByRef nSize As Long) As Long

如果函数成功,则返回值为 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 a Long, which means that you need to pass it ByRef in VBA.

So the revised declaration would look like this, instead:

Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
        (ByVal lpBuffer As String, ByRef nSize As Long) As Long

The return value will be 1 if the function succeeds, or 0 if it fails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文