#ifdef WIN32
#	include "winreg.h"
#	include "lprintf.h"
#	include "lib.h"

	HKEY winreg_newkey(HKEY addto, char *key) {
		HKEY newKey = NULL;
		DWORD action;

		if (addto == NULL) return NULL;

		if (RegCreateKeyEx(addto,
			key,
			0,
			"REG_SZ",
			REG_OPTION_NON_VOLATILE,
			KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
			NULL,
			&newKey,
			&action) != ERROR_SUCCESS) return NULL;

		return newKey;
	}
	int winreg_delkey(HKEY from, char *key) {
		return RegDeleteKey(from,key) == ERROR_SUCCESS;
	}
	int winreg_addval(HKEY addto, char *name, char *data) {
		if (addto == NULL) return FALSE;
		if (RegSetValueEx(addto,name,0,REG_SZ,data,lib_strlen(data)) != ERROR_SUCCESS) return FALSE;
		return TRUE;
	}
	void winreg_closekey(HKEY key) {
		 if (key != NULL) RegCloseKey(key);
	}
	char *winreg_error(char *bf, int bfsz) {
		int n;

		if (!bf || bfsz <= 0) return NULL;

		bf[0] = '\0';
		n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)bf,bfsz,NULL);
		if (n == 0) lprintf(bf,bfsz,"Failed to get system error message");
		else for(n--; iseol(bf[n]); n--) bf[n] = '\0';

		return bf;
	}
#endif
