Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Calling API function from VB

Calling API function from VB

Scheduled Pinned Locked Moved Visual Basic
c++json
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Miszou
    wrote on last edited by
    #1

    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 Long

    Dim strUserName As String
    Dim nUserLen As long
    Dim nRetVal as long

    nUserLen = 32
    strUserName = Space(nUserLen)

    retVal = GetUserName(strUserName, nUserLen)

    :confused:


    There are 10 kinds of people - those that get binary and those that don't.

    J 1 Reply Last reply
    0
    • M Miszou

      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 Long

      Dim strUserName As String
      Dim nUserLen As long
      Dim nRetVal as long

      nUserLen = 32
      strUserName = Space(nUserLen)

      retVal = GetUserName(strUserName, nUserLen)

      :confused:


      There are 10 kinds of people - those that get binary and those that don't.

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • J J Dunlap

        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

        M Offline
        M Offline
        Miszou
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • M Miszou

          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.

          J Offline
          J Offline
          J Dunlap
          wrote on last edited by
          #4

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

          If 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

          M 1 Reply Last reply
          0
          • J J Dunlap

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

            If 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

            M Offline
            M Offline
            Miszou
            wrote on last edited by
            #5

            Thanks! It works fine now :)


            There are 10 kinds of people - those that get binary and those that don't.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups