flat assembler
Message board for the users of flat assembler.

Index > Windows > Saving current directory to text file

Author
Thread Post new topic Reply to topic
Traxler



Joined: 30 Oct 2014
Posts: 12
Traxler 30 Oct 2014, 18:26
Hello 'world'


buffer db ?
buffersize = $ - buffer
invoke GetCurrentDirectory,buffersize,buffer

how do I declare variables so that the current directory can be copied to text file with WriteFile ?
if someone had a small example.

thank you for your help
Post 30 Oct 2014, 18:26
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 30 Oct 2014, 20:30
Regarding this

Code:
DWORD WINAPI GetCurrentDirectory(
  _In_   DWORD nBufferLength,
  _Out_  LPTSTR lpBuffer
);
    


you can code it like this:

Code:
buffer db 200h dup(0)
buffersize = $ - buffer
invoke GetCurrentDirectory,buffersize,buffer
    
Post 30 Oct 2014, 20:30
View user's profile Send private message Send e-mail Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 31 Oct 2014, 02:24
Code:

invoke GetCurrentDirectoryA, PATH_LENGTH, pszBuffer
invoke CreateFileA, pszFileName, 0, GENERIC_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
invoke lstrlenA,  pszBuffer
invoke WriteFile, [hFile], pszBuffer, eax, dwWritten, 0
invoke CloseHandle, [hFile]

    
Post 31 Oct 2014, 02:24
View user's profile Send private message Reply with quote
Traxler



Joined: 30 Oct 2014
Posts: 12
Traxler 31 Oct 2014, 09:19
buffer db 200 dup(0) instead of buffer db ? solved my problem
Thans a lot.

Laughing

shutdownall wrote:
Regarding this

Code:
DWORD WINAPI GetCurrentDirectory(
  _In_   DWORD nBufferLength,
  _Out_  LPTSTR lpBuffer
);
    


you can code it like this:

Code:
buffer db 200h dup(0)
buffersize = $ - buffer
invoke GetCurrentDirectory,buffersize,buffer
    
Post 31 Oct 2014, 09:19
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1397
Location: Piraeus, Greece
Picnic 01 Nov 2014, 15:12
typedef wrote:
Code:

invoke GetCurrentDirectoryA, PATH_LENGTH, pszBuffer
invoke CreateFileA, pszFileName, 0, GENERIC_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
invoke lstrlenA,  pszBuffer
invoke WriteFile, [hFile], pszBuffer, eax, dwWritten, 0
invoke CloseHandle, [hFile]

    


Mind the handle Wink
Code:
invoke CreateFileA, pszFileName, 0, GENERIC_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
mov [hFile], eax
    
Post 01 Nov 2014, 15:12
View user's profile Send private message Visit poster's website Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 01 Nov 2014, 20:43
Yeah. I left out a lot assuming that they know how to weave that code together.
Post 01 Nov 2014, 20:43
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 02 Nov 2014, 13:55
Traxler wrote:
buffer db 200 dup(0) instead of buffer db ? solved my problem
Thans a lot.

Laughing


Just in case, the 200 means the size of the buffer.
So for bigger directories you may choose more buffer (1000h = 4096 bytes) and to be more general it would be good to allocate memory dynamically. But this maybe too much for a hello world project. Wink
Post 02 Nov 2014, 13:55
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 02 Nov 2014, 15:44
shutdownall,

Dynamic allocation is good when data is really dynamic.
Post 02 Nov 2014, 15:44
View user's profile Send private message Reply with quote
TheRaven



Joined: 22 Apr 2008
Posts: 91
Location: U.S.A.
TheRaven 15 Jan 2015, 13:37
Traxler wrote:
buffer db 200 dup(0) instead of buffer db ? solved my problem
Thans a lot.

Laughing
you can code it like this:



Buffer sizes are explicit with Windows and a ? in best case scenario will result in a buffer of zero length. By general convention, text file buffers are 1024 or a megabyte.

_________________
Nothing so sought and avoided more than the truth.
I'm not insane, I know the voices in my head aren't real!
Post 15 Jan 2015, 13:37
View user's profile Send private message Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 350
Location: Poland
macgub 16 Jan 2015, 11:16
Hi. I have similar problem. I have something write to file. But there are some problems with file handler. File appears only if I leave program and ex. change current directory Who help me fix my code Question
Code:
;--code

    invoke  GetStdHandle, STD_OUTPUT_HANDLE
    mov  [file_handle],eax
    invoke  CreateFile,file_name, GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
    invoke  WriteFile,eax,file_buffer,[file_size], byteswritten, 0
    invoke  CloseHandle,[file_handle]

;--data
  file_handle dd ?
  file buffer rb 65536                                        
  bytes_written dd ?    

    
Post 16 Jan 2015, 11:16
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1657
Location: Toronto, Canada
AsmGuru62 16 Jan 2015, 16:33
Not sure what do you want to do with the code.
When you close the handle - you're closing the handle returned by GetStdHandle(), not the one returned by CreateFile().
Also, [file_size] and [file_name] are not declared.
Post 16 Jan 2015, 16:33
View user's profile Send private message Send e-mail Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 350
Location: Poland
macgub 16 Jan 2015, 16:47
Now the code is:
Code:
;---code
  invoke  CreateFile,file_name, GENERIC_WRITE, 0, 0,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
    mov  [file_handle],eax
    invoke  WriteFile,eax,file_buffer,[file_size], byteswritten, 0
    invoke  CloseHandle,[file_handle]  ]    
    
;--data
   file_handle dd ? 
  file buffer rb 65536                                         
  bytes_written dd ?     
 file_name    db 'surface.3ds',0 
file_size dd 1024

    

And all works as I want. Thanks AsmGuru62 for the tip.

[/b]
Post 16 Jan 2015, 16:47
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 17 Jan 2015, 05:41
my win32 version, was bored...
Code:
format PE GUI 4.0
entry start

include 'win32axp.inc'

section '.text' code readable executable

 start:
        invoke GetCurrentDirectoryA,0,0
        mov [dwLength],eax

        invoke LocalAlloc,LPTR,[dwLength]
        mov [pszBuffer],eax

        invoke GetCurrentDirectoryA,[dwLength],[pszBuffer]

        invoke  CreateFileA,pszFileName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        mov [hOutputFile],eax

        mov eax,[dwLength]
        sub eax,sizeof.TCHAR
        invoke WriteFile,[hOutputFile],[pszBuffer],eax,dwBytesWritten,0

        invoke CloseHandle,[hOutputFile]

        invoke LocalFree,[pszBuffer]

        invoke ExitProcess,0

section '.data' data readable writeable

 pszFileName db 'spit.txt',0
 hOutputFile dd INVALID_HANDLE_VALUE
 dwBytesWritten dd ?
 pszBuffer dd ?
 dwLength dd ?

section '.idata' import data readable

 library kernel32,'KERNEL32.DLL',\
         user32,'USER32.DLL'

 include 'api\kernel32.inc'
 include 'api\user32.inc'
    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 17 Jan 2015, 05:41
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.