Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreateProcess unreachable #1

Open
geckoo1337 opened this issue Dec 31, 2024 · 8 comments
Open

CreateProcess unreachable #1

geckoo1337 opened this issue Dec 31, 2024 · 8 comments

Comments

@geckoo1337
Copy link

Hello. In this code, there is a problem with the CreateProcess function which does not accept a string as an argument. I am trying to convert the path in order to synchronize the StockFish.exe engine with the application, but I cannot. Have you an idea how I could fix this error? I am using VS2022. Thanks ++

@GabrieleDG0
Copy link
Owner

Hi, @geckoo1337 thanks for letting me know about the error!
There may be a few reasons why the CreateProcess function is not working as expected in your setup. Here are the possible reasons:

  1. The CreateProcess function requires the path to the executable to be a C-style string (LPSTR) and not a std::string. If there is a problem with passing the path to CreateProcess, this can lead to an error.

  2. If Stockfish.exe is not in the correct directory or there is a path problem, the function will fail. Make sure that the Stockfish.exe file is in the correct directory and that the path is correct.

  3. Depending on whether you are using MinGW, MSVC or another compiler, there may be differences in the way the compiler handles strings (e.g. std::string vs. C-style strings). This can lead to problems if the CreateProcess function expects a specific string format.

  4. Make sure your application has the necessary authorizations to run the Stockfish.exe process, especially on Windows. Sometimes it may be necessary to run VSCode or the terminal as administrator.

I recommend replacing the following code to the previous ConnectToEngine (engine.hpp file) function:

void ConnectToEngine(char* path)
{
    // Convert std::string path to LPSTR (C-style string)
    const char* path_cstr = path;

    // Initialize handles to NULL
    pipin_w = pipin_r = pipout_w = pipout_r = NULL;

    // Set security attributes for the pipes
    sats.nLength = sizeof(sats);
    sats.bInheritHandle = TRUE;
    sats.lpSecurityDescriptor = NULL;

    // Create pipes for input and output
    CreatePipe(&pipout_r, &pipout_w, &sats, 0);
    CreatePipe(&pipin_r, &pipin_w, &sats, 0);

    // Set up the STARTUPINFO structure to redirect standard input, output, and error
    sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    sti.wShowWindow = SW_HIDE; // Hide the window
    sti.hStdInput = pipin_r;   // Redirect standard input to pipin_r
    sti.hStdOutput = pipout_w; // Redirect standard output to pipout_w
    sti.hStdError = pipout_w;  // Redirect standard error to pipout_w

    // Create the process, with redirection of handles set up in STARTUPINFO
    if (!CreateProcess(NULL, const_cast<char*>(path_cstr), NULL, NULL, TRUE, 0, NULL, NULL, &sti, &pi))
    {
        // Handle error
        std::cerr << "Error creating process: " << GetLastError() << std::endl;
    }
} 

Please let me know if everything is working correctly in your environment! And then I will replace the old file in my repo!

@geckoo1337
Copy link
Author

geckoo1337 commented Jan 5, 2025

Hello. Thank you for your feedback. I tested your method, but unfortunately it does not work. I was always the same error and it seems to me that I cannot cast a pointer no? I guess that it is just a matter of C++ semantics - something different among compilers. Annoying 🦝

However I tried something and it works :

// Function to set up a connection to an external process (e.g., a chess engine)
void ConnectToEngine(TCHAR path[])
{   // Initialize handles to NULL
    pipin_w = pipin_r = pipout_w = pipout_r = NULL;
    // Set security attributes for the pipes
    sats.nLength = sizeof(sats);
    sats.bInheritHandle = TRUE;
    sats.lpSecurityDescriptor = NULL;
    // Create pipes for input and output
    CreatePipe(&pipout_r, &pipout_w, &sats, 0);
    CreatePipe(&pipin_r, &pipin_w, &sats, 0);
    // Set up the STARTUPINFO structure to redirect standard input, output, and error
    sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    sti.wShowWindow = SW_HIDE; // Hide the window
    sti.hStdInput = pipin_r;   // Redirect standard input to pipin_r
    sti.hStdOutput = pipout_w; // Redirect standard output to pipout_w
    sti.hStdError = pipout_w;  // Redirect standard error to pipout_w
    // Create the process, with redirection of handles set up in STARTUPINFO
    if (!CreateProcess(NULL, path, NULL, NULL, TRUE, 0, NULL, NULL, &sti, &pi))
    {
        std::cerr << "Error creating process: " << GetLastError() << std::endl;
    }
} 

TCHAR is the key compiling under Visual Studio 2022. Thank you Gabriele for your help. I wish you the best ++

@GabrieleDG0
Copy link
Owner

Hey there!

Thanks for the update, and I’m really glad it's working! Using TCHAR was a smart move — it keeps the code compatible with both ANSI and Unicode modes, which is especially handy since Visual Studio defaults to Unicode. This way you don’t have to worry about manually handling char* vs wchar_t*, and everything works smoothly across different setups. Nice work figuring that out!

By the way, what do you think of the project overall? Does it seem fun or useful? I’m curious — what do you plan to use it for? If you have any ideas or feedback, I’d love to hear about it! Then, I think, I will close this issue

@geckoo1337
Copy link
Author

geckoo1337 commented Jan 5, 2025

Hello Gabriele. This project is really cool. At first, I used it in order to understand how to integrate VCPKG in a project so as to use the SFML library. It seems to me that the code has been developed to be compiled using Code::Block or something similar. But now, according to my passion about Chess games - and C++, I would like to do a full application for everyone - a Chess game in which players can configure Skill level, moves, sounds and more. The code is clean - StockFish is powerful. So the door is open. I will post a new branch for this project on GitHub soon.

Question. Is there a way to check move validation using StockFish ? I guess that we can check moves according to their piece, developing a function which returns a boolean, but is there a better way ?

GabrieleDG0 added a commit that referenced this issue Jan 5, 2025
@GabrieleDG0
Copy link
Owner

Hii! Wow, I love your idea of turning this project into a complete chess game for everyone! That sounds great, especially with features like difficulty levels, sounds and move configuration. I’d love to see how it develops, and I’d love to actively contribute to the project, if you want me in, obv. Working together to create a polished end product would be a fantastic opportunity, especially given our shared passion for chess and C++.

Regarding your question about move checking with Stockfish: Yes, there is a way to use it, but it may not be the most efficient, and I'm not sure how to do it properly.

I think you send the move to Stockfish with the position command and check if it is processed without error:

position startpos moves e2e4

Use the go perft command to get all legal moves for a position and compare them.

go perft 1

Stockfish will return all legal moves and you can compare your move with this list.
However, if you want maximum efficiency, especially for real-time games, you should write your own validation I think!

@GabrieleDG0
Copy link
Owner

Just wanted to say, from the official GitHub page related to Stockfish, I found really good detailed info about the different commands of Stockfish. There, it is perfectly described what the UCI protocol is and how to give commands to the engine. Check this:

UCI COMMANDS

But then I found a few of the commands there, which my Stockfish executable does not recognize presumably because they belong to more modern implementations, which are not present in my file.

That said, most commands work, including the most important ones.

@geckoo1337
Copy link
Author

Hello. I had read these UCI explanations at first when I wondered how to develop a chess game.
I need to find a better way to implement a piece control and checker. Stay tuning ++

@GabrieleDG0
Copy link
Owner

I will try to implement something, if I get results I will inform you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants