使用别名关键字来声明在VBA函数别名、函数、关键字、声明

2023-09-08 11:17:23 作者:笙歌相知起

我有VBA的MS Access表code,其中I型下面的函数声明:

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?

推荐答案

没有,有没有特殊的库都必须使用别名;这是所有语言内置的。

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.

除了纯粹的语法,第二个参数( n大小)实际上是一个的指针的到,这意味着你需要通过它的ByRef 在VBA。

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

返回值将是1,如果函数成功,或者0,如果它失败。

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