Hi
I write a procedure,about file operation. 98/2k it works normally.
but in xp, it couldnt read,(but can write), I thought it is ReadFile API causes, therefor i write a dll using MFC CFile to instead of it.

i pass file handle hFile to dll function, use CFile::CFile(int hFile) to construct.and it works,under 98/2k.
then it occurs c++ runtime err in XP.

i don't know why and i tried my best.
.....
invoke ReadFile,htmpfile,addr tmpstr,35,addr unuse,0
....
the code read null from file.

.............
extern "C" UINT PASCAL EXPORT fnRandReadFile(int hFile,void* lpTmpStr,long offset)
{
UINT nStrlen = 0;

CFile cf = CFile::CFile(hFile);
cf.Seek(offset,CFile::begin);
nStrlen = cf.Read(lpTmpStr,35);


return nStrlen;

}
.........................
the function told me runtime err.

.........
invoke LoadLibrary,addr libName
mov hLib,eax
invoke GetProcAddress,hLib,addr funcName
mov hFunc,eax
...
push tmpoffset
lea eax,tmpstr
push eax
push htmpfile
call
........
the code i call function in dll.


thanks
Terry
Posted on 2002-01-17 11:45:58 by xqterry
i noticed your using GetProcAddress so check out this thread: http://www.asmcommunity.net/board/index.php?topic=2240&highlight=GetProcAddress . there appears to be some issues when using GetProcAddress with windows xp.
:confused:
Posted on 2002-01-17 12:10:44 by smurf
I looked at the thread on GetProcAddress, and it seems that there aren't any problems with it.

However, I do notice that you use the PASCAL calling convention. I am not really knowledgeable in this area, but that might mess you up. I think you need STDCALL instead.

I think the best thing you can do is use a debugger and make sure the parameters are pushed correctly and check every little instruction to make sure it is doing what it is supposed to do.

Just a little side-note: Last night I spent one hour debugging an API call because I used STD and forgot to use CLD after that. :( Fun stuff!
Posted on 2002-01-17 14:33:22 by Hel
xqterry, do you test all API returns for error? (especially CreateFile
in this case). Have you tried tracing through the code with a debugger? (OllyDbg).
Is your buffer big enough? Does the ReadFile work when you import
it normally (ie, don't use GetProcAddress)? Can you open the file
in say a text editor (it might be open with write only share mode).
Do your user account have permission to access the file?

Smurf, GetProcAddress works fine on XP. S? fat det dog!
Posted on 2002-01-17 16:03:30 by f0dder
Hi

smurf
I think GetProcAddress works fine on xp too,same as f0dder.

Hel
i changed it to __stdcall and i change params order,but it still works on 2k only.

f0dder
the dbg i used is softice,i compiled my procedure by debugtype:cv,but i couldnt found any errs on 2k.
i have no xp sys and i won't buy one,my partner uses it,
hehe,it seems we troubled ourselves.

have you ever used CreateFile api on xp?
I didnt use Alloc Memory for File read,just SetFilePointer and read,on xp,it returns 0,and though buffer is big enough,it is null.

Terry
Posted on 2002-01-17 21:13:09 by xqterry
Yes, I had XP installed (briefly) before going back to win2k, and
CreateFile, ReadFile (and friends) worked perfectly. Otherwise about
95% of the programs I use daily would have failed :tongue: .
Get yourself a copy of OllyDbg and trace your code at your friends
machine (it's easier getting OllyDbg working on XP than softice...
besides, you don't need to ring0 power for this task, and OllyDbg
doesn't require a license).

Perhaps you should post a .asm file with your entire problematic
program, if it's not too large.
Posted on 2002-01-18 05:00:36 by f0dder
f0dder

the code i put file name pointer and file handle in memory:
--------------------------------------------------------------------------
invoke SetCurrentDirectory,addr szRandDir


invoke SendMessage,hListBox,LB_GETCOUNT,0,0
mov nfilecount,eax
mov ebx,eax
;;; Get files count in the listBox.

INVOKE GlobalAlloc,0,512
mov hash,eax
INVOKE GlobalLock,hash
mov filehandle,eax
mov edx,eax
lea ecx,buffer
;;; alloc memory for store pointer of name and handle,and i am sure all of them is less than 512 bytes.




@next: dec ebx
push ebx

mov ,ecx

;;;; store name pointer.

push ecx
push edx
invoke SendMessage,hcombo,LB_GETTEXT,ebx,ecx

pop edx
pop ecx

push ecx
push edx
push ebx
invoke CreateFile,ecx,\
GENERIC_READ,\
FILE_SHARE_READ,\
NULL,\
OPEN_EXISTING,\
FILE_ATTRIBUTE_NORMAL,\
NULL

pop ebx
pop edx

mov ,eax

;;; store file handle

pop ecx
push ecx
invoke lstrlen,ecx
add eax,1
pop ecx
add ecx,eax

pop ebx
cmp ebx,0
jg @next
----------------------------------------------------------------

then, 2k and 98 could get handle from memory block correctly later on, but xp always take out 0, sometime is -1.

And, I used filehandle[512]:dword not alloc memory for it. same,it only doesn't work on xp.

could you tell me what is the matter?

thanks

Terry
Posted on 2002-01-18 08:21:21 by xqterry
lstrlen may have changed.

You don't preserve EDX, and a compiler with a different optimization setting may use EDX when building lstrlen.
Posted on 2002-01-18 18:35:56 by tank
tank

Thanks,it works now.
to my surprise,xp is so rigorous to manage memory.
it shows how careless i was too.
:o

thanks for everybody.

Terry
Posted on 2002-01-19 00:04:01 by xqterry