how to know the current user name

good morning, I would ask gently a question about a c++ code. I want to create and write on a .bat file with c++. This file must be located in the startup folder of windows but for that I had to know the name of the current user in order to write FILE.open("C:\\Users\\nameUsers\\...") but I don't know nameUser of all machines. I would ask if exist a function to obtain that or if other ways exist to do what I need. Infact I'm interesting to know how programmers create installer programs which create files in some folders of my machine without knowing the name of the user. Secondly I would ask if some examples for that exist.
Thank you very much
Best Regards
To get the name of the current logged on user
https://msdn.microsoft.com/de-de/library/windows/desktop/ms724432(v=vs.85).aspx


It seems there are 2 startup folders - at least on Windows 7

C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx

how programmers create installer programs which create files in some folders of my machine without knowing the name of the user.
They probably find out - it's quite easy to do.
Thank you really much for your answer...I try immediately.
Thank you
closed account (E0p9LyTq)
DS92 wrote:
how programmers create installer programs which create files in some folders of my machine without knowing the name of the user.


They probably used Windows Installer, a service included with Windows.
https://msdn.microsoft.com/en-us/library/windows/desktop/cc185688(v=vs.85).aspx

Windows Installer can install applications as well as patching already installed applications.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa367449(v=vs.85).aspx

There are companies that create an installer creation application that makes entering all the needed information easier for the programmer, InstallShield being one.
http://www.flexerasoftware.com/producer/products/software-installation/installshield-software-installer/
Thomas tried to help me with this but unsuccessfully because of error :( : http://www.cplusplus.com/forum/windows/193326/
Last edited on
Thomas tried to help me with this but unsuccessfully because of error :(


Problem was that you were not willing or smart enough to google what include file you needed for sprintf_s.
closed account (E0p9LyTq)
@prako2,

You didn't create this topic, quit hijacking others' questions.
Problem was that you were not willing or smart enough to google what include file you needed for sprintf_s.

Thanks for reply, I googled it but https://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx refers
that necessary headers are:
sprintf_s, _sprintf_s_l
C: <stdio.h>
C++: <cstdio> or <stdio.h>
(Scroll down to requirements section) that I have already added and still doesn't work.
Last edited on
To obtain the path to the startup folder you should use the WinAPI call

SHGetKnownFolderPath()
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188(v=vs.85).aspx

Passing FOLDERID_Startup as the KNOWNFOLDERID will obtain the path to the current user's startup folder, FOLDERID_CommonStartup will give you the path of the shared startup folder. You should not try to concoct the path by querying for the username, etc.

The link that Thomas1965 provided is to the deprecated SHGetFolderPath() call, which should only be used if you have to support pre-Vista systems.

Andy
Last edited on
Thanx, you enlighted me
Topic archived. No new replies allowed.