Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sending Text (from Clipboard) to cmd.exe

Posted on 2010-08-18
10
Medium Priority
?
735 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

610 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