View Full Version : Windows User Name
ajetrumpet 09-27-2008, 01:36 PM 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...
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
strUserName = String$(254, 0)
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...
georgedwilkinson 09-27-2008, 01:51 PM 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?
georgedwilkinson 09-27-2008, 01:55 PM 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).
ajetrumpet 09-27-2008, 02:05 PM 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!.
georgedwilkinson 09-27-2008, 02:10 PM 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.
ajetrumpet 09-27-2008, 02:15 PM 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...
georgedwilkinson 09-27-2008, 03:37 PM 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?
ajetrumpet 09-27-2008, 03:49 PM 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...
Banana 09-27-2008, 04:09 PM 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)?
ajetrumpet 09-27-2008, 04:31 PM Yes Banana. As a matter fact, I think I've just checked that. I will check it again.
Banana 09-27-2008, 04:35 PM If indeed so, I would suspect a corruption.... perhaps importing into a fresh, blank database?
georgedwilkinson 09-27-2008, 05:17 PM 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?
ajetrumpet 09-27-2008, 05:34 PM 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 (http://www.access-programmers.co.uk/forums/showthread.php?t=156838) with the DLL error JPEG that I posted up.
georgedwilkinson 09-27-2008, 05:48 PM 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.
ajetrumpet 09-27-2008, 05:57 PM Does this mean that there is no fix to my problem George? It seems like corruption to me really, as Banana has said...
datAdrenaline 10-14-2008, 12:01 PM 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 ...
Private Declare Function apiGetUserName 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 = apiGetUserName(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.
nIGHTmAYOR 10-14-2008, 01:36 PM 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.
|
|