flat assembler
Message board for the users of flat assembler.

Index > Windows > [solved] undefined symbol 'CreateFile'

Author
Thread Post new topic Reply to topic
Foxxy



Joined: 14 Jul 2014
Posts: 42
Location: Somewhere over the rainbow...
Foxxy
Hello, I am fairly new to assembly, and I have found that FASM is the best Assembler. However, I am having issues with CreateFile. I am trying to get the size of a file, and I'm using CreateFile to open it. However, When I attempt to compile I get the error: 'Error: undefined symbol 'CreateFile'. Can you please look over my code and see what I'm doing wrong? I've compared it to other examples and I don't see what I have done wrong. Also, I have another question. When should I surround a variable like 'numBytes' with brackets []?

Code:
format PE console
entry start

include 'win32ax.inc'

section '.data' data readable writeable
inputFile db "C:\WINDOWS\NOTEPAD.EXE",0

crc dd ?
numBytes db ?
hSrcFile dd ?

section '.code' code readable executable

start:

;===========Open File===========
push 0
push FILE_ATTRIBUTE_NORMAL
push OPEN_EXISTING
push 0
push FILE_SHARE_READ
push GENERIC_READ
push inputFile
call CreateFile

mov [hSrcFile],eax
;======set write head at 0======
push FILE_BEGIN
push 0
push 0
push [hSrcFile]
call SetFilePointer
;=========get file size=========
push 0
push [hSrcFile]
call GetFileSize

invoke ExitProcess,0

section '.idata' import data readable
library kernel,'kernel32.dll',\
        msvcrt,'msvcrt.dll'

import  kernel32,\
        ExitProcess,'ExitProcess',\ 
        CloseHandle,'CloseHandle',\ 
        CreateFile,'CreateFileA',\
 
import  msvcrt,\
        printf,'printf',\
        getchar,'_fgetchar'
    


Last edited by Foxxy on 14 Jul 2014, 05:19; edited 1 time in total
Post 14 Jul 2014, 02:59
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
Code:
        numBytes        dd      128
        ...
        mov     eax, numBytes           ; EAX <-- address of numBytes
        mov     eax, [numBytes]         ; EAX <-- contents of numBytes (128 in this case)
    

1. Try to use invoke for all APIs (CreateFile, GetFileSize), like you do with ExitProcess.
2. No need for SetFilePointer - when file just opened - its pointer is already zero - at the beginning of the file.
Post 14 Jul 2014, 03:16
View user's profile Send private message Send e-mail Reply with quote
Foxxy



Joined: 14 Jul 2014
Posts: 42
Location: Somewhere over the rainbow...
Foxxy
Why should I use invoke over normal calling? I guess it makes it neater. However, I did try to use invoke, but it still gave me the error. And thank you for clearing up the brackets for me, in disassembly brackets indicate the address of something, not the contents.
Post 14 Jul 2014, 05:05
View user's profile Send private message Reply with quote
Foxxy



Joined: 14 Jul 2014
Posts: 42
Location: Somewhere over the rainbow...
Foxxy
Also, as it turns out it is a bad idea to import something already declared in 'win32ax.inc', it causes this issue.
Post 14 Jul 2014, 05:19
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
I think you're mistaken about disassembler.
Please compare these:

DisAsm:
Code:
;
; Load into ECX from address "ebx + 4Ch"
;
mov ecx, dword [ebx + 4Ch]
    


FASM:
Code:
;
; Load into EAX from address "numBytes"
;
mov eax, [numBytes]
    

Brackets present in both cases and whatever inside is an address.
Post 14 Jul 2014, 10:39
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.