Solved

Sending Text (from Clipboard) to cmd.exe

Posted on 2010-08-18
10
704 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now