News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Environment variables

Started by Jackal, September 06, 2007, 05:52:53 AM

Previous topic - Next topic

Jackal

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.

RotateRight

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

RotateRight

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.

Jackal

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.

sinsi

Have a look at SHGetSpecialFolderPath as well - works with 95/NT4 (with IE4) and 98/2000 or better
Light travels faster than sound, that's why some people seem bright until you hear them.

Jackal

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 :)