Solved

Sending Text (from Clipboard) to cmd.exe

Posted on 2010-08-18
10
725 Views
Last Modified: 2012-05-10
Hello,

I would like to sent some text to the cmd.exe that has the focus, via a keyboard hook.
I tried, with no result, after putting the text inside the clipboard:
keybd_event(VK_MENU,0,0,0);
keybd_event(VK_SPACE,0,0,0);
keybd_event(VK_SPACE,0,KEYEVENTF_KEYUP,0);
keybd_event(VK_MENU,0,KEYEVENTF_KEYUP,0);
keybd_event('B',0,0,0);
keybd_event('B',0,KEYEVENTF_KEYUP,0);
keybd_event('E',0,0,0);
keybd_event('E',0,KEYEVENTF_KEYUP,0);

The System-Menu will not open, it will just type ' be' inside the command promt.

I also tried:
CWnd cmW;
cmW.Attach(GetForegroundWindow());
cmW.SendMessageToDescendants(WM_PASTE);
cmW.Detach();

No results...

Please help.
Thank You
0
Comment
Question by:hlienert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33470620
how about using OpenClipboard() etc to get the text from the clipboard, then get the stdout of the console and push it there (something similar to http://dslweb.nwnexus.com/~ast/dload/guicon.htm) ?
0
 

Author Comment

by:hlienert
ID: 33475926
Hi!

this could work:
      int hConHandle;
      long lStdHandle;
      CONSOLE_SCREEN_BUFFER_INFO coninfo;
      FILE *fp;
      // allocate a console for this app
      AllocConsole();
      // set the screen buffer to be big enough to let us scroll text
      GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&coninfo);
      coninfo.dwSize.Y = MAX_CONSOLE_LINES;
      SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),coninfo.dwSize);
      // redirect unbuffered STDOUT to the console
      lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
      hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
      fp = _fdopen( hConHandle, "w" );
      *stdout = *fp;
      setvbuf( stdout, NULL, _IONBF, 0 );
fprintf(stdout,"hallo");

However, it starts a new console instead of sending the text to the existing cmd.exe window.
Is there a way to connect to the existing console?

Thanks

0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33476037
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:hlienert
ID: 33476779
Sorry, somehow not able to get it to work:
HWND hwW=GetForegroundWindow();
//this IS the cmd.exe Window!, code running from an Keyboard HOOK-Dll
DWORD pid=GetWindowThreadProcessId(hwW,NULL);
AttachConsole(pid) //It fails, code 87

:(
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33476896
as they rightly mention here

http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx

GetWindowThreadProcessId() returns threadID, but the process Id is a 2nd parameter ;)
0
 

Author Comment

by:hlienert
ID: 33477771
Well,
I did try that before, still with error, but error code 31 this time...
DWORD pid=0;
GetWindowThreadProcessId(hwW,&pid);
AttachConsole(pid) //It fails, code 31
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 33478262
well, this means that your process does not exist.

why do you think hwW window handle is the one of console?
0
 

Author Comment

by:hlienert
ID: 33479526
HWND hwW=GetForegroundWindow();
char Name[255];
GetWindowText(hwW,&Name[0],255);
CString cs_Name=Name;
cs_Name.MakeLower();
if (cs_Name.Right(7)!="cmd.exe") return;
...
DWORD pid=0;
GetWindowThreadProcessId(hwW,&pid);
AttachConsole(pid) //It fails, code 31
0
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
ID: 33480188
well, the title might be in Unicode and not in Ascii.

if I were you I'd try few things:

- use Spy++ and see what's Windows Class of the console, usually it's ConsoleWindowClass; this way you can use FindWindow():

      HWND hwW=FindWindow(TEXT("ConsoleWindowClass"),NULL);
      TCHAR Name[255];
      GetWindowText(hwW,Name,255);
      DWORD pid=0;
      GetWindowThreadProcessId(hwW,&pid);
      BOOL res = AttachConsole(pid);
      DWORD dwErr = GetLastError();

Above code finds my console, gets correct HWND and title, and pid - checked on Spy++ and Task Manager.

- use Toolhelp API (CreateToolhelp32Snapshot() and then Process32First()/Next()) to get a list of processes and get first "cmd.exe" - this gives you processId regardless of the window handles
0
 

Author Comment

by:hlienert
ID: 33494750
Thank you,
CreateToolhelp32Snapshot did the trick!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question