0

Introduction: I'm an administrator currently working on the dedicated servers of the game "Sniper Elite V2". SEV2's dedicated servers are running under windows in a console application. We are able to run this application under Linux too, with wineHQ. Everything is good, excepted that the console is launched in a new window, so no way to have the console inputs/outputs (link: http://img802.imageshack.us/img802/650/sev22.png). Moreover, we can read (with an HEX editor) that "This program cannot be run in DOS mode". My objective is to bring back DOS compatibility.

My idea is to create another console application written in C++ (with CodeBlocks). This "Hook" should retrieve the content (text) of the dedicated server "window" and will display it, with the possibility of course to input commands to the dedicated server console. Like that, we will be able to work with inputs/outputs both on Windows and Linux. I took the time to draw a little schema : http://img195.imageshack.us/img195/3017/29585679.png

My question is: How to do this ? Which functions should I use ?

At this point, I'm stuck. I've tried the following approaches :

FUNC "AttachConsole" has failed.

The following pseudo-code has failed too:

mainWindow = FindWindow( TEXT("ConsoleWindowClass"), NULL) );

console = FindWindowEx( mainWindow, NULL, TEXT("ConsoleWindowClass"), NULL );

SendMessage(
  console,
  WM_GETTEXT,
  sizeof(buffer) / sizeof(TCHAR),
  (LPARAM)buffer);

I'm using a tool called "WinSpy++" to help me, but the tool is not able itself to retrieve/input data to the Sniper Elite V2 Dedicated Server.

Any ideas/examples/reflections is welcome :-)

  • `AttachConsole` is right way. Why it "fails"? – Maximus Jul 31 '12 at 20:38
  • But, can't understand the idea. Did you want to read/write from **two** consoles simultaneously? And not sure created process tree. And you said Wine. What works in Win, may not work in WineHQ – Maximus Jul 31 '12 at 20:43
  • Well, the function `AttachConsole` return zero. `GetLastError` returns nothing. – user1566601 Jul 31 '12 at 21:13
  • @Maximus, I would like to read from dedicated server console, and output data in a second console application. This second application should also be able to send commands to the dedicated server console. Would you like a schema/GRAFCET ? Currently, forget the WineHQ side, just think windows. – user1566601 Jul 31 '12 at 21:15

1 Answers1

1

"This program cannot be run in DOS mode"

You make a mistake. Don't mix terms "DOS mode" and "Console". Windows console is NOT a DOS mode. Console is native terminal, where you can run any windows application 32/64 bit, written for console subsystem. "DOS mode" is pre Windows operating system, e.g. DOS, TR-DOS, FreeDOS and so on.

As for question, one console app can not work with two consoles simultaneously.

Upd

Seems that server was builded for GUI subsystem and creates console with AllocConsole.

  1. Run server with 'CreateProcess'
  2. Wait a little while new console wIndow appears.
  3. Call 'FreeConsole' and 'AttachConsole(ServerPID)'

Old

So, if you want to read console_1 from process belonging to console_2, you must

  1. start third process, for example with DETACHED_PROCESS
  2. call FreeConsole (required) and AttachConsole(RootPidFromConsole_1)
  3. communicate between third process and console_2's process (pipes, shared memory, ...)

this works in windows.

Maximus
  • 10,751
  • 8
  • 47
  • 65
  • Thank you for your answer, I will try it tomorrow. To recap, in my hook: 1) Createprocess (with de dedicated server console as a child process, with DETACHED_PROCESS attribute), 2) call freeconsole fallowed by AttachConsole(pidofmychildprocess), 3) communication stuff. Is it ok ? – user1566601 Jul 31 '12 at 22:33
  • Hm? I think that you want to read from "dedicated server", no? If you can start process in the console, you want to read from (call CreateProcess from pprocess in that console) - omit `DETACHED_PROCESS` flag and step 2. – Maximus Aug 01 '12 at 06:12
  • Yeah it is a bit complicated... In my program (*console_primary*), when I use `CreateProcess` to launch the dedicated server console, this one is launched in a separate console (*console_child*). I would like that *console_primary* read the data of *console_child* console and output the retrieved data in its console (*console_primary*). I would like also that when I write a command in *console_primary*, this one is launched to *console_child*. In fact, I would like a redirection of the output of *console_child* and the ability to input from *console_primary* to *console_child*. – user1566601 Aug 01 '12 at 08:41
  • As I notified, the dedicated server console (*console_child*) "cannot be run in DOS mode". To avoid the problem, my program *console_primary* should be able to run in DOS mode ; and will allow communication with the dedicated server console (console_child). – user1566601 Aug 01 '12 at 08:45
  • Thank you for correcting me :). I would like to start the dedicated server in the same console but it is impossible, since the dedicated server opens a new console (see: http://img821.imageshack.us/img821/8810/maino.png). Download Image Associated Source Code: http://www.mediafire.com/?s1ehmc3d22phgpf – user1566601 Aug 01 '12 at 11:40
  • Thank you :). I've tried your new solution: failed. But one interesting point : after `FreeConsole` and `AttachConsole(pi.dwProcessId)`, I've added `cout << "Some Text" << endl;` and "Some Text" was placed in the dedicated server's console but without effect (I think that I have to simulate key). – user1566601 Aug 01 '12 at 12:43
  • I've also added `WaitForSingleObject( pi.hProcess, INFINITE );` at the end but my application still closing... Updated Source Code: http://www.mediafire.com/?1vf5idoovu5dkm0 – user1566601 Aug 01 '12 at 12:51
  • Update: It seems that "Some Text" is not captured by dedicated server console (I've simulated with `PostMessage(sev2, WM_KEYDOWN, VK_RETURN, 0);` where `sev2` is `FindWindow( TEXT("ConsoleWindowClass"), NULL )`. Dedicated server's console wait for user input via usual way => keyboard ! – user1566601 Aug 01 '12 at 13:01
  • Solution can not be "failed". Cause of «"Some Text" was placed in the dedicated server's console ». So, you process can communicate with new console. Your comments are obscure for me. Can suppose, that you are using console incorrect way. What the "PostMessage"? Thought, you may study console function. For example WriteConsoleInput. Another thought, this will be out of original question, imho. Third thought (Q is very obscured :)) that pi.hProcess really ends, and new console is created by third process. Can't look into your code for deep checks. – Maximus Aug 01 '12 at 13:19
  • Yes, now we know that the two processes can communicate with each other. I will study console functions and come back later if I still stuck. Thank you for your help (and yes, my question is very obscured...) :) – user1566601 Aug 01 '12 at 15:15