0

I'm trying out to read window text (a console kind of window) using my application. This is how i do it.

1. Send Ctrl+a and then Ctrl + C using 'sendkey' to terminal

// Ctrl + a       
keybd_event(VK_CONTROL, 0, 0, 0);
fn_SendKeys('a');
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);

// Ctrl + c
keybd_event(VK_CONTROL, 0, 0, 0);
fn_SendKeys('c');
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);

2. Once copied, it is saved to a txt file from the clipboard

function Tform1.fn_Save_Clipboard_Text_ToFile(sFileTo: string): Boolean;
var
  success: Boolean;
  counter: Integer;
begin
  success := False; 
  counter := 1; 

  while ((not success) and (counter < 6)) do
  begin
    Result := Clipboard.HasFormat(CF_UNICODETEXT);
    if Result then
    try
      TFile.WriteAllText(sFileTo, Clipboard.AsText);
      success := True; 
    except
      on Exception do
      begin
        fn_delay(1);
        counter := counter + 1;
      end;
    end;
  end;
end;

3. process the information in txt file by reading it

Everything works perfectly, except when i do a Ctrl + C on a memo, within my application, prior to sending Ctrl + c to window. When i do the Ctrl + C within memo, and try to save the clip board as text in my application, it throws exception saying "cannot open clipboard - access denied".

It works 100% perfectly when i do not do the copy operation on the memo prior to screen copy. The strange thing is, it fails 100% if i do the copy/paste in memo prior to screen read.

Could anyone please help me to identify, what am I doing wrong here?

jimsweb
  • 1,082
  • 2
  • 17
  • 37
  • 2
    `keybd_event()` is deprecated, use `SendInput()` instead. Are you able to reproduce this problem reliably without simulating key presses? If you just copy/paste some text on the Memo and then read `Clipboard.AsText`, without involving the mainframe window, does it still fail every time? If so, [file a bug report](http://quality.embarcadero.com) with Embarcadero. In any case, it would probably be better, safer, and easier to use `WM_GETTEXT` to extract the desired text directly from the mainframe window without involving the clipboard at all. – Remy Lebeau Feb 06 '16 at 02:24
  • 2
    You may also want to experiment with sending a [WM_COPY](https://msdn.microsoft.com/en-us/library/windows/desktop/ms649022(v=vs.85).aspx) to the source window. – 500 - Internal Server Error Feb 06 '16 at 02:26
  • @500-InternalServerError: and along those lines, check if [`EM_SETSEL`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb761661.aspx) works, too. – Remy Lebeau Feb 06 '16 at 02:28
  • @remy i tried out your suggestion and found that the terminal window is the culprit. It holds the clipboard exclusively / does something peculiar and it prevents the application from accessing clipboard. regarding WM_gettext, will definitly try out this option. – jimsweb Feb 06 '16 at 03:05
  • @jimsweb: if what you say is true, the code you showed would not have worked in the first place, regardless of anything you do with the Memo, unless the mainframe window lets go after a delay before you try to save the file. You might try calling [`GetOpenClipboardWindow()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms649044.aspx) in a loop for a few seconds and if the mainframe window does lets go of the clipboard THEN you can access it. Of course, this issue is moot if you find another solution that doesn't involve using the clipboard. – Remy Lebeau Feb 06 '16 at 03:12
  • this what happens with mainframe window: Do a copy in memo, then go to mainframe terminal window, do a ctrl +a and ctrl +C and then open a note pad paste it.!! To my surprise it fails. it wont work initially, but it will work after 2-4 seconds. If i do not perform a copy in memo, i can paste it in notepad without any issues. – jimsweb Feb 06 '16 at 03:23
  • 2
    Please don't keep adding details in comments. You should [edit] your question to provide that information there instead, where it's visible to people who are trying to help you. Put the information in your question, rather than burying it in comments. – Ken White Feb 06 '16 at 07:44
  • And what about the solution, that doesn't (ab)use the clipboard? The clipboard belongs to the user, not your application, and you should not be thrashing data the user may wish to keep. – IInspectable Feb 06 '16 at 16:28

0 Answers0