I am trying to convert some c file rutines to api equiavalents.I have some difficulties when converting them.I present original and converted source code.Could you tell me are they correct and how can I make compile fopen_file_func proc.Thanks


First Function
--------------


C version
voidpf ZCALLBACK fopen_file_func (opaque, filename, mode)
voidpf opaque;
const char* filename;
int mode;
{
FILE* file = NULL;
const char* mode_fopen = NULL;
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
mode_fopen = "rb";
else
if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
mode_fopen = "r+b";
else
if (mode & ZLIB_FILEFUNC_MODE_CREATE)
mode_fopen = "wb";

if ((filename!=NULL) && (mode_fopen != NULL))
file = fopen(filename, mode_fopen);
return file;
}


My implementation



ZLIB_FILEFUNC_SEEK_SET = 0
ZLIB_FILEFUNC_SEEK_CUR = 1
ZLIB_FILEFUNC_SEEK_END = 2
ZLIB_FILEFUNC_MODE_READ = 1
ZLIB_FILEFUNC_MODE_WRITE = 2
ZLIB_FILEFUNC_MODE_READWRITEFILTER = 3
ZLIB_FILEFUNC_MODE_EXISTING = 4
ZLIB_FILEFUNC_MODE_CREATE = 8


fopen_file_func proc opaque, filename, mode
;I dont know correct values for mode_fopen therefore below
;ones are dummy values
mov ecx,0 ;ecx =mode_fopen
;!!! Below Line doesnt compile.......
.if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)== ZLIB_FILEFUNC_MODE_READ)
mov ecx,1 ;"rb"
.elseif (mode & ZLIB_FILEFUNC_MODE_EXISTING)
mov ecx,2 ;"r+b"
.elseif (mode & ZLIB_FILEFUNC_MODE_CREATE)
mov ecx,3 ;"wb"
.endif
.if ((filename!=NULL) && (ecx != NULL))
invoke CreateFile,filename,GENERIC_READ or GENERIC_WRITE,0,0,ecx,0,0

.endif



ret

fopen_file_func endp


I dont know what is the correct flags for createfile (rb,r+b,wb equals to what ?)

Second Function
---------------



long ZCALLBACK ftell_file_func (opaque, stream)
voidpf opaque;
voidpf stream;
{
long ret;
ret = ftell((FILE *)stream);
return ret;
}

my convertion


ftell_file_func proc opaque, stream

invoke SetFilePointer,stream,0,0,FILE_CURRENT

ret
ftell_file_func endp


Third Function
--------------



long ZCALLBACK fseek_file_func (opaque, stream, offset, origin)
voidpf opaque;
voidpf stream;
uLong offset;
int origin;
{
int fseek_origin=0;
long ret;
switch (origin)
{
case ZLIB_FILEFUNC_SEEK_CUR :
fseek_origin = SEEK_CUR;
break;
case ZLIB_FILEFUNC_SEEK_END :
fseek_origin = SEEK_END;
break;
case ZLIB_FILEFUNC_SEEK_SET :
fseek_origin = SEEK_SET;
break;
default: return -1;
}
ret = 0;
fseek((FILE *)stream, offset, fseek_origin);
return ret;
}


my convertion


fseek_file_func proc opaque, stream, off, origin

mov ecx,0
.if origin == ZLIB_FILEFUNC_SEEK_CUR
mov ecx,FILE_CURRENT
.elseif origin == ZLIB_FILEFUNC_SEEK_END
mov ecx,FILE_END

.elseif origin == ZLIB_FILEFUNC_SEEK_SET
mov ecx,FILE_BEGIN
.else
mov eax,-1
ret
.endif

invoke SetFilePointer,stream,off,0,ecx
xor eax,eax


ret
fseek_file_func endp


I will appreciate any response.Thanks.
Posted on 2002-11-26 14:12:57 by LaptoniC
Hi LaptoniC,

I think you can ignore the "b" part of the fopen flag because WriteFile etc. does no text conversion, so you just need:

"r" = GENERIC_READ with OPEN_EXISTING
"r+" = GENERIC_READ or GENERIC_WRITE with OPEN_EXISTING
"w" = GENERIC_WRITE with CREATE_ALWAYS

e.g. CreateFile,lpFileName,GENERIC_READ or GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL

Your functions look otherwise ok but I just looked briefly.

I hope this helps :)

-stormix
Posted on 2002-11-26 17:28:30 by stormix
Thanks and what you think about below line

.if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)== ZLIB_FILEFUNC_MODE_READ)

masm doesnt accept this.
Posted on 2002-11-26 17:39:06 by LaptoniC
Hi, try


mov eax,mode
and eax,ZLIB_FILEFUNC_MODE_READWRITEFILTER
.if eax == ZLIB_FILEFUNC_MODE_READ


-stormix
Posted on 2002-11-26 17:49:16 by stormix
LaptoniC,
You need to refer to the MS help documentation on the file related APIs. They tell you everything you need to know about how to read, write, and search files. If whatever low level function is not documented there, it probably cannot be done. I am enclosing a do nothing program that shows how a file read program can be called as a subroutine. Notice that it does not use the PROC directives, which I consider needless red tape. Most of the bookkeeping for the local variables and pushed paramenters is done by the STRUC directive. Some of the definitions which are referenced by the APIs can be found . I hope you find the code helpful. Ask if you have questions. Ratch

Posted on 2002-11-26 23:46:26 by Ratch
Interesting coding style Ratch. :)
Posted on 2002-11-27 00:01:38 by bitRAKE
bitRAKE,
I hope you find it interesting and useful. It does not use the EBP register or stack frames which the PROC and INVOKE combinaton do. That is slower and generates more code. My method does most of the work via structure definitions of the pushed stack parameters and the local variables. By the way, you should see my method of dispatching window messages in my window callback procedure. I have not found anyone that does it quite the way I do. Ratch
Posted on 2002-11-27 00:28:21 by Ratch
Ratch: very nice ... when you have time please post your method of dispatching window messages.
Posted on 2002-11-27 03:12:02 by TBD
Thanks for the replies.What I was doing is to remove C runtime libraries from zlib so we can all use zlib in asm.Most io rutines was in ioapi.c.So I thought maybe I can sonvert them to asm.I tried today but it didnt worked.So I will use with C runtime dlls.
Posted on 2002-11-27 08:30:30 by LaptoniC
TBD,
Ok, here is the code in the attached file. The program is a translation of a C program in Charles Petzold's book Programming Windows, 5th edition on p. 539. It is called POPAD, and it is a rudimentary text editor.
The callback routine is called WINCALLBACK. It first manipulates the stack to store the old critical 4 windows registers, and repositions the return address. When finished, the message number, hwnd, wParam, and lParam are in registers ESI,EAX,EBX,and EDX respectively. Then it does a search by REPNE of a table containing all the message numbers the program wants to process. After determining the position in the table of the message, it uses that index for a jump table to go to the proper code. Naturally, the tables must be in sync with each other, so a macro is used to make that happen. So now the program is in the proper message handling code with the message number, wParam, and lParam in registers for quick parameter access. EBP is set to zero because that is the most often used constant. When finished, the code jumps back to WINCALLBACK for cleanup and exit, or further processing by DefWindowProc. Dialog messages via WM_COMMAND are processed the same way. Ask if you have any questions. Comments welcome. Ratch
Posted on 2002-11-27 10:28:03 by Ratch
Posted on 2002-11-27 10:50:45 by bitRAKE
Ratch: i dont want to be grudgy but a smaller example that is compilable with default masm32 package would be nice. a simple window routine, maybe with some controls on it.

it is a nice ideea and i want a simple example to grasp it properly. thanks.
Posted on 2002-11-28 02:22:33 by TBD
TBD,
You are right, I got lazy and sent out something I had quickly available. After this holiday, I will try to put together a simplified example. However, I just wanted to illustrate my method of dispatching WM_MESSAGES. If you only look at WINCALLBACK in my program listing, you will see everything that I wanted to illustrate. Just follow the references from that routine and ignore the rest of the code. You will then see everything I wanted to exhibit. Some of the macros are my own and not available in the MASM32 default package. My next example will have more white space, less clutter, and a better comments. Again, the example will be to illustrate one particular (or is it peculiar) way to dispatch window messages, and not try to show how to do certain applications in Win32. Ratch
Posted on 2002-11-28 10:36:02 by Ratch
TBD,
OK, here is the simplified example. I will be away on vacation from 11/30/02 until 12/7/02. You have one day for comment or questions until I leave. Ratch
Posted on 2002-11-29 02:05:07 by Ratch
thanks Ratch ! well commented :alright:
... but to be complete your macro file would be nice :)

btw, have a nice holiday ...
Posted on 2002-11-29 02:13:09 by TBD
TBD,
OK, here it is. Thanks for the good wishes. Ratch
Posted on 2002-11-29 02:46:59 by Ratch
wow, that was quick :)
but i meant for RPUSHIT, POPIT,...

do you have time to make a small one window example (as i stated a bit ago) that can be compiled with the standard masm32 package (all files needed included in the zip) ?

i think this would be a nice example for the hutch's next masm32 version.
Posted on 2002-11-29 03:00:15 by TBD
TBD,
Pick one of Hutch's tutorials, or send a C code example, and I will try to knock it off tomorrow. Nothing too big, of course. Ratch
Posted on 2002-11-29 03:06:02 by Ratch
here is a listbox example (6k zip) ... i pick it because is more than a simple window :)
Posted on 2002-11-29 03:27:40 by TBD
TBD,
Got back from my vacation yesterday. Had a good time. Here is my implementation of the program you sent a week ago. Notice that I use the stack for the WNDCLASSEX structure. As requested, all the files needed to generate this code should be contained in the Zip file or MASM32. Ask if you have any questions or comments. Ratch
Posted on 2002-12-08 16:10:59 by Ratch