I am having trouble with the following script:

.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib ;adding all my libs

.data
nsv db "APPDATA", 0 ;i want to use this in getenv();
fnsv db 260 dUP(0) ; char fnsv[260];
ffn db 260 dUP(0) ; char ffn[260];
mbt db "Test", 0 

tl db "getenv", 0 ;to call this later

.code
start:


push offset nsv ;pushing APPDATA 
push 260 ;pushing the max size? 260
call tl ;calling getenv

;getenv("APPDATA") should be stored in eax now

mov ffn, eax      ;move the path into char ffn[260];



invoke MessageBox, NULL, addr fnsv, addr mbt, MB_OK + MB_ICONINFORMATION ;show a msgbox displaying path

invoke ExitProcess, NULL ;exit

end start

I get these errors:
C:\Users\Canada FTW\Desktop\nsv.asm(26) : error A2024: invalid operand size for
instruction
C:\Users\Canada FTW\Desktop\nsv.asm(28) : error A2070: invalid instruction opera
nds


I am new to mASM, so I am not really sure what the problem would be. I left a few notes so you guys could understand what I was doing. I am not really sure how to fix this, can you even call getenv in assembly? or would it be more difficult than this?

char *ffn = getenv("APPDATA"); but in massembly. This script was made from looking at the following script in ollydbg.

#include <windows.h>

int main()
{
char *path = getenv("APPDATA");
}

You don't need line 18 or line 25 as getenv only takes one argument, that being a string pointer.

I generally like using kernel32 library functions and in this case may work for you

GetEnvironmentVariable

The GetEnvironmentVariable function retrieves the value of the specified variable from the environment block of the calling process. The value is in the form of a null-terminated string of characters.

DWORD GetEnvironmentVariable(
LPCTSTR lpName, // address of environment variable name
LPTSTR lpBuffer, // address of buffer for variable value
DWORD nSize // size of buffer, in characters
);

Line 26 would be replaced with

invoke GetEnviromentVariable, lpName, lpBuffer, nSize

or

push    nSize
        push    lpBuffer
        push    lpName
        call      GetEnvironmentVariable

You don't need line 18 or line 25 as getenv only takes one argument, that being a string pointer.

I generally like using kernel32 library functions and in this case may work for you

GetEnvironmentVariable

The GetEnvironmentVariable function retrieves the value of the specified variable from the environment block of the calling process. The value is in the form of a null-terminated string of characters.

DWORD GetEnvironmentVariable(
LPCTSTR lpName, // address of environment variable name
LPTSTR lpBuffer, // address of buffer for variable value
DWORD nSize // size of buffer, in characters
);

Line 26 would be replaced with

invoke GetEnviromentVariable, lpName, lpBuffer, nSize

or

push    nSize
        push    lpBuffer
        push    lpName
        call      GetEnvironmentVariable

Thank you so much, GetEnvironmentVariable solved my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.