hakanfa
asked on
Getting data from another application..
Hello there Experts!
Is it possible to get data from another external application not build in Delphi?
I have an application written in Visual Basic from which i would like to get data
(string) from its TextEdit fields. I have no possibilities to change the source on
the VB side so all possible programming is to be done in Delphi..
Is this possible ??
Hokki
Yes. You can read from TextEdit fields if you know its handlers
Here is sample code:
var
Form1: TForm1;
g_w_handle: HWND;
g_w_cap: string;
g_stage: longint;
g_str: array[0..2048] of char;
function EnumChildsFunc(wnd: HWND; lParam: longint): longint; stdcall;
var Res: integer;
str: array[0..1024] of char;
begin
Res := GetWindowText(wnd, g_str, 2048);
case g_stage of
1: begin
if g_str[0] = #0 then begin
if StrComp(@g_str[1], 'First Name:') = 0 then begin
StrPCopy(str, 'alex');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Last Name:') = 0 then begin
StrPCopy(str, 'm');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Gender:') = 0 then begin
SendMessage(wnd, CB_SETCURSEL, 1, 0);
end else if StrComp(@g_str[1], 'Zip / Postal Code:') = 0 then begin
StrPCopy(str, '22222');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Occupation:') = 0 then begin
SendMessage(wnd, CB_SETCURSEL, 2, 0);
end;
end else if ((StrComp(@g_str[0], '&Contact me from time to time about specials and new products') = 0) or
(StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0)) then begin
SendMessage(wnd, BM_CLICK, 0, 0);
end;
end;
2: begin
if g_str[0] = #0 then begin
if StrComp(@g_str[1], 'reate My Own') = 0 then begin
StrPCopy(str, Form1.Edit1.Text);
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'e&type Password:') = 0 then begin
StrPCopy(str, '123123');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Password:') = 0 then begin
StrPCopy(str, '123123');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end;
end else if ((StrComp(@g_str[0], 'Create My Own') = 0) or
(StrComp(@g_str[0], '&Remember my ID and Password') = 0) or
(StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0)) then begin
SendMessage(wnd, BM_CLICK, 0, 0);
end;
end;
3: begin
if g_str[0] = #0 then begin
if StrComp(@g_str[1], 'Question we''ll ask:') = 0 then begin
SendMessage(wnd, CB_SETCURSEL, 2, 0);
end else if StrComp(@g_str[1], 'our &answer:') = 0 then begin
StrPCopy(str, '1234567890');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'our &birthday:') = 0 then begin
StrPCopy(str, '01/01/1975');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'urrent &Email Address:') = 0 then begin
StrPCopy(str, 'alexn911@hotmail.com');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end;
end else if (StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0) then begin
SendMessage(wnd, BM_CLICK, 0, 0);
end;
end;
end;
end;
procedure TForm1.SpeedButton1Click(S ender: TObject);
begin
if FindWindowByCap('Window caption') <> HWND(-1) then begin
g_stage := 1;
FillMemory(@g_str[0], 2048, BYTE('#'));
EnumChildWindows(g_w_handl e, @EnumChildsFunc, 0);
end;
end;
function FindWindowByCap(text: string): HWND;
begin
g_w_handle := HWND(-1);
g_w_cap := text;
EnumWindows(@EnumFunc, 0);
Result := g_w_handle;
end;
function EnumFunc(wnd: HWND; lParam: longint): longint; stdcall;
var str: array[0..2048] of char;
Res: integer;
begin
Res := GetWindowText(wnd, str, 2048);
if ((Res > 0) and
(CompareStr(g_w_cap, StrPas(str)) = 0)) then begin
g_w_handle := wnd;
Result := 0;
end else begin
Result := 1;
end;
end;
var
Form1: TForm1;
g_w_handle: HWND;
g_w_cap: string;
g_stage: longint;
g_str: array[0..2048] of char;
function EnumChildsFunc(wnd: HWND; lParam: longint): longint; stdcall;
var Res: integer;
str: array[0..1024] of char;
begin
Res := GetWindowText(wnd, g_str, 2048);
case g_stage of
1: begin
if g_str[0] = #0 then begin
if StrComp(@g_str[1], 'First Name:') = 0 then begin
StrPCopy(str, 'alex');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Last Name:') = 0 then begin
StrPCopy(str, 'm');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Gender:') = 0 then begin
SendMessage(wnd, CB_SETCURSEL, 1, 0);
end else if StrComp(@g_str[1], 'Zip / Postal Code:') = 0 then begin
StrPCopy(str, '22222');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Occupation:') = 0 then begin
SendMessage(wnd, CB_SETCURSEL, 2, 0);
end;
end else if ((StrComp(@g_str[0], '&Contact me from time to time about specials and new products') = 0) or
(StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0)) then begin
SendMessage(wnd, BM_CLICK, 0, 0);
end;
end;
2: begin
if g_str[0] = #0 then begin
if StrComp(@g_str[1], 'reate My Own') = 0 then begin
StrPCopy(str, Form1.Edit1.Text);
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'e&type Password:') = 0 then begin
StrPCopy(str, '123123');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'Password:') = 0 then begin
StrPCopy(str, '123123');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end;
end else if ((StrComp(@g_str[0], 'Create My Own') = 0) or
(StrComp(@g_str[0], '&Remember my ID and Password') = 0) or
(StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0)) then begin
SendMessage(wnd, BM_CLICK, 0, 0);
end;
end;
3: begin
if g_str[0] = #0 then begin
if StrComp(@g_str[1], 'Question we''ll ask:') = 0 then begin
SendMessage(wnd, CB_SETCURSEL, 2, 0);
end else if StrComp(@g_str[1], 'our &answer:') = 0 then begin
StrPCopy(str, '1234567890');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'our &birthday:') = 0 then begin
StrPCopy(str, '01/01/1975');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end else if StrComp(@g_str[1], 'urrent &Email Address:') = 0 then begin
StrPCopy(str, 'alexn911@hotmail.com');
SendMessage(wnd, WM_SETTEXT, 0, integer(@str[0]));
end;
end else if (StrComp(@g_str[0], '„Ÿ„‚„„‡„‡ >') = 0) then begin
SendMessage(wnd, BM_CLICK, 0, 0);
end;
end;
end;
end;
procedure TForm1.SpeedButton1Click(S
begin
if FindWindowByCap('Window caption') <> HWND(-1) then begin
g_stage := 1;
FillMemory(@g_str[0], 2048, BYTE('#'));
EnumChildWindows(g_w_handl
end;
end;
function FindWindowByCap(text: string): HWND;
begin
g_w_handle := HWND(-1);
g_w_cap := text;
EnumWindows(@EnumFunc, 0);
Result := g_w_handle;
end;
function EnumFunc(wnd: HWND; lParam: longint): longint; stdcall;
var str: array[0..2048] of char;
Res: integer;
begin
Res := GetWindowText(wnd, str, 2048);
if ((Res > 0) and
(CompareStr(g_w_cap, StrPas(str)) = 0)) then begin
g_w_handle := wnd;
Result := 0;
end else begin
Result := 1;
end;
end;
ASKER
Well thank You, but one problem.. I would like to receive (get) data from the other application..
So lets say, I have an application (VB) with caption "DemoForm" and a textedit "DemoText" with a text in it
like "Hello!". How do I get this text into my application..??
Hokki
So lets say, I have an application (VB) with caption "DemoForm" and a textedit "DemoText" with a text in it
like "Hello!". How do I get this text into my application..??
Hokki
;-)
Just use WM_GETTEXT instead WM_SETTEXT like that:
SendMessage(wnd, WM_GETTEXT, 64, integer(@str[0]));
// Hint From Help
SendMessage(
(HWND) hWnd, // handle to destination window
WM_GETTEXT, // message to send
(WPARAM) wParam, // number of characters to copy
(LPARAM) lParam // text buffer
);
Just use WM_GETTEXT instead WM_SETTEXT like that:
SendMessage(wnd, WM_GETTEXT, 64, integer(@str[0]));
// Hint From Help
SendMessage(
(HWND) hWnd, // handle to destination window
WM_GETTEXT, // message to send
(WPARAM) wParam, // number of characters to copy
(LPARAM) lParam // text buffer
);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By the way could you give me your demo application and I'll write cade sample for it.
ASKER
Very kind of you.. Email?
ASKER
And yes one more thing.. (I've been doin some studing here) is it possible to get the name och the component? Not the ClassName but the name? How is it otherwise possible to know from where the data is retrieved? Like if the "external" application has lets say 3 textedits (FirstName, FamilyName, Address), and I would like to pick up only the address text and put it in my application..??
Thanks,Hokki
Thanks,Hokki
eric at f2w.net
ASKER
Well GloomyFriar I can not get the email through with that address?
>: Recipient address rejected: This user does not have an account here (MTA:imta13))
Can you please send me a "blanco" email so I get the correct address...
may address hakan.fagerstrom at dg-office.net
Thanks,
Hokki
>: Recipient address rejected: This user does not have an account here (MTA:imta13))
Can you please send me a "blanco" email so I get the correct address...
may address hakan.fagerstrom at dg-office.net
Thanks,
Hokki