Solved

Get login name/user name

Posted on 2001-06-18
20
205 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
  • 6
  • 5
  • 3
  • +4
20 Comments
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
try to use sleep function to delay your exe
0
 
LVL 5

Expert Comment

by:rkot2000
Comment Utility
did you try net name?
0
 
LVL 5

Expert Comment

by:gbaren
Comment Utility
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
 
LVL 5

Expert Comment

by:gbaren
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Have you tried

     Environ("Username")
0
 
LVL 5

Expert Comment

by:gbaren
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Expert Comment

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

Expert Comment

by:gbaren
Comment Utility
What the...

NEVER POST AN ANSWER

newbies...
0
 

Author Comment

by:cvogt
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Oops, mispelled Environ in the above comment
0
 
LVL 5

Accepted Solution

by:
gbaren earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
 ....~~~~~:)-> 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now