The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Jackal on September 06, 2007, 05:52:53 AM

Title: Environment variables
Post by: Jackal on September 06, 2007, 05:52:53 AM
i was wondering if someone can help me get the path stored in an environment variable. For instance i was the path of %USERPROFILE% so i can create a desktop shortcut. My idea would be that %USERPROFILE% will make it easy with the different OS's since xp uses documents and settings but on vista it has a users folder instead. %USERPROFILE%\desktop does open the desktop folder on my vista pc and if i remember right that should work on xp aswell.
Title: Re: Environment variables
Post by: RotateRight on September 06, 2007, 06:53:47 AM
Jackal,

If I understand your question, I believe to get what you
want, a simple call to the c runtime function getenv will
do it.

As they say "pardon my French" (meaning this is in Fasm).


;**************
;* GetEnv.asm *
;**************

format pe gui 4.0
entry start

include 'win32ax.inc'

.code

proc start
        cinvoke getenv,"USERPROFILE"
        invoke  MessageBox,0,eax,"GetEnv: UserProfile",0
        invoke  ExitProcess,0
endp

data import

  library kernel32,'kernel32.dll',\
          user32,'user32.dll',\
          msvcrt,'msvcrt.dll'

  import kernel32,\
         ExitProcess,'ExitProcess'

  import user32,\
         MessageBox,'MessageBoxA'

  import msvcrt,\
         getenv,'getenv'

end data
Title: Re: Environment variables
Post by: RotateRight on September 06, 2007, 07:07:02 AM
Check this site to see if the OS you
want the environment variable for
is supported:

http://www.scriptlogic.com/support/CustomScripts/environmentVariableReference.html

Also see:

DWORD WINAPI GetEnvironmentVariable(
  LPCTSTR lpName,
  LPTSTR lpBuffer,
  DWORD nSize
);

in kernel32 if you don't want to use msvcrt.
Title: Re: Environment variables
Post by: Jackal on September 06, 2007, 07:21:25 AM
thanks.. while you posted that last message i tried getenv but it didnt work right away but led me to GetEnvironmentVariable and i was able to get it working. Again thanks and ill check out that site.
Title: Re: Environment variables
Post by: sinsi on September 06, 2007, 07:36:32 AM
Have a look at SHGetSpecialFolderPath as well - works with 95/NT4 (with IE4) and 98/2000 or better
Title: Re: Environment variables
Post by: Jackal on September 06, 2007, 09:56:11 PM
I do happen to like that better as %USERPROFILE% is only valid in 2000 or better where i can use this in pre windows versions and it does work on my vista aswell even though vista has changed from csidl to knowfolderid's but allows for the csidl id for backwards compatibility.

         Invoke SHGetSpecialFolderPath, hWnd, addr DeskBuffer, CSIDL_DESKTOPDIRECTORY, 0

Thanks for the info... Worked like a charm aswell :)