flat assembler
Message board for the users of flat assembler.

Index > Windows > Cannot set environment variable!

Author
Thread Post new topic Reply to topic
EaxRax



Joined: 20 Jul 2013
Posts: 15
EaxRax
Wrote a small program to set the environment variable, but it does not install it. Why can't I set an environment variable? What am I doing wrong?
Code:
include 'C:\Program Files\FASM\INCLUDE\win32a.inc'

format PE Console 4.0
entry  Start

section '.text' code readable executable

Start:


        nop

        invoke  SetEnvironmentVariableA, _data.EnvironmentName, _data.EnvironmentContents

      ; check for an error.
        test   eax, eax
        jz     Exit

        invoke  GetEnvironmentStrings

        nop


Exit:
  invoke  ExitProcess, 0

section '.data' data readable writeable

  _data.EnvironmentName:      db  'Path', 0
  _data.EnvironmentContents:   db  'C:\Program Files\ENVAR', 0

section '.idata' import data readable

  library kernel, 'KERNEL32.DLL'

  import kernel,\
    SetEnvironmentVariableA, 'SetEnvironmentVariableA',\
    GetEnvironmentStrings, 'GetEnvironmentStrings',\
    ExitProcess, 'ExitProcess'    
It may be necessary to prescribe something in the registry?


Description: Application source.
Download
Filename: ENVAR.7z
Filesize: 604 Bytes
Downloaded: 140 Time(s)

Post 18 Aug 2013, 15:19
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
How did you see that it does not work?
Maybe need GetEnvironmentStringsA?
Post 18 Aug 2013, 15:35
View user's profile Send private message Send e-mail Reply with quote
EaxRax



Joined: 20 Jul 2013
Posts: 15
EaxRax
AsmGuru62 wrote:
How did you see that it does not work?
See screen shot, below!
AsmGuru62 wrote:
Maybe need GetEnvironmentStringsA?
It is possible and so, but it will change nothing.


Description:
Filesize: 135.77 KB
Viewed: 1844 Time(s)

Envar_screenshot.png


Post 18 Aug 2013, 16:36
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
I see... you were hoping to change the OS environment variables.
Please read this:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx

It describes where in the registry the environment is located.
So, basically, yes, you need to write into Registry.
Post 18 Aug 2013, 17:09
View user's profile Send private message Send e-mail Reply with quote
EaxRax



Joined: 20 Jul 2013
Posts: 15
EaxRax
Thanks, now I will read!
Post 18 Aug 2013, 17:18
View user's profile Send private message Reply with quote
EaxRax



Joined: 20 Jul 2013
Posts: 15
EaxRax
I read article. Written, need to make changes to a registry key "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\ Environment". Has looked for functions working with the registry on MSDN. Has found these functions, but so it is a lot of them, that eyes run up. And i do not know, what function should use.

Can anyone knows which function to change an environment variable in the registry?

Prompt me, please!
Post 19 Aug 2013, 17:29
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
This tested (and worked) on XP SP3.
I am not checking any of return codes - just ran it in OLLYDBG and looked
into Environment. New variable appeared OK.
Code:
    lpSubKey  db 'System\CurrentControlSet\Control\Session Manager\Environment',0
    lpValue   db 'MyFreshVar32',0
    bufData   db 'THIS VARIABLE HAS BEEN ADDED BY FASM!',0
    HKey      dd 0

...

    invoke    RegOpenKeyExA, HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_SET_VALUE, HKey
    mov       ebx, [HKey]
    mov       esi, bufData

    invoke    lstrlenA, esi
    inc       eax

    invoke    RegSetValueExA, ebx, lpValue, 0, REG_SZ, esi, eax
    invoke    RegCloseKey, ebx

...

; ---------------------------------------------------------------------------
section '.idata' import data readable writeable

    library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL',advapi32,'ADVAPI32.DLL'

    include 'API\Kernel32.Inc'
    include 'API\User32.Inc'
    include 'API\Gdi32.Inc'
    include 'API\AdvApi32.Inc'      ; <-- this one needed to work with Registry

    
Post 19 Aug 2013, 18:42
View user's profile Send private message Send e-mail Reply with quote
EaxRax



Joined: 20 Jul 2013
Posts: 15
EaxRax
AsmGuru62 That's it! Very Happy Code works perfectly! Very Happy Thank you very much! Rolling Eyes Very Happy

P.S. Questions on this topic no more!
Post 19 Aug 2013, 20:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.