Calling API function from VB
-
I am trying to call the Windows API function "GetUserName" from VB 6, and it always returns 0 for failure. However, I can call this exact same function from Visual C++ 6.0 and it works just fine... Here is the code:
Declare Function _
GetUserName Lib "Advapi32.dll" Alias "GetUserNameA" _
(ByRef lpBuffer As String, ByRef nSize As Integer) As LongDim strUserName As String
Dim nUserLen As long
Dim nRetVal as longnUserLen = 32
strUserName = Space(nUserLen)retVal = GetUserName(strUserName, nUserLen)
:confused:
There are 10 kinds of people - those that get binary and those that don't.
-
I am trying to call the Windows API function "GetUserName" from VB 6, and it always returns 0 for failure. However, I can call this exact same function from Visual C++ 6.0 and it works just fine... Here is the code:
Declare Function _
GetUserName Lib "Advapi32.dll" Alias "GetUserNameA" _
(ByRef lpBuffer As String, ByRef nSize As Integer) As LongDim strUserName As String
Dim nUserLen As long
Dim nRetVal as longnUserLen = 32
strUserName = Space(nUserLen)retVal = GetUserName(strUserName, nUserLen)
:confused:
There are 10 kinds of people - those that get binary and those that don't.
What do you mean by "it doesn't work"? What happens? Also, you are declaring nUserLen as Long and the variable nSize as Integer. They should both be the same type as each other (either type is fine, but you don't really need it to be as big as a Long). You might consider making the buffer be bigger than 32 characters, because if the UserName happens to be longer than this (but it generally isn't) the function will fail. When you are done calling this function you should do:
strUserName=Left(strUserName, nUserLen)
This will cut off the unnecessary bytes.
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi -
What do you mean by "it doesn't work"? What happens? Also, you are declaring nUserLen as Long and the variable nSize as Integer. They should both be the same type as each other (either type is fine, but you don't really need it to be as big as a Long). You might consider making the buffer be bigger than 32 characters, because if the UserName happens to be longer than this (but it generally isn't) the function will fail. When you are done calling this function you should do:
strUserName=Left(strUserName, nUserLen)
This will cut off the unnecessary bytes.
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhijdunlap wrote: What do you mean by "it doesn't work"? What happens? I mean it just returns 0 for failure, the string contains the 32 spaces that I initilialized it with and nUserLen is still 32. Oddly enough, when I call "GetLastError" afterwards to find out the exact reason it failed, that too returns 0, for no error! :~ jdunlap wrote: Also, you are declaring nUserLen as Long and the variable nSize as Integer I have tried all combinations of integer/long with the exact same results. Just above this section of code (in the same sub actually), I am calling another API function, "GetPrivateProfileString", and it works just fine. I can also call "GetUserName" from C++ and it works as expected. I am wondering if there's some other library or something that I need instead of kernel32, as we are on a Novell network here and not the standard MS networking... It's still strange how it works from C++ though... :confused: jdunlap wrote: You might consider making the buffer be bigger than 32 characters Yeah, you're probably right, but since my user name is only 3 characters I figured it would be big enough for testing... ;)
There are 10 kinds of people - those that get binary and those that don't.
-
jdunlap wrote: What do you mean by "it doesn't work"? What happens? I mean it just returns 0 for failure, the string contains the 32 spaces that I initilialized it with and nUserLen is still 32. Oddly enough, when I call "GetLastError" afterwards to find out the exact reason it failed, that too returns 0, for no error! :~ jdunlap wrote: Also, you are declaring nUserLen as Long and the variable nSize as Integer I have tried all combinations of integer/long with the exact same results. Just above this section of code (in the same sub actually), I am calling another API function, "GetPrivateProfileString", and it works just fine. I can also call "GetUserName" from C++ and it works as expected. I am wondering if there's some other library or something that I need instead of kernel32, as we are on a Novell network here and not the standard MS networking... It's still strange how it works from C++ though... :confused: jdunlap wrote: You might consider making the buffer be bigger than 32 characters Yeah, you're probably right, but since my user name is only 3 characters I figured it would be big enough for testing... ;)
There are 10 kinds of people - those that get binary and those that don't.
Declare Function _
GetUserName Lib "Advapi32.dll" Alias "GetUserNameA" _
(ByRef ByVal lpBuffer As String, ByRef nSize As Integer) As LongIf you pass it ByVal, then VB does the marshalling for you, and it acts like a ByRef byte array. VB does unicode BSTRs, so if you pass it ByRef, it passes the unicode BSTR, which Windows doesn't understand. :)
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi -
Declare Function _
GetUserName Lib "Advapi32.dll" Alias "GetUserNameA" _
(ByRef ByVal lpBuffer As String, ByRef nSize As Integer) As LongIf you pass it ByVal, then VB does the marshalling for you, and it acts like a ByRef byte array. VB does unicode BSTRs, so if you pass it ByRef, it passes the unicode BSTR, which Windows doesn't understand. :)
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi