Solved

Sending Text (from Clipboard) to cmd.exe

Posted on 2010-08-18
10
708 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now