我在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函數。 –