如何用API函数实现对无类型文件的读取,请给出例子!(100分)

  • 主题发起人 主题发起人 ynfly
  • 开始时间 开始时间
Y

ynfly

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; 我需要编写一个DLL在Lotus Notes中调用(Lotus Notes中的lsScript语法跟VB的语法可以说一样的)<br>此DLL的有一个重要的功能是读取源文件的部分信息然后存放到一个目标文件中,其中我要到了BlockRead<br>函数,编写完成后,我把此DLL拷贝到系统目录下面,然后在word的宏里面声明此Dll中的所有引出的函数,调用后,<br>结果均正常,然后在Notes里面建立一个数据库,然后在表单中声明此DLL中引出的所有函数,调用后发现<br>其他的函数结果正常,就是一个包含了BlockRead函数的引出函数调用结果不正常,下面是有问题的函数的代码:<br>function setWpgFiletoBmp(srcFileName,dstFileName,strPassword:string):integer;stdcall;<br>&nbsp; var<br>&nbsp; srcTypedFile:File;<br>&nbsp; decTypedFile:File;<br>&nbsp; BytesRead,BytesWritted:Integer;<br>&nbsp; Buffer:array[0..15] of &nbsp;byte;<br>&nbsp; strTruePassWord:string;<br>&nbsp; key:Word;<br>&nbsp; begin<br>&nbsp; &nbsp; Result:=0;<br>&nbsp; &nbsp; AssignFile(srcTypedFile,srcFileName);<br>&nbsp; &nbsp; AssignFile(decTypedFile,dstFileName);<br>&nbsp; &nbsp; if fileexists(dstFileName ) then<br>&nbsp; &nbsp; &nbsp; reset(decTypedFile)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; rewrite(decTypedFile);<br>&nbsp; &nbsp; &nbsp;reset(srcTypedFile,1);<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repeat <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//经通过设计标记得方法,发现在Notes中执行时,下面代码不能运行<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BlockRead(srcTypedFile,buffer,SizeOF(buffer),BytesRead);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if BytesRead&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlockWrite(decTypedFile,buffer,BytesRead,BytesWritted);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if BytesRead&lt;&gt;BytesWritted then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('Error Copying file')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;until BytesRead=0;<br>&nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp;Erase(decTypedFile);<br>&nbsp; &nbsp; &nbsp;raise;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; closefile(decTypedFile);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; closefile(srcTypedFile);<br>&nbsp; end;<br>&nbsp; &nbsp; &nbsp; Result:=1<br>&nbsp; end;<br><br><br>我想问的是1、BlockRead函数的调用是否有问题?<br> &nbsp;2、如何用API函数的调用的方法代替BlockRead方法实现对无类型文件的读取,最好给出例子代码?
 
各位高手请积极发言,不吝赐教
 
给你一个用API写的COPY文件的粒子,或许会有帮助<br>function TFileOpertionClass.doFileCopy(Src,Tar:string;Pos:int64=0):int64;<br>const<br>&nbsp; MaxBufSize = $80000;<br>var<br>&nbsp; BufSize,N : DWORD;<br>&nbsp; Buffer: PChar;<br>&nbsp; FSrcHandle,FTarHandle:THandle;<br>&nbsp; hi,lo:DWORD;<br>begin<br>&nbsp; result:=-1;<br>&nbsp; FSrcHandle:=CreateFile(PChar(Src),GENERIC_READ,0,nil,OPEN_EXISTING,<br>&nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN,0);<br>&nbsp; if FSrcHandle=INVALID_HANDLE_VALUE then<br>&nbsp; begin<br>&nbsp; &nbsp; showmessage('Open Source Failed!!Exit!');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; showmessage('Open Src successfully!!');<br>&nbsp; try<br>&nbsp; &nbsp; FTarHandle:=CreateFile(PChar(Tar),GENERIC_WRITE,0,nil,OPEN_ALWAYS,<br>&nbsp; &nbsp; &nbsp; &nbsp;GetFileAttributes(PChar(Src)) or FILE_FLAG_SEQUENTIAL_SCAN,0);<br>&nbsp; &nbsp; if FTarHandle=INVALID_HANDLE_VALUE then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('Open Target Failed!!Exit!');<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; showmessage('Open Target successfully!!');<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; N:=0;<br>&nbsp; &nbsp; &nbsp; sCount:=FGetSize(FSrcHandle);<br>&nbsp; &nbsp; &nbsp; if sCount&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if pos&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tCount:=FGetSize(FTarHandle);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if (Pos&lt;sCount) and (Pos&lt;=tCount) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lo:=Pos and $FFFFFFFF;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hi:=(Pos and $FFFFFFFF00000000) shr 8;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sCount:=sCount-Pos;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=scount;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if sCount &gt; MaxBufSize then BufSize := MaxBufSize else BufSize := sCount;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetMem(Buffer,MaxBufSize);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetFilePointer(FSrcHandle, lo, @hi, FILE_BEGIN);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetFilePointer(FTarHandle, lo, @hi, FILE_BEGIN);<br>// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;showmessage(inttostr(lo));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while sCount&lt;&gt;0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not ReadFile(FSrcHandle, Buffer^, BufSize, N, nil) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage('Read Source Error!!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not WriteFile(FTarHandle,Buffer^,N,N,nil) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage('Write Target Error!!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dec(sCount,N);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeMem(Buffer);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=sCount;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; if not CloseHandle(FTarHandle) then<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage('Close Target Failed!!');<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; if not CloseHandle(FSrcHandle) then<br>&nbsp; &nbsp; &nbsp; showmessage('Close Source Failed!!');<br>&nbsp; end;<br>end;
 
新年帮助你ding!<br>
 
虽然没有太大帮助,但我已经知道了,谢谢
 
后退
顶部