我在Delphi编写的遗留应用程序,并需要建立一种机制,将数据写入德尔福TStringGrid
- 阅读和
- /写入
数据到TStringGrid。
我没有应用程序的源代码,没有自动化接口,供应商也不太可能提供它。
所以我创建
- 一个C++ DLL中注入
- 德尔福DLL(我写的),为
- 遗留应用程序的地址空间。
DLL 2可以访问传统应用程序中的TStringGrid实例,读取单元格值并将它们写入调试日志。
阅读正常。但是,当我尝试使用类似于
realGrid.Cells[1,1] := 'Test';
的呼叫将数据写入网格单元时,会发生访问冲突。
下面的代码:
procedure DllMain(reason: integer) ;
type
PForm = ^TForm;
PClass = ^TClass;
PStringGrid = ^TStringGrid;
var
[...]
begin
if reason = DLL_PROCESS_ATTACH then
begin
handle := FindWindow('TForm1', 'FORMSSSSS');
formPtr := PForm(GetVCLObjectAddr(handle) + 4);
if (not Assigned(formPtr)) then
begin
OutputDebugString(PChar('Not assigned'));
Exit;
end;
form := formPtr^;
// Find the grid component and assign it to variable realGrid
[...]
// Iterate over all cells of the grid and write their values into the debug log
for I := 0 to realGrid.RowCount - 1 do
begin
for J := 0 to realGrid.ColCount - 1 do
begin
OutputDebugString(PChar('Grid[' + IntToStr(I) + '][' + IntToStr(J) + ']=' + realGrid.Cells[J,I]));
// This works fine
end;
end;
// Now we'll try to write data into the grid
realGrid.Cells[1,1] := 'Test'; // Crash - access violation
end;
end; (*DllMain*)
我怎样才能将数据写入TStringGrid没有得到访问冲突问题?
+1 - 好问题! – Jeff
+1 - 好方案! – CloudyMarble
您是否设法同步对象树和内存映射?在DLL和EXE中有两个不同的TObject类,两个不同的GetMem函数。 –