/
win32_exec.h
42 lines (32 loc) · 1.03 KB
/
win32_exec.h
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
#pragma once
#include <windows.h>
#include <shellapi.h>
#include "log.h"
#include "str.h"
#pragma warning( push )
#pragma warning( disable : 4302 )
#pragma warning( disable : 4311 )
enum RunAsAdmin {
RunAsAdmin_No,
RunAsAdmin_Yes,
};
inline void exec_and_quit( const char * path, RunAsAdmin run_as_admin, const char * command_line = NULL ) {
wchar_t * wide_path = UTF8ToWide( path );
wchar_t * wide_command_line = command_line == NULL ? NULL : UTF8ToWide( command_line );
defer { free( wide_path ); };
defer { free( wide_command_line ); };
int ok = int( ShellExecuteW( NULL, run_as_admin == RunAsAdmin_Yes ? L"runas" : NULL, wide_path, wide_command_line, NULL, SW_SHOWDEFAULT ) );
if( ok == SE_ERR_ACCESSDENIED )
return;
if( ok <= 32 )
FATAL( "ShellExecute" );
exit( 0 );
}
inline void run_game( const char * path ) {
exec_and_quit( path, RunAsAdmin_No );
}
inline bool open_in_browser( const char * url ) {
int ok = int( ShellExecuteA( NULL, "open", url, NULL, NULL, SW_SHOWDEFAULT ) );
return ok > 32;
}
#pragma warning( pop )