News:

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

Can't Load Dll

Started by MusicalMike, February 17, 2006, 12:48:56 AM

Previous topic - Next topic

MusicalMike


.486
.model flat, stdcall

include windows.inc
include masm32.inc
include kernel32.inc
include msvcrt.inc

includelib msvcrt.lib
includelib masm32.lib
includelib kernel32.lib
       
.data
        libname       db "DisasmEngineDLL.dll", 00h
        procname      db "Disassemble", 00h
        dialog        db "Disassemble working fine\n", 00h
        assembly      db 256 dup(0)
        remarks       db 256 dup(0)
        opcode        db 30 dup(0)
        address       dd 00h
        opcodesize    db 00h
        prefixsize    db 00h
        hdll          dd 00h
        linear        dd 00h
        decode        dd 00h
        error         dd 00h       
        fmt           db "%s%s", 00h       
        msg           db 128 dup(0)
       
.code

Start:          push offset libname
                call LoadLibrary
                cmp eax, 00h
                je handleerror
                mov hdll, eax
                push offset procname
                push offset hdll
                call GetProcAddress
                mov decode, eax
                cmp decode, 00h
                jne ok
                push offset hdll
                call FreeLibrary
                jmp handleerror
ok:             push offset dialog
                call crt_printf
                nop
                nop
                push offset hdll
                call FreeLibrary
                push 0
                call ExitProcess
handleerror:    call GetLastError
                push NULL
                push 128
                push offset msg
                push 00h
                push eax
                push NULL
                push FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS
                call FormatMessage
                push offset msg + 2
                push offset libname
                push offset fmt
                call crt_printf
                push 5000
                call Sleep
                push 0
                call ExitProcess
               
End Start


If you run this code, getlasterror reports not a valid win32 application. When I run the example c++ code that came with the dll, it loaded fine. Please help.

Tedd

At a guess, crt_printf doesn't clean up the stack for you (it's a c function). So you need to do so after each call to it.
When compiling with c++, the compiler inserts the cleanup for you.

add esp,(4*number_of_parameters_pushed)
No snowflake in an avalanche feels responsible.

Gustav





                push offset procname
                push offset hdll                     ;you should code "push hdll" instead
                call GetProcAddress

MusicalMike

I corrected the code based on what you said and even optomized it a bit, but still have the same problem.


.data
        assembly      db 256 dup(0)
        remarks       db 256 dup(0)
        opcode        db 30 dup(0)
        address       dd 00h
        opcodesize    db 00h
        prefixsize    db 00h
        hdll          dd 00h     
        decode        dd 00h       
        libname       db "DisasmEngineDLL.dll", 00h
        procname      db "Disassemble", 00h
        dialog        db "Disassemble working fine!", 0Ah, 0Dh, 00h
        error1        db "Problem loading dll!", 0Ah, 0Dh, 00h
        error2        db "Problem getting function!", 0Ah, 0Dh, 00h

.code
Start:  push offset libname
        call LoadLibrary
        cmp eax, 00h
        je err1
        mov hdll, eax
        push offset procname
        push hdll
        call GetProcAddress
        cmp eax, 00h
        je err2
        mov decode, eax
        push offset dialog
        call StdOut
        jmp stop
err1:   push offset error1
        call StdOut
        jmp stop
err2:   push offset error2
        call StdOut
        push hdll
        call FreeLibrary
stop:   push 5000
        call Sleep
        push 00h
        call ExitProcess

End Start


Any ideas?

MusicalMike

Turns out it was a user error, I forgot to actually put the dll in the working dirrectory. Your suggestions probably helped also. Thanks.