Using Alias keyword to declare a function in VBA

假装没事ソ 提交于 2020-01-02 15:57:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!