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 · 1 comment
Open

CreateProcess unreachable #1

geckoo1337 opened this issue Dec 31, 2024 · 1 comment

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!

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