Author Topic: Problem loading a .dll  (Read 4034 times)

Offline Teol

  • Jr. Member
  • *
  • Posts: 12
Problem loading a .dll
« on: March 25, 2016, 11:40:18 AM »
Hi,
I´m quite new to NASM. What i´m trying to achieve is to load a dll to another assembly (.exe) in windows 10 64 bit environment.
So i have two modules one executable (.exe) Loader and one dynamic link library DebugProc (.dll)

I´m trying to load the dll to Loader with winapi function LoadLibraryA but the LoadLibraryA wont return the handle in rax i can see this with ArkDasm.

Here is the code for the Loader:

global main
extern LoadLibraryA ;params: string filename of the dll to load.
extern ExitProcess

bits 64
section .data
debugProcFileName: db 'DebugProc.dll', 0

section .text
start:
    mov rcx, debugProcFileName
    call LoadLibraryA

For compliling and linking this loader i use:
Compile: nasm -fwin64 Loader.asm
Link: golink /console Loader.obj Kernel32.dll

The DebugProc.dll code:
export DebugProc
bits 64
section .text
DebugProc:
ret

For compiling and linkin this i use:
Compile: nasm -fwin64 DebugProc.asm
Link: golink /dll /console /export DebugProc DebugProc.obj

Both the .dll and the .exe are run in the same folder.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Problem loading a .dll
« Reply #1 on: March 27, 2016, 01:27:25 AM »
Okay, I'll make note of some things in Loader.asm that might help you get going.
Code: (Loader.asm) [Select]
;; global main << Why do you need this? You use 'start below.

extern LoadLibraryA ;params: string filename of the dll to load.
extern ExitProcess

bits 64
section .data
debugProcFileName: db 'DebugProc.dll', 0
debugProcRef: dq 0

section .text
start:
    ;; LoadLibraryA : string -> pointer .
    mov rcx, debugProcFileName
    call LoadLibraryA
    mov [debugProcRef], RAX ;; pointer to function is in RAX

    ;; call your procedure.
    call [debugProcRef]

    ;; call it again.
    call [debugProcRef]

    ;; ExitProcess : errorcode -> nothing.
    mov rcx, 0
    call ExitProcess ;; return control to the operating system.

Looking at the DebugProc.asm code, I don't think this will work. I haven't used Windows in quite a long time, but the convention for DLL's was to export a routine called DllMain, maybe try the following changes.

Code: (DebugProc.asm) [Select]
export DebugProc
export DllMain
bits 64
section .text
DllMain:
    xor rax, rax ;; clear RAX
    cmp rdx, 1 ;; is second parameter to DllMain DLL_PROCESS_ATTACH
    jne .DONE ;; if not, the return value is ignored.

    ;; otherwise, return true
    mov rax, 1
.DONE:
    ret

DebugProc:
ret

HtH,
~ Bryant

About Bryant Keller
bkeller@about.me