Create a gist now

Instantly share code, notes, and snippets.

#include <Windows.h>
#include <stdio.h>
#define dllx extern "C" __declspec(dllexport)
dllx double delay(double ms) {
Sleep((DWORD)ms);
return 1;
}
static COLORREF get_colors[16];
dllx double get_color_win(double c) { //
CHOOSECOLOR cc;
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = GetActiveWindow();
cc.lpCustColors = (LPDWORD)get_colors;
cc.rgbResult = (int)c;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc) == TRUE) {
return cc.rgbResult;
} else return c;
}
dllx double file_copy_win(char* source, char* dest) {
return CopyFile(source, dest, false) != 0;
}
dllx double window_set_caption_ext(double w, char* s) { //
int ptr = w;
SetWindowText((HWND)ptr, s);
return ptr;
}
#define SHELL_SHOWCMD SW_SHOWNORMAL
// for completely mysterious reasons, this particular function
// always returns FILE_NOT_FOUND, even with "open" action hardcoded.
dllx double shell_open(char* file) {
return (int)ShellExecute(NULL, NULL, file, NULL, NULL, SHELL_SHOWCMD);
}
dllx double shell_do(char* action, char* file) {
return (int)ShellExecute(NULL, action, file, NULL, NULL, SHELL_SHOWCMD);
}
dllx double shell_do_at(char* action, char* file, char* directory) {
return (int)ShellExecute(NULL, action, file, NULL, directory, SHELL_SHOWCMD);
}
dllx double shell_do_ext(char* action, char* file, char* params, char* directory) {
return (int)ShellExecute(NULL, action, file, params, directory, SHELL_SHOWCMD);
}
dllx double shell_execute(char* file, char* params) {
return (int)ShellExecute(NULL, NULL, file, params, NULL, SHELL_SHOWCMD);
}
dllx double shell_execute_at(char* file, char* params, char* directory) {
return (int)ShellExecute(NULL, NULL, file, params, directory, SHELL_SHOWCMD);
}
dllx char* file_text_get(char* file) {
FILE *f;
char *r = NULL;
long fl;
fopen_s(&f, file, "r");
if (f) {
fseek(f, 0L, SEEK_END);
fl = ftell(f);
rewind(f);
if (r = (char*)calloc(1, fl + 1)) {
// if a file-sized string was allocated successfully.
if (!fread(r, fl, 1, f)) {
// if failed to read the contents, dealloc the string.
free(r);
r = NULL;
}
}
fclose(f);
}
if (r == NULL) {
// errors yield empty strings
r = (char*)calloc(1, 1);
r[0] = 0;
}
return r;
}
dllx double file_text_put(char* file, char* text) {
FILE *f;
fopen_s(&f, file, "w");
int r = 0;
if (f) {
if (fputs(text, f)) {
r = 1;
}
fclose(f);
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment