内存文件读取,如何把c++代码转化为delphi代码(附源码)?(50分)

  • 主题发起人 主题发起人 liu_sir
  • 开始时间 开始时间
L

liu_sir

Unregistered / Unconfirmed
GUEST, unregistred user!
内容: <br>HANDLE s_hFileMap; <br>&nbsp; &nbsp; LPVOID lpView; <br>1。打开 <br>&nbsp; s_hFileMap = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FALSE,"xmldkd"); <br>&nbsp; &nbsp; if(s_hFileMap != 0) <br>&nbsp; &nbsp; &nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp;lpView = MapViewOfFile(s_hFileMap, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_MAP_READ | FILE_MAP_WRITE,0,0,0); <br>&nbsp; &nbsp; &nbsp;if (lpView == NULL) <br>&nbsp; &nbsp; &nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; ShowMessage("Can not open file mapping."); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return; <br>&nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp; &nbsp; &nbsp; &nbsp; } <br>&nbsp; &nbsp; else <br>&nbsp; &nbsp; &nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage("不能打开实时库"); <br><br>&nbsp; &nbsp; &nbsp; &nbsp; } <br><br>2。请问如何读取(用delphi) <br><br>a=((PFLOAT)lpView)[n];n字节数,如何转化??? <br>读取内存文件的n个字节起的float量(4字节)?<br><br>
 
1<br>Procedure .............<br>var s_hFileMap:HANDLE; <br>&nbsp; &nbsp; lpView:pointer; <br>begin<br>&nbsp; s_hFileMap:=OpenFileMapping(FILE_MAP_READ or FILE_MAP_WRITE,FALSE,'xmldkd'); <br>&nbsp; if(s_hFileMap &lt;&gt; 0) then<br>&nbsp; begin<br>&nbsp; &nbsp; lpView:= MapViewOfFile(s_hFileMap,FILE_MAP_READ or FILE_MAP_WRITE,0,0,0);<br>&nbsp; &nbsp; if Assigned(lpView) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('Can not open file mapping.'); <br>&nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; end<br>&nbsp; else ShowMessage('不能打开实时库'); <br>end;<br><br>2<br>var a:^Real;<br>begin<br>&nbsp; a=^lpView[n];<br>end;
 
1 &nbsp;lpView 应该是记录型的指针!<br>type<br>&nbsp; TSharedData = record<br>&nbsp; &nbsp; ...<br>&nbsp; &nbsp; i: String[n];<br>&nbsp; end;<br>&nbsp; PSharedData &nbsp; =^TSharedData;<br><br>var<br>&nbsp; s_hFileMap : THandle;<br>&nbsp; lpView : PSharedData;<br>&nbsp; <br>&nbsp; s_hFileMap :=OpenFileMapping(FILE_MAP_READ or FILE_MAP_WRITE, False, 'xmldkd'); <br>&nbsp; if s_hFileMap =0 then Exit;<br>&nbsp; lpView :=MapViewOfFile(s_hFileMap, FILE_MAP_READ or FILE_MAP_WRITE, 0, 0, 0);<br>&nbsp; if lpView =Nil then<br>&nbsp; begin &nbsp; <br>&nbsp; &nbsp; ShowMessage('Can not open file mapping.'); &nbsp;<br>&nbsp; &nbsp; CloseHandle(s_hFileMap);<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br>&nbsp; else<br>&nbsp; &nbsp; ShowMessage('不能打开实时库'); &nbsp;<br>&nbsp; UnMapViewOfFile(lpView);<br>&nbsp; CloseHandle(s_hFileMap);<br>end;<br>&nbsp; <br>2<br>Label1.Caption:=SharedData^.i; &nbsp; &nbsp; //这样可以读取指定数据<br>SharedData^.i:=Edit1.text; &nbsp; &nbsp; //这样可以修改指定数据
 
请问二位大侠: <br>在delphi中,这样定义:<br>&nbsp; var &nbsp; lp_view &nbsp;:^single;//地址指针<br>&nbsp; &nbsp; &nbsp; lp_view:=mapviewoffile(f_handle,FILE_MAP_READ,0,0,0);<br>读取:<br>&nbsp;var &nbsp; &nbsp;value:^single;<br><br>begin<br>&nbsp; &nbsp; &nbsp; value:=^lp_view[count];<br>end;<br>提示error:<br>&nbsp; &nbsp;incompantable types:'char'and 'pointer',这是为何?<br><br>
 
value:=^lp_view[count];<br>这种写法是错误的,可以这样写:<br>value:=Pointer(Integer(lp_view) + count * sizeof(Single));
 
sgyzh大侠,按您的方法调试通过,小d天资愚笨:指针地址<br>是顺序存放得吗,<br>&nbsp; value:=Pointer(Integer(lp_view) + count * sizeof(Single));<br>是移动记录到从lp_view的指向位置,后移count个single量位置,然后指向第count个single<br>量?integer(lp_view)是什么意思,不太明白,可以具体解释一下么?
 
多人接受答案了。
 
后退
顶部