Skip to content

Commit c75dfe1

Browse files
committed
Add Windows error handler.
1 parent 29cf3f2 commit c75dfe1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: portability.c

+31
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ ctr_object* ctr_program_waitforpassword(ctr_object* myself, ctr_argument* argume
2626
#endif
2727

2828

29+
#ifdef WINDOWS_ERROR_SYSTEM
30+
ctr_object* ctr_error( char* message, uint16_t error_code ) {
31+
int MAX_LEN_ERROR_STR = 800;
32+
int MAX_LEN_SYSTEM_STR = 400;
33+
int ok_msg;
34+
char* errstr;
35+
char* error_message;
36+
errstr = ctr_heap_allocate( sizeof(char) * MAX_LEN_ERROR_STR );
37+
error_message = ctr_heap_allocate( sizeof(char) * MAX_LEN_SYSTEM_STR );
38+
ok_msg = FormatMessage(
39+
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
40+
NULL,
41+
error_code,
42+
0,
43+
error_message,
44+
MAX_LEN_SYSTEM_STR,
45+
NULL
46+
);
47+
if (!ok_msg) {
48+
snprintf(error_message, MAX_LEN_SYSTEM_STR, "(#%d) (#%d)", error_code, (uint16_t) GetLastError());
49+
}
50+
snprintf( errstr, MAX_LEN_ERROR_STR, message, error_message );
51+
CtrStdFlow = ctr_build_string_from_cstring( errstr );
52+
ctr_heap_free( errstr );
53+
ctr_heap_free( error_message );
54+
CtrStdFlow->info.sticky = 1;
55+
errstack = 0;
56+
return CtrStdFlow;
57+
}
58+
#endif
59+
2960
#ifdef WINDOWS_PLUGIN_SYSTEM
3061
typedef int (__cdecl *MYPROC)();
3162
void* ctr_internal_plugin_find(ctr_object* key) {

0 commit comments

Comments
 (0)