Hi there from a complete newbie and new member.

I know this is a stupid question but can someone give me a clue, well not a clue but a bit of code that generates a new file. I've found heaps of examples of opening and writing to existing files but can't seem to find anything on creating a new one. I must admit I know absolutely nothing about programming apart from a bit of basic in years gone by so I'm starting from nothing here. I'm assuming one uses CreatefileA. I'm struggling with all of this but I am trying, honest. Am I just stupid??(That's a rhetorical question ;) I even tried resorting to a routine using int 21 that I got from, I think it was Gavin somethings dos asm tutorial and then tried to change the int 21 to dos3call like something I saw in another book . I'm clutching at straws here and have absolutely noone I can turn to to get me over the hump of the learning curve. I've downloaded the api references but my brain won't allow me to see how they apply to w32 assembly.

Sorry about this. I'll try not to be so dumb in the future.

Hopefully I might have answered the question myself before someone gets back to me.

Cheers,

Raavin
Posted on 2002-04-30 00:22:45 by Raavin


invoke CreateFile, addr filename, \
GENERIC_READ or GENERIC_WRITE, \
0,0, CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE, 0
mov HandleNewFile,eax
; returns ERROR_ALREADY_EXISTS in eax if it already exists


np :)

ps: I highly recommend the Iczelion's Tutorials for the little jump start you may need
Posted on 2002-04-30 01:00:11 by JimmyClif
welcome to the board Raavin!

i really would recommend that you read through iczelions tutorials at http://spiff.tripnet.se/~iczelion/tutorials.html. it will teach you everything you really need to know to get you started.

creating a file is basically the same way as opening a file. so you can pretty much use the same code but you need to change a paramater or two in the CreateFile api call. instead of having the CreateFile api call open a file with the paramter OPEN_EXISTING you would just need to change it to CREATE_NEW.

this is just to get you started. if i had time i would have coded you up a simple example.
Posted on 2002-04-30 01:06:58 by smurf
Cheers peeple. I'll give it a bash and get back to you.

Raavin
Posted on 2002-04-30 01:53:07 by Raavin
Raavin, you might also want to check out the stuff at http://www.madwizard.org .
Posted on 2002-04-30 04:52:23 by f0dder
Here ?tis

########################################

Rest of asm
|
MYSTRING db "Blah Blah",0
MYFILENAME db "Blah.txt",0
|
more of asm
|
FILE_ATTRIBUTE_NORMAL equ 80h
CREATE_ALWAYS equ 2
GENERIC_READ equ 80000000h
GENERIC_WRITE equ 40000000h
|
a bit more
|
fhandle dd 0
bwrite dd 0
|
even more
|
Call CreateFileA, offset MYFILENAME, GENERIC_READ + GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
mov fhandle, eax
Call WriteFile, fhandle, offset MYSTRING, 9, offset bwrite, 0
Call CloseHandle, fhandle
|
|
The rest?

#########################################

Ok. I took your advice, checked out the API reference, which doesn?t look half as intimidating now, and slipped this into the code and, voila, it worked like a dream. There is probably a better way to do it so feel free to add any tips. I got the CREATE_ALWAYS equate value from another header file but I?m assuming that if I used the right include file I wouldn?t need to define these here. The bwrite and fhandle were already in the template code for file opening so I just used them. Could I have just set these to 0??? What is the difference between db, dd and dw anyway?? I also believe that the 9 should be replaced with something that counts the number of bytes at a location rather than directly assigning it. I?m not sure which api that is associated with.

I am trying to read up on all this. I?ve got a copy of Barry Kauler?s book which seems to be ok. I?m slowly getting through it. I downloaded the AOA Win32 edition but it all seems to focus on his own High Level assembly which is not really the direction I want to head in. Maybe later it might come in handy but it seems to defeat the purpose a bit.

Thanks heaps for your help so far. This was my first question and was answered quickly and successfully. What I am going to try to do is to put together my own ?Moron?s Assembly and API reference and snippets? as I learn new things trying to add the bits that more knowledgeable people leave out just because it is so obvious to them.

I like it here so far. I think I might stick around.

Cheers,

Raavi ;)
Posted on 2002-04-30 20:38:11 by Raavin
I got the CREATE_ALWAYS equate value from another header file but I?m assuming that if I used the right include file I wouldn?t need to define these here.


The flags listed in the api ref are defined in windows.inc. You can just include windows.inc at the top of your asm file and use the constants CREATE_ALWAYS,OPEN_EXISTING,etc.

The bwrite and fhandle were already in the template code for file opening so I just used them. Could I have just set these to 0???


The file handle (fhandle) uniquely indentifies a file. You use it in subsequent file operations (ReadFile, WriteFile, ...)
bwrite is used to return the number of bytes actually written after a call to WriteFile.

What is the difference between db, dd and dw anyway??


db = declare byte
dw = declare word (2 bytes)
dd = declare double word (4 bytes)

I also believe that the 9 should be replaced with something that counts the number of bytes at a location rather than directly assigning it. I?m not sure which api that is associated with.


Look in the help file masmlib.hlp file (\masm32\help\) for StrLen. That's a custom assembler routine that returns the string length.
Or you could use lstrlen which you'll find documented in the api ref.
Posted on 2002-05-01 01:22:19 by grv575
Thanks for all your advice so far. Here's an example of the sort of moron's API reference I want to put together. If anyone wants to change or add anything as far as info or usage suggestions go it would be appreciated, then I can include the ideas in subsequent descriptions. For example, I've used call rather than invoke. If there are compatibility issues with compilers that's the stuff that needs to go in. It's adapted from the normal API references with some extras thrown in related to usage, necesarry includes and data elements. Obviously if there are major errors then that needs to be addressed too. The formatting also isn't perfect yet.

Thanks

Raavin
Posted on 2002-05-02 02:07:10 by Raavin
People who don't ask know already. Usually they're stupid.

I know what I'm talking about.

Giovanni
Posted on 2002-05-05 16:36:14 by sch.jnn
People who don't ask know already. Usually they're stupid.


I don't understand your meaning, although if you're inferring that I'M stupid, then maybe that makes you right. Putting this API reference together cartainly has cleared things in my mind. Sometimes the delivery by people in the know is over peoples heads. I personally am getting into assembly from a background of bugger all programming. What seems obvious to someone who is migrating from experience in a HLL is just plain greek to me. I also tend to lose inspiration when I can't get something. This forum has been a great help at clearing up the vague descriptions one often encounters. I'd like to contribute to others starting out and also make concrete the things in my mind. I am trying to develop a format that has all of the information one requires when using an API like formatting examples, equates for values and includes, clearer descriptions of the data types etc.

I basically don't think that anything is inaccessible if it's conveyed in the right way. There are some out there who like to restrict difficult to grasp information to an elite group. I for one don't agree. As I learn I would like to pass on my new knowledge to those like me. The time and logically challenged.

Help me out if you can or want to. Don't ifr you're one of the elite.

:)

Raavin
Posted on 2002-05-08 18:57:57 by Raavin