Write to Windows event log: Difference between revisions
Content added Content deleted
(Add Rust implementation) |
(Added Wren) |
||
Line 810: | Line 810: | ||
Call write_event("INFORMATION","This is a test information.") |
Call write_event("INFORMATION","This is a test information.") |
||
</lang> |
</lang> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
This embedded program is untested as I no longer have a working Windows machine but should work when run with administrative privileges - what can possibly go wrong? |
|||
<lang ecmascript>/* write_to_windows_event_log.wren */ |
|||
class Windows { |
|||
foreign static eventCreate(args) |
|||
} |
|||
var args = [ |
|||
"/T", "INFORMATION", "/ID", "123", "/L", "APPLICATION", |
|||
"/SO", "Wren", "/D", "\"Rosetta Code Example\"" |
|||
].join(" ") |
|||
Windows.eventCreate(args)</lang> |
|||
<br> |
|||
Now embed this script in the following C program, compile and run it. |
|||
<lang c>#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include "wren.h" |
|||
/* C <=> Wren interface functions */ |
|||
void C_eventCreate(WrenVM* vm) { |
|||
const char *args = wrenGetSlotString(vm, 1); |
|||
char command[strlen(args) + 13]; |
|||
strcpy(command, "EventCreate "); |
|||
strcat(command, args); |
|||
system(command); |
|||
} |
|||
WrenForeignMethodFn bindForeignMethod( |
|||
WrenVM* vm, |
|||
const char* module, |
|||
const char* className, |
|||
bool isStatic, |
|||
const char* signature) { |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "Windows") == 0) { |
|||
if (isStatic && strcmp(signature, "eventCreate(_)") == 0) return C_eventCreate; |
|||
} |
|||
} |
|||
return NULL; |
|||
} |
|||
static void writeFn(WrenVM* vm, const char* text) { |
|||
printf("%s", text); |
|||
} |
|||
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) { |
|||
switch (errorType) { |
|||
case WREN_ERROR_COMPILE: |
|||
printf("[%s line %d] [Error] %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_STACK_TRACE: |
|||
printf("[%s line %d] in %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_RUNTIME: |
|||
printf("[Runtime Error] %s\n", msg); |
|||
break; |
|||
} |
|||
} |
|||
char *readFile(const char *fileName) { |
|||
FILE *f = fopen(fileName, "r"); |
|||
fseek(f, 0, SEEK_END); |
|||
long fsize = ftell(f); |
|||
rewind(f); |
|||
char *script = malloc(fsize + 1); |
|||
fread(script, 1, fsize, f); |
|||
fclose(f); |
|||
script[fsize] = 0; |
|||
return script; |
|||
} |
|||
int main(int argc, char **argv) { |
|||
WrenConfiguration config; |
|||
wrenInitConfiguration(&config); |
|||
config.writeFn = &writeFn; |
|||
config.errorFn = &errorFn; |
|||
config.bindForeignMethodFn = &bindForeignMethod; |
|||
WrenVM* vm = wrenNewVM(&config); |
|||
const char* module = "main"; |
|||
const char* fileName = "write_to_windows_event_log.wren"; |
|||
char *script = readFile(fileName); |
|||
WrenInterpretResult result = wrenInterpret(vm, module, script); |
|||
switch (result) { |
|||
case WREN_RESULT_COMPILE_ERROR: |
|||
printf("Compile Error!\n"); |
|||
break; |
|||
case WREN_RESULT_RUNTIME_ERROR: |
|||
printf("Runtime Error!\n"); |
|||
break; |
|||
case WREN_RESULT_SUCCESS: |
|||
break; |
|||
} |
|||
wrenFreeVM(vm); |
|||
free(script); |
|||
return 0; |
|||
}</lang> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |