Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get login name/user name

Posted on 2001-06-18
20
Medium Priority
?
217 Views
Last Modified: 2008-01-16
I can get the login name by using either:

Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

or..

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Both works fine when executed. My problem is that I want to execute my program (exe-file) from a login script (NT or Novell) and write the login name to a file.

All I get is an empty string. Looks like Windows can't return the info this early in the startup process.

The main reason for running the code from a login script, is that I don't have to install any program on the clients.

The program works fine if I launch it from the Startup-folder.

Any suggestions? Is there other ways to return the login name?

Regards,
Christopher
0
Comment
Question by:cvogt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +4
20 Comments
 
LVL 5

Expert Comment

by:rkot2000
ID: 6204068
try to use sleep function to delay your exe
0
 
LVL 5

Expert Comment

by:rkot2000
ID: 6204079
did you try net name?
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6204083
For NT, the username is available in an Environment variable USERNAME. There are also USERDOMAIN and USERDNSDOMAIN variables. You really don't need to use a VB program to get this information.


To use these from a batch file, just surround the variable name with %, like this:

echo off
echo %USERDOMAIN%\%USERNAME% >> C:\LOGFILE.LOG

Tell me a bit more about what you need to do with the user name.

Gary
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:gbaren
ID: 6204085
By the way, this information exists directly in Novell login scripts. It's been over six years since I administered a Novell server, though. Don't remember what the variables are called anymore.
0
 

Author Comment

by:cvogt
ID: 6204122
I know about the login script variables. I should have told you: This is part of a sysinfo program that log's all kind of information - memory size, free disc space, installed software etc. So I think I have to use an .exe file rather than a batch file.

The only thing I can't get at startup is the login name (and for some strange reasons: on some computers I can't get the network adapters MAC address or Ethernet address).
0
 

Author Comment

by:cvogt
ID: 6204182
I know about the login script variables. I should have told you: This is part of a sysinfo program that log's all kind of information - memory size, free disc space, installed software etc. So I think I have to use an .exe file rather than a batch file.

The only thing I can't get at startup is the login name (and for some strange reasons: on some computers I can't get the network adapters MAC address or Ethernet address).
0
 

Author Comment

by:cvogt
ID: 6204234
I know about the login script variables. I should have told you: This is part of a sysinfo program that log's all kind of information - memory size, free disc space, installed software etc. So I think I have to use an .exe file rather than a batch file.

The only thing I can't get at startup is the login name (and for some strange reasons: on some computers I can't get the network adapters MAC address or Ethernet address).
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6204386
Have you tried

     Environ("Username")
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6204500
cv,

You can pass the userid on the command line. From the Novell login script, you can pass the MAC ID as well.

Alternatively, you can add your EXE to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\


Gary
0
 
LVL 3

Expert Comment

by:jcgd
ID: 6205460
You can run a batch file in login script (NOVELL) that execute "whoami > c:\nnnnn.nn"? Read and parse this file. Put a whoami.exe in Login directory.
0
 

Expert Comment

by:MB
ID: 6207559
How about converting your exe to an NT Service?
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6207765
What the...

NEVER POST AN ANSWER

newbies...
0
 

Author Comment

by:cvogt
ID: 6212150
Thanks for all comments. I am trying the login script approach and I have had some luck logging on a win9x PC on a Netware server, setting environment variables in the script and reading them in my prog using the Environ statement.

I am not that experienced with login scripts on NT. Note: I am logging win9x PC's onto a win NT/2000 server. How do I set environment variables in the login script? In Netware I use this statments:

Set name="%LOGIN_NAME"
Set MACaddress = "%P_STATION"

Can I do this in NT? I have tried writing something like:

Set name=%USERNAME% or Set name="%USERNAME%"
I have also tried the suggestion from gbaren: ECHO %USERNAME% (with and without the piping to a file), but it seems that the %USERNAME% variable is empty. I am logging in using the standard Client for Microsoft Networks.

Any comments on this matters?
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6212182
CV,

Open the command prompt and type SET. You'll get a list of all defined environment variables.

You don't need to set another environment variable, either:

1. Read the existing environment variable from your program like this: sUser = Environ("USERNAME")

2. Change your program to accept a parameter and pass the environment variable like this: YOURAPP.EXE %USERNAME%

Gary

By the way, you should reject the proposed answer because it has locked this thread and removed it from the open thread list.
0
 

Author Comment

by:cvogt
ID: 6213225
Gary

If I type Set on a dos prompt ON A WIN98 computer (logged on tho a NT 4.0 server) I see just a few variables (Path, sound card etc. not username or MAC address)

If I open a dos prompt ON THE NT SERVER itself I see them. But I am logging the WIN98 computer, not the Server. Is there something missing in the setup in the environment I am testing?

0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6214982
If you want to set all of the Environ parameters do this,

    for a = 1 to 30
        debug.print a & " - " & envrion(a)
    next a

This will give you a list with their corresponding number
The return will contain the parameter name. The format will be like this

    27 - USERNAME=hornet241
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6214988
Oops, mispelled Environ in the above comment
0
 
LVL 5

Accepted Solution

by:
gbaren earned 800 total points
ID: 6215124
OK, if you're on a 9x client, you won't have those locally. Here's a table from a help file Logon Script topic:

Parameter Description
 
%HOMEDRIVE% The user's local workstation drive letter connected to the user's home directory

%HOMEPATH% The full path of the user's home directory

%OS% The operating system of the user's workstation

%PROCESSOR_ARCHITECTURE% The processor type (such as 80386) of the user's workstation

%PROCESSOR_LEVEL% The processor level of the user's workstation

%USERDOMAIN% The domain containing the user's account

%USERNAME% The user name


These are all supposed to be available from the Logon Script. I believe the Logon Script runs on the server itself, not on the client. Which is probably why SET name=%USERNAME% doesn't work -- you're setting another variable on the server

Use the parameter approach. Change your app to accept a parameter and call it with  "app.exe %USERNAME%"

0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6918907
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20137543.html
http://www.experts-exchange.com/questions/Q.20171666.html
http://www.experts-exchange.com/questions/Q.20274820.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6922231
 ....~~~~~:)-> Thanks for returning and finalizing this. <=(:~~~~~~~....

        Complete the NEW USER SURVEY for additional points.  Look here:
             http://www.experts-exchange.com/jsp/memberEarnPoints.jsp

                 >>>>>>>> Moondancer - EE Moderator <<<<<<<<<
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question