I have a legacy application written in Delphi, and need to build a mechanism for
- reading and
- writing
data from/to a TStringGrid.
I don't have the source code of the application, there is no automation interface and it is very unlikely that the vendor will provide one.
Therefore I've created
- a C++ DLL, which injects
- a Delphi DLL (written by me) into
- the address space of the legacy application.
DLL 2 gets access to the TStringGrid instance inside the legacy application, reads cell values and writes them into the debug log.
Reading works fine. But, when I try to write data into a grid cell using a call like
realGrid.Cells[1,1] := 'Test';
an access violation occurs.
Here's the code:
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*)
How can I write data into a TStringGrid without getting access violation problem?
Cells
property fails, why would callingSetCells
be different? You can presumably readSetCells
with class helper to crack the private visibility. VMT won't help becauseSetCells
is not virtual. – David Heffernan