c++ - 阅读控制台缓冲区/输出 C++

标签 c++ windows winapi game-development

我的问题很简单,但我似乎绝对找不到解决方案。

我有一个专用的游戏服务器 (JEDI ACADEMY JAMPDED),它是一个控制台应用程序。它不断地写一些信息,我想以某种方式处理数据。如果我可以用外部读取它的输出,那就很容易了。

问题:它不写入标准输出,因此不能用批处理文件进行管道传输,popen 也不起作用。

所以我想用 WINAPI 来做。我能够创建流程,但仍然无法读取输出。

我试过这些:

这是 jampded.exe: console

我从 friend 那里得到了一个视觉基础代码,他从 Ingame 读取 ConsoleInput,所以我很确定,可以读取控制台:

片段:

Global hWnd = FindWindow_(#Null,"Jedi Knight Academy MP Console")              ;console window
Global hWnd2 = FindWindow_(#Null,"Jedi Knight®: Jedi Academy (MP)")            ;actual game window
Global inputhWnd = FindWindowEx_(hwnd,0,"edit",0)                                         ;the one to send stuff to
Global consolehWnd = FindWindowEx_(hwnd,inputhWnd,"edit",0)                      ;the one to read the console from


Procedure checkConsole()
    Protected wholetext.s, oldtext.s,text.s, checkname.s
    Repeat   
        wholetext = getText()
        If wholetext
            text = StringField(wholetext,CountString(wholetext,#CRLF$),#CRLF$)
            If oldtext <> text
                oldtext = text
                analyseConsole(@text)
            EndIf
        EndIf
        Delay(20)
        writePreferences()
    Until quit
EndProcedure

Procedure.s getText()
    Protected wholetext.s
    If hWnd And hWnd2
        If Not inputhWnd Or Not consolehWnd
            inputhWnd = FindWindowEx_(hWnd,0,"edit",0)
            consolehWnd =  FindWindowEx_(hWnd,inputhWnd,"edit",0)
        EndIf
        length = SendMessage_(consolehWnd, #WM_GETTEXTLENGTH, 0, 0)
        wholetext = Space(length)
        SendMessage_(consolehWnd,#WM_GETTEXT,length + SizeOf(Character),@wholetext)
        ProcedureReturn wholetext
    Else 
    If FindWindow_(#Null,"Jedi Knight Academy MP Console")
        hWnd = FindWindow_(#Null,"Jedi Knight Academy MP Console")
        hWnd2 = FindWindow_(#Null,"Jedi Knight®: Jedi Academy (MP)")
        inputhWnd = FindWindowEx_(hwnd,0,"edit",0)
        consolehWnd = FindWindowEx_(hwnd,inputhWnd,"edit",0)
    EndIf
    ProcedureReturn ""
    EndIf
    If @wholetext > 0
        FreeMemory(@wholetext)
    EndIf
EndProcedure

也许这对我和其他人也有帮助 :)

最佳答案

Hans Passant 是对的。它不写入标准输出,但我可以用程序集修复它。所以他在这方面是不对的。

此后输出未同步,但可以通过交换跳转到 nop 来成功修复。更多:stackoverflow.com/questions/23811572/writefile-function-with-assembly-debugging-syncing

总结一下:

它不仅适用于 Jedi Academy 专用服务器,它对任何存储输出的二进制文件都有用,但不适用于 STDOUT。

将 WriteFile 函数更改为写入 STDOUT(-11 是标准输出):

enter image description here

修复同步问题:

enter image description here

它是针对JampDed 1.00的,所以如果你想将它用于其他版本的JK,或者其他游戏,你必须自己找到这些地址。最佳实践(在我看来)如果你在代码中更深入地调用。您可以从找到的任何消息开始,然后再深入。

我用这种方式记录了整个调试过程:

004429a7 is (call 0043d440)
- 0043d44e (call 00422720)
-- 004227dD (call edx) - 2008F1B0
--- 2008f103 (call 2008cad0)
---- 2008c4d0
----- 2008c569 (call 2008b030)
------ 200a25f7 DWORD PTR DS:[20106A28] - 00422540
------- 0042254b (CALL DWORD PTR DS:[ECX+4]) - 0043d590
-------- 0043d8aa (call 0040fb40)
--------- 0040FC39 (call 0044b7a0)
---------- 0044b66c (call 004964dd)
----------- 00496500 (call 0049a4ba)
------------ 0049a4cd (call 00497d66)
------------- 00497d92 (call 0049e18c)
-------------- 0049e26f (CALL DWORD PTR DS:[4A715C])

如果您注意到最后一个是 WriteFile 所在的位置。在那之前的 2 是 logsync 东西所在的位置

现在

jampDed.exe | stdout.exe

有效,您可以开始轻松地为服务器创建自己的模组。或者你想要的任何东西。 :)

关于c++ - 阅读控制台缓冲区/输出 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19298861/

相关文章:

c++ - 在屏幕上绘制图像后背景发生变化

c++:类外的模板变量

c++ - 如何在 OpenGL 中缩放模型?

c++ - 如何在 C++ 图形中打印窗口控制台字体

windows - 如何在 CMake (Windows) 中检索用户环境变量

winXP 上的 python 版本

c - execv 是怎么做到的?

c++ - ReadFile 函数 lpbuffer 和字节读取说明

c - 以编程方式或使用 list (MinGW) 将 "Override high DPI scaling"设置为 "System (Enhanced)"

c++ - 使用 gcc 4.9 在 snow leopard 的 qt creator 中设置 c++11