问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/5756827/using-alias-keyword-to-declare-a-function-in-vba