GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo. ** SOLVED **

Just starting out? Need help? Post your questions and find answers here.
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo. ** SOLVED **

Post by CONVERT »

Hello,

Windows 10 last version, Purebasic 6.01

Code: Select all

Debug "userprofile=" + GetEnvironmentVariable("USERPROFILE")
gives:
userprofile=C:\Users\Travail
CMD command gives, in the same session, of course:
c:\>echo %USERPROFILE%
C:\Users\jeanc
I am logged as jeanc.

Travail is an admin account. jeanc is a normal user account.

I don't understand why it does not give the same result.
I try other environment variables, they work.

Please give me an explanation, if possible.
Thanks a lot.
Last edited by CONVERT on Mon Mar 20, 2023 11:00 am, edited 1 time in total.
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
User avatar
idle
Always Here
Always Here
Posts: 5050
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo.

Post by idle »

What about?

Code: Select all

Debug UserName()
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo.

Post by CONVERT »

You're right. Debug UserName() gives Travail (the admin one) and it's the same for GetEnvironmentVariable("USERNAME").

RunProgram("CMD") gives an admin windows command, and a direct launch of CMD from Windows gives an user Windows command.

Of course, if I launch from Windows CMD as admin (giving the password), I get the same result as RunProgram("CMD") in Purebasic.

The curious thing is that RunProgram("CMD") does not need admin password.
And the purebasic program is able to write under C:\Users\Travail\ (the admin one).

There is a security problem in my Windows 10 Pro. I am able to create a directory under C:\Users\Travail\ (the admin one) directly from Windows when I am logged with a normal user (jeanc).

Code: Select all

Debug "GetEnvironmentVariable('USERPROFILE')=" + GetEnvironmentVariable("USERPROFILE")
Debug "GetEnvironmentVariable('USERNAME')=" + GetEnvironmentVariable("USERNAME")
Debug "Username()=" + UserName()

RunProgram("CMD")
CMD launched from Purebasic gives admin environment variables:

Image

CMD launched from Windows logged with normal user (jeanc) gives:

Image
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo.

Post by BarryG »

Did you ever rename your profile at any time?
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo.

Post by CONVERT »

Of course, all the tests above are done with a normal user session (jeanc).

Purebasic launched with a normal user session seems to run with an admin session.
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo.

Post by CONVERT »

And the PB shortcut does not launch it as administrator:

Image
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: GetEnvironmentVariable("USERPROFILE") *** SOLVED ***

Post by CONVERT »

I remembered that I run PureBasic_Windows_X64_LTS_6.01_(xxxxxx).exe as admin, in a normal user session.

I uninstall it.

I run again PureBasic_Windows_X64_LTS_6.01_(xxxxxx).exe normally, in a normal user session, waiting for the admin password prompt.

No, it works fine.

Code: Select all

Debug "GetEnvironmentVariable('USERPROFILE')=" + GetEnvironmentVariable("USERPROFILE")
Debug "GetEnvironmentVariable('USERNAME')=" + GetEnvironmentVariable("USERNAME")
Debug "Username()=" + UserName()

RunProgram("CMD")
gives the good current user, and not the admin user,
and SET in the CMD windows launched by PB gives the good information for the current user.

Sorry to have disturbed you for nothing...
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo. ** SOLVED **

Post by BarryG »

You didn't disturb for nothing - it was a good lesson for us.
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo.

Post by CONVERT »

BarryG wrote: Mon Mar 20, 2023 9:50 am Did you ever rename your profile at any time?
No, never. Travail was created as admin, and jeanc as normal user, and the two ones were never renamed.
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: GetEnvironmentVariable("USERPROFILE") does not give the same result as CMD echo. ** SOLVED **

Post by CONVERT »

BarryG wrote: Mon Mar 20, 2023 11:26 am You didn't disturb for nothing - it was a good lesson for us.
Thanks :)
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
Post Reply