Solved

Sending Text (from Clipboard) to cmd.exe

Posted on 2010-08-18
10
711 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

813 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

9 Experts available now in Live!

Get 1:1 Help Now