ASM call DLL functions (win32) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ASM call DLL functions (win32)

xkgdiamxkgdiam Posts: 111Member
[code]
i have an hlib=LoadLibrary(...) object, and an
hproc=GetProcAddress(...,..) one.
i tried to call the 'hproc' throught inline assembly code,
but failed,(rather i dont know how to).
here is my code :
int main(int argc,char *argv[]){
HINSTANCE hdll;
FARPROC hproc;
char msg[]="This is my message !";
char tle[]="test";
hdll=LoadLibrary("dll.dll");
//do the test if NULL
hproc=GetProcAddress(hdll,"_MyMsgBox");
//do the test if NULL

hproc(0,msg,tle,0); //that works ok ....
asm { .386 //put them here to force
.model flat //code to work
push 0
push offset tle
push offset msg
push 0
call hproc //i try, call [hproc] ,too
}
FreeLibrary(hdll);
return(1);
}
[/code]
Can anyone help me, with this above ?

Comments

  • xkgdiamxkgdiam Posts: 111Member
    [b][red]This message was edited by xkgdiam at 2003-12-14 14:32:48[/red][/b][hr]
    [code]
    :: hproc(0,msg,tle,0); //that works ok ....
    :: asm { .386 //put them here to force
    :: .model flat //code to work
    :: push 0
    :: push offset tle
    :: push offset msg
    :: push 0
    :: call hproc //i try, call [hproc] ,too, but fails
    :: }
    :: Can anyone help me, with this above ?
    [hr]
    due an error in my code, my editor open the disassembling src of
    my code and i found how C disassmble 'hproc(0,msg,tle,0);'

    push 0
    lea eax,dword ptr tle
    push eax
    lea eax,dword ptr msg
    push eax
    push 0
    call dword ptr hproc
    actually C uses [ebp+ ? ] , but this is the main idea

    Now, which the difference between
    lea eax,dword ptr tle
    push eax
    and
    push offset tle

    Another quest
    lea eax,dword ptr tle ;is equal with (for the compiler)
    lea eax,dword ptr [tle] ;in there logic explanation for this ?
    [/code]

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : [b][red]This message was edited by xkgdiam at 2003-12-14 14:32:48[/red][/b][hr]
    : [code]
    : :: hproc(0,msg,tle,0); //that works ok ....
    : :: asm { .386 //put them here to force
    : :: .model flat //code to work
    : :: push 0
    : :: push offset tle
    : :: push offset msg
    : :: push 0
    : :: call hproc //i try, call [hproc] ,too, but fails
    : :: }
    : :: Can anyone help me, with this above ?
    : [hr]
    : due an error in my code, my editor open the disassembling src of
    : my code and i found how C disassmble 'hproc(0,msg,tle,0);'
    :
    : push 0
    : lea eax,dword ptr tle
    : push eax
    : lea eax,dword ptr msg
    : push eax
    : push 0
    : call dword ptr hproc
    : actually C uses [ebp+ ? ] , but this is the main idea
    :
    : Now, which the difference between
    : lea eax,dword ptr tle
    : push eax
    : and
    : push offset tle
    :
    : Another quest
    : lea eax,dword ptr tle ;is equal with (for the compiler)
    : lea eax,dword ptr [tle] ;in there logic explanation for this ?
    : [/code]
    :
    :
    [blue]Just want to say that when you disassemble C/C++ code it has a great difference between the DEBUG build and RELEASE build. Try to see what is happening in RELEASE - it probably uses that "PUSH OFFSET [msg]" you mentioned.[/blue]
  • xkgdiamxkgdiam Posts: 111Member
    [b][red]This message was edited by xkgdiam at 2003-12-14 20:17:56[/red][/b][hr]
    [code]
    : : Now, which the difference between
    : : lea eax,dword ptr tle
    : : push eax
    : : and
    : : push offset tle
    : :
    : : Another quest
    : : lea eax,dword ptr tle ;is equal with (for the compiler)
    : : lea eax,dword ptr [tle] ;in there logic explanation for this ?
    : : [/code]
    : :
    : :
    : [blue]Just want to say that when you disassemble C/C++ code it has a great difference between the DEBUG build and RELEASE build. Try to see what is happening in RELEASE - it probably uses that "PUSH OFFSET [msg]" you mentioned.[/blue]
    :
    [code]
    i'm using bcc32 and tasm32 and no IDE, just own makefiles, so i only
    one version of my program exist.Anyway i just turn debug info off,
    and the output is just the same.Actuall i'm talking for the compilation
    of my code in asm file, but i think its too near with the exe.

    in my first post, i said that i use the format (inline asm)
    push offset arg1
    push offset arg2
    call hproc
    cause it seem logical to me, but this code FAILS !
    when later i write my inline asm code as
    lea eax,dword ptr arg1
    push eax
    ...
    call hproc
    it works fine.
    So i think the 2 codes arent the same .... ?
    Maybe something is error i dont know (that is my question),
    but in my first post is ALL of the code i used (console appl).






    [/code]


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : [b][red]This message was edited by xkgdiam at 2003-12-14 20:17:56[/red][/b][hr]
    : [code]
    : : : Now, which the difference between
    : : : lea eax,dword ptr tle
    : : : push eax
    : : : and
    : : : push offset tle
    : : :
    : : : Another quest
    : : : lea eax,dword ptr tle ;is equal with (for the compiler)
    : : : lea eax,dword ptr [tle] ;in there logic explanation for this ?
    : : : [/code]
    : : :
    : : :
    : : [blue]Just want to say that when you disassemble C/C++ code it has a great difference between the DEBUG build and RELEASE build. Try to see what is happening in RELEASE - it probably uses that "PUSH OFFSET [msg]" you mentioned.[/blue]
    : :
    : [code]
    : i'm using bcc32 and tasm32 and no IDE, just own makefiles, so i only
    : one version of my program exist.Anyway i just turn debug info off,
    : and the output is just the same.Actuall i'm talking for the compilation
    : of my code in asm file, but i think its too near with the exe.
    :
    : in my first post, i said that i use the format (inline asm)
    : push offset arg1
    : push offset arg2
    : call hproc
    : cause it seem logical to me, but this code FAILS !
    : when later i write my inline asm code as
    : lea eax,dword ptr arg1
    : push eax
    : ...
    : call hproc
    : it works fine.
    : So i think the 2 codes arent the same .... ?
    : Maybe something is error i dont know (that is my question),
    : but in my first post is ALL of the code i used (console appl)
    : [/code]
    :
    [blue]I see, it is a common mistake. When the address of local variable or parameter from stack needs to be taken - LEA must be used. When the address of a code label or variable from .DATA section needs to be taken - OFFSET can be used. LEA also can be used for that purpose, but OFFSET produces shorter code when you work with variables in .DATA section. When working with stack - LEA only should be used.[/blue]
  • xkgdiamxkgdiam Posts: 111Member
    [b]
    : [blue]I see, it is a common mistake. When the address of local variable or parameter from stack needs to be taken - LEA must be used. When the address of a code label or variable from .DATA section needs to be taken - OFFSET can be used. LEA also can be used for that purpose, but OFFSET produces shorter code when you work with variables in .DATA section. When working with stack - LEA only should be used.[/blue]
    :
    [/b]
    Thank you, i understand. When i move my variables outside the
    'main' proc, push offset .., works right !
Sign In or Register to comment.