Daniel Pereira
Daniel Pereira

Reputation: 25

Getting the current working directory in Assembly

I'm working on an assembly code using Flat Assembler that would read a value from an .ini file, and, in order to do that, I'm trying to invoke the Kernel 32.dll GetPrivateProfileInt function.

There is only one problem with that: in order to properly read the file, I need to pass the full path of the .ini as a parameter to this function. I have tried to pass '.\config.ini' as a parameter and I have also tried to use MAX_PATH/rb MAX_PATH to get the full working directory (which is sometimes valid in FASM), but that didn't work either...

If anyone could help me, I would be grateful!

Here is my current code:

[...]

invoke GetPrivateProfileInt,.secname,.keyname,-1,.inifile

cmp eax,1
je .start

invoke   MessageBoxA,0,.inifile,.secname,MB_ICONERROR

 [...]

.inifile: db '.\config.ini',0
.secname: db 'config',0
.keyname: db 'advanced',0

(Note: this messagebox code is just something I included in order to know if that function was actually reading the value from the config.ini)

And the .ini file I'm trying to read:

[config]
advanced=1

Again, if anyone could help me, I would be grateful!

Upvotes: 1

Views: 430

Answers (1)

johnfound
johnfound

Reputation: 7061

Actually, this behavior is described in MSDN.

lpFileName [in]

The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

If you want to use exactly the current working directory (which is not always the one where the executable file resides), use GetCurrentDirectory API to get the current working directory and then append the configuration file after this string.

But usually you want to read the configuration file from the directory where the executable file is placed.

In these cases I use something like the following:

.aminitwindow:
; Create string with the filename of the INI file.

        lea     ebx, [.str]
        invoke  GetModuleFileNameA, NULL, ebx, 512
        mov     ecx, eax

.findloop:
        dec     eax
        js      .notfound

        cmp     byte [ebx+eax], '.'
        je      .found
        jmp     .findloop

.notfound:
        mov     eax, ecx
.found:
        mov     dword [ebx+eax], '.cfg'
        mov     byte [ebx+eax+4], 0

        lea     esi, [eax+16]

Here I construct the name of the configuration file, simply changing the extension of the executable file (from .exe to .cfg). If you want to use different name, simply scan back to the first "\" character and then add the whole filename of your config file.

Upvotes: 1

Related Questions