Windows User Name

ajetrumpet

Banned
Local time
Today, 08:37
Joined
Jun 22, 2007
Messages
5,638
Folks,

What does the line strUserName = String$(254, 0) do in the code? What is the dollar sign for? What does it do? Why, when I try to extract the windows user name from my machine, do I get an error that tells me the String function is not in the library? Thank you...
Code:
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
    "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    [COLOR="Red"]strUserName = String$(254, 0)[/COLOR]
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If ( lngX > 0 ) Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = vbNullString
    End If
End Function


EDIT

A picture is attached with an interesting error when I was testing Environ("username") in the immediate window...
 

Attachments

  • YMMAPI error.jpg
    YMMAPI error.jpg
    108.5 KB · Views: 191
Last edited:
Folks,

What does the line strUserName = String$(254, 0) do in the code? What is the dollar sign for? What does it do? Why, when I try to extract the windows user name from my machine, do I get an error that tells me the String function is not in the library? Thank you...

It puts 254 bytes with all the bits reset into the variable strUserName. No doubt, this is so the API call you're making will be forced to return a NULL terminated string (the "z" in sz). This is the cheap VB way of doing a C memset.

The $ says to return a string instead of a variant.

I don't get any error when using the String() function (with or sans the $). Do you have all your references set correctly?
 
I just noticed your screen shot. Part of your problem looks like you don't have the right library (with the advapi32.dll) included in your references (that shouldn't impact String() though).
 
George,

Which library is that DLL file in? I have NO idea...is it the library that is in the screenshot? From what I've read about it, it could be a malware file disguised with that name. But, if it IS legit, I've also read that it really should belong to YAHOO!.
 
I'm a little lost. I can't see the file you're talking about.

And it is difficult for me to tell the difference between anything built into Vista and malware. They both behave about the same way to me.
 
What I mean is THIS:

DLL files that are used for API purposes are usually included in a folder of files that are collectively called a LIBRARY, is that correct? If it is, where in world do I find the folder (or reference in the Visual Basic reference list) that contains the DLL files called advapi32.dll!? ;) Thanks George. (I have NO idea, as all windows versions are HUGE with regard to the number of files!)


EDIT

Another thing I would like to ask you is why, on some of my Access databases, I cannot access my "right-click" editing menu on all of my objects (like OPEN, SAVE, DELETE, etc...). I did not change any settings...
 
A "library" is a group of programs that can be used by other programs. A DLL (Dynamic Link Library) is a library all by itself. DLLs were created so programs didn't have to have libraries linked into new programs at compile time (thus the moniker "Dynamic").

Typically, Windows likes it's libraries stored in the c:\Windows\System or, more commonly, c:\Windows\System32 directories (or sometimes in one of their sub-directories), though they could be anywhere if you tell your program where they are located (that's the purpose of the "browse" button on the references popup).

It looks like you may have taken code from somewhere else that uses a library (DDL file) that you don't have installed or that you need to supply a path to.

As far as your other problem, I don't follow you. Can you post a screen shot showing what you expect to happen and another showing it not happening?
 
here is a picture of the right click working to bring down the shortcut menu with selected objects. In some of my own databases, I cannot do this. A right click will do absolutely nothing. It will not bring up a menu...
 

Attachments

  • right click.jpg
    right click.jpg
    100.4 KB · Views: 165
And to be sure, those database that do not work are your own, without any security and menus/shortcuts/context menu enabled (Tools->StartUp -> Dialog box with four checkboxes)?
 
Yes Banana. As a matter fact, I think I've just checked that. I will check it again.
 
If indeed so, I would suspect a corruption.... perhaps importing into a fresh, blank database?
 
I'd go so far as to say you have something corrupting things. My first suspicion is Vista, but it may be something else.

Do you have security turned off in Vista?
 
Yes. Security is turned off. I have something on here. I know it. i cannot update my firewall. Take a look at the following JPEG. The message popped up when I tried to update the Windows Defender definitions. It has been like this for several months now...


(I suspect Malware, but really have no idea)
If indeed so, I would suspect a corruption.... perhaps importing into a fresh, blank database?
This will not work Banana, as my real problem with the database issues in Access lies here with the DLL error JPEG that I posted up.
 

Attachments

  • Windows Defender.jpg
    Windows Defender.jpg
    98.3 KB · Views: 152
Vista is really the biggest piece of garbage since...well, I don't know what since. I met with MS reps a little over a month ago and they were all like "Vista is great, no problems with it!" My contacts at another large company who are being forced by Microsoft into forcing us to use Vista (on every new PC) were not quite as enthusiastic.
 
Does this mean that there is no fix to my problem George? It seems like corruption to me really, as Banana has said...
 
Just to add my 2c ... you don't need to have the .DLL referenced in your list of references to use fOSUsername ... you can simply DECLARE the function you wish to use out of the DLL ...

Code:
Private Declare Function [COLOR=purple]apiGetUserName[/COLOR] Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
 
Public Function fOSUserName() As String
' Returns the network login name
 
    Dim lngLen As Long, lngX As Long
    Dim strUserName As String
 
    'Fill the a series of bytes
    strUserName = String$(254, 0)
    lngLen = 255
 
    'Run the API. The API Function returns the number
    'of characters that were dumped into the empty
    'string of 255 bytes.
    lngX = [COLOR=purple]apiGetUserName[/COLOR](strUserName, lngLen)
 
    'Get the string that has been dumped in the
    'series of bytes.
    If lngX <> 0 Then
        fOSUserName = Left$(strUserName, lngLen - 1)
    Else
        fOSUserName = ""
    End If
 
End Function

I know this does not relate to what appears to be some sort of file corruption issue on your PC ... but I thought this info might add something for when you recover.
 
ok a side notice about the initial problem of this thread , there are two versions of this popular script , the first is the one posted above the second is one almost identical but tend to retrieve computer name . now why the author has limited retrieved field to 255 charachter was a mere trial to keep the string from generating errors apon inserts statments as network names can sometime exceed access restricted field lengths of 255 charachters.
 

Users who are viewing this thread

Back
Top Bottom