急啊--为什么ReadFile老是调用不成功啊???(50分)

  • 主题发起人 主题发起人 sunman
  • 开始时间 开始时间
S

sunman

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在程序运行时把本程序加载到一个内存地址里,下面的代码为什么不对啊??<br>请高手指点!!<br>var<br>&nbsp; hFile,hMemory:Cardinal;<br>&nbsp; OwnFileName:array[0..255]of char;<br>&nbsp; dwSize,NumberOfBytesReadWritten:Dword;<br>&nbsp; pMemory:pointer;<br>&nbsp; a:longbool;<br>begin<br>&nbsp; // strpcopy(OwnFileName,Application.ExeName );<br>&nbsp; GetModuleFileName(hinstance,OwnFileName,256);<br>&nbsp; hFile:=CreateFile(OwnFileName,GENERIC_READ, FILE_SHARE_READ,<br>&nbsp; &nbsp; &nbsp; nil,OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE,0);<br>&nbsp; dwSize:=GetFileSize(hFile,nil);<br>&nbsp; hMemory:=GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT,dwSize);<br>&nbsp; pMemory:=GlobalLock(hMemory);<br>&nbsp; a:=ReadFile(hFile,pMemory,dwSize,NumberOfBytesReadWritten,nil);<br>&nbsp; &nbsp; &nbsp;//上句ReadFile总是返回False啊!!<br>&nbsp; CloseHandle(hFile);<br>end;<br>
 
hFile返回值正确不?
 
hFile返回没有问题啊!!
 
我已经搞定了!!
 
hFile:=CreateFile(OwnFileName,GENERIC_READ, FILE_SHARE_READ,<br>&nbsp; &nbsp; &nbsp;nil,OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE,0);<br>为什么用CreateFile?换成fileopen试试。
 
啊呀,有分不? &nbsp; &nbsp;
 
看不大懂代码,不过我想我会这样写<br>getmem(pmemory,dwsize);<br>readfile(hfile,pmemory^,dwsize);
 
我自己已经解决了,不过还是谢谢大家,散分!
 
多人接受答案了。
 
后退
顶部