flat assembler
Message board for the users of flat assembler.

Index > Windows > Renaming Files (using MSVCRT)?

Author
Thread Post new topic Reply to topic
optiplex



Joined: 07 Mar 2008
Posts: 7
optiplex
Hello

Im kinda new to FASM, and I want to rename a file.

Im using the msvcrt.dll

This is my template
Code:
; by optiplex

format PE CONSOLE 4.0
entry start

include 'INCLUDE\win32a.inc'

section ".code" code readable writeable executable


start:
     cinvoke printf, strMain
        ; rename file, wtf?

  cinvoke exit, 0

section '.data' data readable writeable
        
        strMain db 'Renaming cool.txt to mool.txt Smile'
     strOld db 'C:\cool.txt'
      strNew db 'C:\mool.txt'

section '.idata' import data readable writeable

 library msvcrt, 'msvcrt.dll'

  import msvcrt,\
           getchar, 'getchar',\
             printf, 'printf',\
               puts, 'puts',\
           rename, 'rename',\
               exit, 'exit'    


I tried various things like
cinvoke rename, strNew, strOld
or
push strNew
push strOld
call [rename]

Plz help me out, im a newb Sad

_________________
-optiplex-
Post 07 Mar 2008, 19:01
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4240
Location: 2018
edfed
[erf mode]
to modify the name, right clic on icon, then, rename.
or F2
or ren file.axt file.ext
[/erf mode]

i insert a general question in this thread.

how to modify, access, create, delete, read, write etc files in a "FILE SYSTEM independant" way?

is it possible?
Post 07 Mar 2008, 19:32
View user's profile Send private message Visit poster's website Reply with quote
wisepenguin



Joined: 30 Mar 2005
Posts: 129
wisepenguin
hello,

this works successfully for me using latest fasm 1.67.26

Code:
start: 
        cinvoke printf, strMain 
        cinvoke rename, strOld, strNew

        cinvoke exit, 0 

section '.data' data readable writeable 
         
        strMain db 'Renaming cool.txt to mool.txt ',0
        strOld db 'cool.txt',0
        strNew db 'mool.txt',0
    


you need to null terminate strings manually in FASM

have a good day
Post 07 Mar 2008, 19:48
View user's profile Send private message Reply with quote
optiplex



Joined: 07 Mar 2008
Posts: 7
optiplex
Thank you wisepenguin.
But it didnt work for me Sad

Im also using the latest fasm 1.67.26, but the code doesnt work.

Code:
; rename example
; by optiplex


format PE CONSOLE 4.0

include 'INCLUDE\win32a.inc'


start:
        cinvoke printf, strMain 
        cinvoke rename, strOld, strNew
        cinvoke getchar, 0

        cinvoke exit, 0 

section '.data' data readable writeable 
         
        strMain db 'Renaming cool.txt to mool.txt ',0
        strOld db 'cool.txt',0
        strNew db 'mool.txt',0



section '.idata' import data readable writeable

        library msvcrt, 'msvcrt.dll'

        import msvcrt,\
        printf, 'printf',\
        getchar, 'getchar',\
        rename, 'rename',\
        exit, 'exit'
    


Any idea?

Thanks again.
Post 07 Mar 2008, 20:05
View user's profile Send private message Reply with quote
wisepenguin



Joined: 30 Mar 2005
Posts: 129
wisepenguin
i ran mine on windows xp, with the following set up
my documents\test.exe (your code)
my documents\cool.txt

so the file to rename is in the same directory as the program.
but your first code hard coded it to "C:\cool.txt".

if you like change it back to C:\cool.txt and make sure C:\cool.txt
exists initially.

but i can still confirm, it works successfully here by null terminating
the strings.

i hope this helps, or you find the fault if it doesn't.
Post 07 Mar 2008, 20:18
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
optiplex wrote:
Thank you wisepenguin.
But it didnt work for me Sad

Im also using the latest fasm 1.67.26, but the code doesnt work.

Code:
; rename example
; by optiplex


format PE CONSOLE 4.0

include 'INCLUDE\win32a.inc'


start:
        cinvoke printf, strMain 
        cinvoke rename, strOld, strNew
        cinvoke getchar, 0

        cinvoke exit, 0 

section '.data' data readable writeable 
         
        strMain db 'Renaming cool.txt to mool.txt ',0
        strOld db 'cool.txt',0
        strNew db 'mool.txt',0



section '.idata' import data readable writeable

        library msvcrt, 'msvcrt.dll'

        import msvcrt,\
        printf, 'printf',\
        getchar, 'getchar',\
        rename, 'rename',\
        exit, 'exit'
    


Any idea?

Thanks again.


1 - getchar() doesn't require any parameter
2 - you don't need to call exit() here, you can use the instruction "ret" instead but remember that EAX must contain the return value of main()

_________________
(French only) http://dap.developpez.com
Post 07 Mar 2008, 20:33
View user's profile Send private message Visit poster's website Reply with quote
optiplex



Joined: 07 Mar 2008
Posts: 7
optiplex
Okay, I got it working now

Thank wisepenguin, and thank you too dap for the tips Smile
Post 08 Mar 2008, 07:27
View user's profile Send private message Reply with quote
wisepenguin



Joined: 30 Mar 2005
Posts: 129
wisepenguin
anytime.

what was the problem in the end ?
Post 08 Mar 2008, 11:51
View user's profile Send private message Reply with quote
optiplex



Joined: 07 Mar 2008
Posts: 7
optiplex
I think it was because of the pathes, i used C:\cool.txt > C:\mool.txt, but when I had them in the same directory as the program, like you did, it worked for me.
Post 08 Mar 2008, 21:34
View user's profile Send private message Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
Why is this in the DOS forum?
Post 09 Mar 2008, 03:29
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
This works on my computer :
Code:
format PE console 4.0
entry start

include 'win32a.inc'

section '.text' code readable executable
start:
        push ebp
        mov ebp, esp
        
        push new
        push old
        call [rename]
        pop ecx ecx

; rename() returns zero if the operation succeeds
        test eax, eax
        jnz .failure
        
        push success
        call [puts]
        pop ecx
        
.failure:
        pop ebp
        xor eax, eax
        ret
        

section '.rdata' data readable        
        old  db  'C:\Documents and Settings\Bastien\Bureau\cool.txt', 0
        new  db  'C:\Documents and Settings\Bastien\Bureau\mool.txt', 0
        success  db  'Success', 0


section '.idata' data readable import
        library msvcrt,   'msvcrt.dll'
        import msvcrt, \
          rename, 'rename',  \
          puts,   'puts'    

_________________
(French only) http://dap.developpez.com
Post 09 Mar 2008, 10:02
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17332
Location: In your JS exploiting you and your system
revolution
You can also use the API MoveFile or MoveFileEx, no need to use msvcrt.
Post 09 Mar 2008, 10:05
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
..


Last edited by DOS386 on 12 Mar 2008, 01:02; edited 1 time in total
Post 10 Mar 2008, 06:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17332
Location: In your JS exploiting you and your system
revolution
Okay, no problem, if ever I manage to catch a mod roaming around doing nothing I will be sure to grab him by the collar point him here.
Post 10 Mar 2008, 06:53
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
when you use pe format, why dont u call from kernel32? MoveFile.
and you dont have to 'exit'. Pogram is loading by call from CsrCreateThread, and must return to CsrExitThread.

Code:
format PE CONSOLE 4.0
entry start     

also entrypoint is not needed. format pe console is enought.
Post 10 Mar 2008, 13:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17332
Location: In your JS exploiting you and your system
revolution
asmrox wrote:
also entrypoint is not needed. format pe console is enought.
AFAIK "entry" is required .
Post 10 Mar 2008, 13:38
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
in exe no. Program will start executing from executable section, or smth like tat
Post 10 Mar 2008, 17:20
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17332
Location: In your JS exploiting you and your system
revolution
asmrox wrote:
in exe no. Program will start executing from executable section, or smth like tat
No it doesn't. Windows will call the location defined by 'entry'.
Post 10 Mar 2008, 17:22
View user's profile Send private message Visit poster's website 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.