Close program if another application is closed.

Pages: 12
> This is the actual MD5 code of the AntiCheat.exe converted to Hex, right ?
> So in this case the MD5 string is "75a82fafa3b892a349f80ce45c7e7a15"

1
2
3
// initialise with the actual md5 digest for anti_cheat.exe
const md5_digest expected_anti_cheat_md5 = { 0x75, 0xa8, 0x2f, 0xaf, 0xa3, 0xb8, 0x92, 0xa3,
                                             0x49, 0xf8, 0x0c, 0xe4, 0x5c, 0x7e, 0x7a, 0x15 } ;


> error: invalid conversion from 'const unsigned char*' to 'PBYTE {aka unsigned char*}' [-fpermissive]

This appears to be an error in the 32-bit and/or pre-Win8 api.

Fix: drop the const in line 23
1
2
// const auto bytes = get_bytes(path) ; // line 23
auto bytes = get_bytes(path) ;
The program is compiling now, but when game.exe and anticheat.exe are started, only AntiCheat.exe is visible. Game.exe is running in the task manager, but it isn't showing.

The code now looks like that:

(string and vector are included in the main.h file)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "main.h"
#include <iostream>
#include <array>
#include <fstream>
#include <iterator>
#include <windows.h>
#include <wincrypt.h>
void init_memory();
void main_loop();
#undef main

bool exists_and_is_of_size( std::string path, unsigned long size )
{
    const auto file = ::CreateFileA( path.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, 0 ) ;
    if( file == INVALID_HANDLE_VALUE ) return false ;

    const auto sz = ::GetFileSize( file, nullptr ) ;
    ::CloseHandle(file) ;
    return sz == size ;
}

HANDLE create_process( std::string path )
{
    ::STARTUPINFO si {} ;
    ::PROCESS_INFORMATION pi {} ;
    std::vector<char> vec( path.begin(), path.end() ) ;
    vec.resize( path.size() + 100 ) ;

    if( ::CreateProcessA( nullptr, vec.data(), nullptr, nullptr, false, 0, nullptr, nullptr,
                          std::addressof(si), std::addressof(pi) ) )
        ::CloseHandle( pi.hThread ) ;

    return pi.hProcess ;
}

constexpr std::size_t MD5_SIZE = 16 ;
using md5_digest = std::array< BYTE, MD5_SIZE > ;

std::vector<BYTE> get_bytes( std::string path )
{
    std::ifstream file( path, std::ios_base::binary ) ;
    return { std::istream_iterator<BYTE>{file}, std::istream_iterator<BYTE>{} } ;
}

md5_digest md5( std::string path ) // MinGW: link with libadvapi32.a
{
    md5_digest digest {} ;

    //const auto bytes = get_bytes(path) ;
    auto bytes = get_bytes(path) ;
    if( !bytes.empty() )
    {
        HCRYPTPROV provider = 0 ;
        if( ::CryptAcquireContext( std::addressof(provider), nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) )
        {
            HCRYPTHASH hash = 0 ;
            if( ::CryptCreateHash( provider, CALG_MD5, 0, 0, std::addressof(hash) ) )
            {
                ::CryptHashData( hash, bytes.data(), bytes.size(), 0 ) ;
                DWORD nbytes = bytes.size() ;
                ::CryptGetHashParam( hash, HP_HASHVAL, digest.data(), std::addressof(nbytes), 0 ) ;
                ::CryptDestroyHash(hash) ;
            }
            ::CryptReleaseContext( provider, 0 ) ;
        }
    }

    return digest ;
}

int main(int argc, char *argv[])
{
    const std::string anti_cheat_path = "AntiCheat.exe" ;
    // const std::uintmax_t anti_cheat_size = 40 ; //file is 40kb
    const std::uintmax_t anti_cheat_size = 40960 ; // 40,960 bytes

    // initialise with the actual md5 digest for anti_cheat.exe
    const md5_digest expected_anti_cheat_md5 = { 0x75, 0xa8, 0x2f, 0xaf, 0xa3, 0xb8, 0x92, 0xa3,
                                             0x49, 0xf8, 0x0c, 0xe4, 0x5c, 0x7e, 0x7a, 0x15 } ;
    // if anti_cheat is in the current directory and has the expected size
    if( exists_and_is_of_size( anti_cheat_path, anti_cheat_size ) )
    {
        const auto anti_cheat = create_process( anti_cheat_path ) ;
        if( !anti_cheat ) return 1 ; // return if the anti_cheat process could not be launched
        if( md5(anti_cheat_path) == expected_anti_cheat_md5 ) // if the md5 digest matches
        {
        if (!init_sdl()) return 0;
        init_memory();
        init_audio();
        load_splash_screen();
        load_login();
        read_host_info();
        read_config_info();
        load_ignore();
        load_sos();
        //connect_to_hub();
        init_sockets();
        main_loop();
        return 0;
        }
        // main_loop() should execute this while loop, once every 'check_interval_millisecs' milliseconds
        while( WaitForSingleObject( anti_cheat, 0 ) == WAIT_TIMEOUT ) // if anti_cheat is still running
        {
            const unsigned int check_interval_millisecs = 5000 ; // check every five seconds
            // run game loop for check_interval_millisecs milliseconds
            // go back to while and check if anti_cheat is still running
        }
    }

    else return 1 ; // anti cheat not found
}


EDIT:

I noticed now that in the code you gave me in post:
http://www.cplusplus.com/forum/general/188138/#msg914515

You have removed:

1
2
        main_loop();
        return 0;


So without these, anticheat.exe starts fine but game doesn't work. The game.exe starts and then crashes "game.exe stopped working".

When I added them back, it works fine, but when I close AntiCheat.exe, the Game.exe is not closing.

I have tried to include the main_loop() inside the while like this:

1
2
3
4
5
6
7
8
        while( WaitForSingleObject( anti_cheat, 0 ) == WAIT_TIMEOUT ) // if anti_cheat is still running
        {
            const unsigned int check_interval_millisecs = 5000 ; // check every five seconds
            main_loop();
            return 0;
            // run game loop for check_interval_millisecs milliseconds
            // go back to while and check if anti_cheat is still running
        }


Again, the game.exe and anticheat.exe starts. The game client is working fine and connects to the server, but when i close anticheat.exe, game.exe is not closing.

Here's the main_loop function if needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void main_loop()
{
	int do_exit = 0;

	while(!do_exit)
	{
		handle_sockets();
		do_exit += handle_sdl_events();
		space_threadless();

#ifdef WIN32
		Sleep(1);
#else
		usleep(1000);
#endif
	}
}
Last edited on
> Again, the game.exe and anticheat.exe starts. The game client is working fine and connects to the server,
> but when i close anticheat.exe, game.exe is not closing.

Repeat: check if anti_cheat is still running in the game loop.

Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// ...

bool initialise()
{
        if (!init_sdl()) return false ;
        init_memory();
        init_audio();
        load_splash_screen();
        load_login();
        read_host_info();
        read_config_info();
        load_ignore();
        load_sos();
        //connect_to_hub();
        init_sockets();

        return true ;
}

void main_loop( HANDLE anti_cheat )
{
	int do_exit = 0;

        const unsigned int interval_millisecs = 1000 ; // run the loop once every second

	while( !do_exit && WaitForSingleObject( anti_cheat, interval_millisecs ) == WAIT_TIMEOUT )
	{
		handle_sockets();
		do_exit += handle_sdl_events();
		space_threadless();
	}
}

int main(int argc, char *argv[])
{
    const std::string anti_cheat_path = "AntiCheat.exe" ;
    const std::uintmax_t anti_cheat_size = 40960 ; // 40,960 bytes

    // initialise with the actual md5 digest for anti_cheat.exe
    const md5_digest expected_anti_cheat_md5 = { 0x75, 0xa8, 0x2f, 0xaf, 0xa3, 0xb8, 0x92, 0xa3,
                                             0x49, 0xf8, 0x0c, 0xe4, 0x5c, 0x7e, 0x7a, 0x15 } ;

    // if anti_cheat is in the current directory and has the expected size
    if( exists_and_is_of_size( anti_cheat_path, anti_cheat_size ) )
    {
        const auto anti_cheat = create_process( anti_cheat_path ) ;
        if( !anti_cheat ) return 1 ; // return if the anti_cheat process could not be launched
        
        if( md5(anti_cheat_path) == expected_anti_cheat_md5 ) // if the md5 digest matches
        {
            if( !initialise() ) return 1 ;
            main_loop(anti_cheat) ;
        }
    }

    else return 1 ; // anti cheat not found
}

Hi @JLBorges, this still doesn't work. Now AntiCheat.exe is starting, but the actual Game.exe is never started. The problem is in the MD5 check. If I remove:

const md5_digest expected_anti_cheat_md5 = { 0x75, 0xa8, 0x2f, 0xaf, 0xa3, 0xb8, 0x92, 0xa3, 0x49, 0xf8, 0x0c, 0xe4, 0x5c, 0x7e, 0x7a, 0x15 } ;

and:

1
2
3
if( md5(anti_cheat_path) == expected_anti_cheat_md5 ) // if the md5 digest matches
{
}


The main function become:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main(int argc, char *argv[])
{
    const std::string anti_cheat_path = "AntiCheat.exe" ;
    const std::uintmax_t anti_cheat_size = 40960 ; // 40,960 bytes

    // initialise with the actual md5 digest for anti_cheat.exe
    //const md5_digest expected_anti_cheat_md5 = { 0x75, 0xa8, 0x2f, 0xaf, 0xa3, 0xb8, 0x92, 0xa3,
    //                                         0x49, 0xf8, 0x0c, 0xe4, 0x5c, 0x7e, 0x7a, 0x15 } ;

    // if anti_cheat is in the current directory and has the expected size
    if( exists_and_is_of_size( anti_cheat_path, anti_cheat_size ) )
    {
        const auto anti_cheat = create_process( anti_cheat_path ) ;
        if( !anti_cheat ) return 1 ; // return if the anti_cheat process could not be launched

        //if( md5(anti_cheat_path) == expected_anti_cheat_md5 ) // if the md5 digest matches
        //{
            if( !initialise() ) return 1 ;
            main_loop(anti_cheat) ;
        //}
    }

    else return 1 ; // anti cheat not found
}


Which is working just fine.

EDIT:

Just noticed that Game.exe runs extremely lagging. It's like when I press a button it waits 2,3 seconds before taking action.
Last edited on
> The problem is in the MD5 check. If I remove ...

What does this print out?

1
2
3
4
5
6
7
8
// ...

int main()
{
    const std::string anti_cheat_path = "AntiCheat.exe" ;
    for( int byte : md5(anti_cheat_path) ) std::cout << std::hex << byte << ' ' ;
    std::cout << '\n' ; 
}



> Just noticed that Game.exe runs extremely lagging.
> It's like when I press a button it waits 2,3 seconds before taking action.

Reduce the wait interval. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void main_loop( HANDLE anti_cheat )
{
	int do_exit = 0;

        // const unsigned int interval_millisecs = 1000 ; // run the loop once every second
        const unsigned int interval_millisecs = 400 ; // run the loop once every 400 milliseconds

	while( !do_exit && WaitForSingleObject( anti_cheat, interval_millisecs ) == WAIT_TIMEOUT )
	{
		handle_sockets();
		do_exit += handle_sdl_events();
		space_threadless();
	}
}
Topic archived. No new replies allowed.
Pages: 12