I am trying to communicate with a chess engine from inside my VB6 code.
I can't simply call the engine with Shell, because it doesn't accept one-line commands. So I decided to run the engine within my code, but then get control over the console and issue the sequential commands into it.
The problem now is that, based on the Microsoft article written here, I could succeffully write a VB sample code that opens the console, I can run my engine inside the console, but then using the WriteConsole when I want to issue the next command, it actually only *writes* then next command, but not *run* it.
If in the opened console I manually type "uci" (the next command that I need to run) and press enter it works, but when I write for example:
This code causes the code to write "uci" on the console at the right place, but doesn't run it!
I tried a lot to simulate a "enter key stroke" both in VB and DOS (e.g. VbNewline, Chr(13), 0d0a, vbCrlf, ...), but no one works.
I know that the WriteConsole is actually meant to just write the characters, but is there any alternative command to *run* the written command, or better, to just run a command in the console?
Programs that run in the Console Subsystem have two basic methods of user interaction.
One is to use the Standard I/O streams. Programs that do this can be used with pipe and redirection modifiers when run from cmd.exe, and can be started with the streams redirected to anonymous pipes by a parent program.
The other way is to use Console Device I/O (such as WriteConsole for output to the screen). These are much tougher to "automate" and it requires the use of console hook APIs that are normally only used by cmd.exe itself or an alternate command shell. That MS KB article does not apply to what you are trying to do.
If you have the first scenario there are some options. But if the chess program is doing Console Device I/O it will be impractical. Hackish techniques like sendkeys and window grabbing are limited in what they can accomplish.
So based on what you said, I should only consider the first of the two methods, "programs that can be used with pipe and redirection modifiers".
Unfortunately in my case the engine it kind of a standalone engine, which doesn't accept parameters; you should just run it, then do whatever you want inside it (still is the same console window though).
What about having a direct communication with a process via VB? Because you know, there are some Chess GUIs that you can install this engine on, then they will easily communicate with it, they send a message to it and it replies an answer. I have no idea how are they written, but still, they are communicating with the engine and it means the engine has the ability to respond in some way.
Do you have any idea how to get something similar done? Or any other way that I can run the engine, then detect that window, and force it to issue another command in it?!
Perhaps they are using anonymous pipe redirection then. That's the first case I was talking about, which connects to the child program's input and output streams. There is no window involved and for that matter not even a Console since it isn't required.
Aside from that I have no idea what "direct communication with a process" would mean.
In any case unless this chess program uses pretty well structured messaging you'll have a lot of work to do sending commands and parsing "results" too.
The example attached here has two programs: DirClient and DirServer (which sort of stands in for your chess engine here). See the readme file. Compile both programs, then run the DirClient.exe which will shell and control the DirServer.exe.
DirServer is a very simple more or less "dummy" program. The source code comments document the messaging protocol used via pipes.
It is possible to open a command prompt and run DirServer from there and interact with it manually if you enter commands in the proper format.
That's the exact thing I was looking for, as the author of the engine also replied me and said you need to use piping.
Anyway, I downloaded the files and was trying to run it based on the ReadMe explanations, there is one error I don't understand. Since I am not familiar how are they working and I am only trying to learn, I would really appreciate if you can help me to get it running.
The first error I face is when I try to run the DriServer, I get the following error:
Run-time error '-2147024890 (80070006)'
Method 'ReadLine' of object 'ITextStream' failed.
and the line issuing the error is:
strMsg = tsStdIn.ReadLine()
I searched for the error, some people facing this error are mentioning that the target file (the pipe?!) is larger than one VB can handle it; it might be totally something else though!
Sorry, I think I found the way to run it. I created exe of both DirServer and DirClient and then I should only run the DirClient.exe, now once I press the "Make Request" it runs the server and the result is coming from the server, is it?
Does it mean that now my VB chess program that works as a GUI should be the client, and the chess engine works as the server is it?
Now I should try to send my request with my program (as the client) and the engine should process my request (as the server)?
Dear dilettante,
Thank you so much for your help, it solved my problem. I could successfully establish the communication, get the results back and close the pipe.
I will refer back to you if I faced any problem in the future process.
By the way, kindly please send me your personal email if you can, I have to discuss something with you about the code.