News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

WriteFile Api Call help

Started by Ash_, February 11, 2005, 12:24:38 PM

Previous topic - Next topic

Ash_

ok, i've written a function to load a Resource thats compiled into the exe.

it all works great as far as i know but i get an error with WriteFile.

i know this because when i comment it out it creates the file, its only 0kb though.
heres my code, thanks for your help.


LoadAndSaveResources proc
LOCAL hLoad:HRSRC
LOCAL hRes:HRSRC
LOCAL hMod:HMODULE
LOCAL x:DWORD
LOCAL bytes:DWORD
LOCAL hMake:HANDLE
LOCAL ResLock:DWORD
invoke lstrcpy, addr file, addr Dir
invoke lstrcat, addr file, addr Filename
invoke GetModuleHandle, NULL
mov hMod, eax
invoke FindResource, hMod,500, RT_RCDATA
mov hRes, eax
invoke LoadResource, NULL, hRes
mov hLoad, eax
invoke SizeofResource,NULL,hRes
mov x, eax
invoke LockResource, hLoad
mov ResLock, eax

invoke CreateFile ,addr file,GENERIC_WRITE,FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
mov hMake, eax
invoke WriteFile, hMake, ResLock, x, bytes, NULL
invoke CloseHandle, hMake
ret
LoadAndSaveResources endp


it compiles correctly and the the Filename and Dir have been pre-filled

Dir db "C:\", 0
Filename db "Testfile.dll", 0


and all the buffers have been declared as 512.

thanks for any help in advance.

Mirno

invoke WriteFile, hMake, ResLock, x, ADDR bytes, NULL

Mirno

Ash_

#2
Thanks for the help Mirno, it wirtes with no problem now.

but it seems as my LoadResource code is wrong, as the File is still only 0kb.

can you see any problems?

pbrennick

Ash,
I suspect that invoke SizeofResource,NULL,hRes
is returning zero in eax, I wonder if you should try
invoke SizeofResource,hLoad,hRes ?
In any case, you should test if you are getting a valid value in EAX after that call instead of assuming it will be correct.
Paul


Ash_

your right pbrennick, the return from GetResourceSize is NULL.

this means im no loading the Resource correctly.

pbrennick

Ash,
Yeah, let us know how you make out.  Start by doing some testing after each call.  Use a MessageBox to look at the values so you wont be working blind.  It is a very effective troubleshooting method that I always use.
Paul

Ash_

cheers pbrennick, i narrowed it down to the call FindResource.

instead of the RT_RSDATA constant i needed that argument to point to a varaible containing 'DISCARDABLE'

my rsrc.rc code was.

200     DISCARDABLE                     "Mydll.dll"


and the API Call was.


INVOKE FindResource, hMod, 200, addr RsrcType


and in my data section i needed

RsrcType        db "DISCARDABLE", 0


thanks for the help Mirno and pbrennick.

pbrennick

Ash,
I'm glad you solved your problem and that we were able to help you.  Most times it is difficult to help when we only get bits and pieces of the problem, that is why we end up guessing.  It is a good feeling when my guess is either on or close to the mark.

Paul

P1

Here is a complete solution.

It's font installer, but the principles are the same.

You have to supply your own font file for the example.

Regards,  P1  :8)

pbrennick

P1,
I think you forgot something.

Paul

Ash_

yeh, sorry just for future probelms exactly how much of the code did you need to be able to find the problem?

i spose i should have said what i put in my resource file.
and P1 i dont think you uploaded it, because i cant find it.

thanks for everyone's help.

pbrennick

Ash,
Unless you are working on something that you plan to offer for sale, when you have a problem; it is a good idea to attach the complete project so we can properly test it.  In this case, because we did not know what was in the resource file it was all guess work.  Still, we always try to help.

Paul

P1

Here's second try.

Regards,  P1  :8)

[attachment deleted by admin]