win32dep.c

来自「Linux下的多协议即时通讯程序源代码」· C语言 代码 · 共 597 行 · 第 1/2 页

C
597
字号
				cp_valname = g_locale_from_utf8(valname, -1,					NULL, NULL, NULL);			if (value) {				char *cp_value = g_locale_from_utf8(value, -1,					NULL, NULL, NULL);				int len = strlen(cp_value) + 1;				if(RegSetValueExA(reg_key, cp_valname, 0, REG_SZ,						cp_value, len						) == ERROR_SUCCESS)					success = TRUE;				g_free(cp_value);			} else				if(RegDeleteValueA(reg_key, cp_valname) ==  ERROR_SUCCESS)					success = TRUE;			g_free(cp_valname);		}		g_free(cp_subkey);	}	if(reg_key != NULL)		RegCloseKey(reg_key);	return success;}static HKEY _reg_open_key(HKEY rootkey, const char *subkey, REGSAM access) {	HKEY reg_key = NULL;	LONG rv;	if(G_WIN32_HAVE_WIDECHAR_API()) {		wchar_t *wc_subkey = g_utf8_to_utf16(subkey, -1, NULL,			NULL, NULL);		rv = RegOpenKeyExW(rootkey, wc_subkey, 0, access, &reg_key);		g_free(wc_subkey);	} else {		char *cp_subkey = g_locale_from_utf8(subkey, -1, NULL,			NULL, NULL);		rv = RegOpenKeyExA(rootkey, cp_subkey, 0, access, &reg_key);		g_free(cp_subkey);	}	if (rv != ERROR_SUCCESS) {		char *errmsg = g_win32_error_message(rv);		purple_debug_info("wpurple", "Could not open reg key '%s' subkey '%s'.\nMessage: (%ld) %s\n",					((rootkey == HKEY_LOCAL_MACHINE) ? "HKLM" :					 (rootkey == HKEY_CURRENT_USER) ? "HKCU" :					  (rootkey == HKEY_CLASSES_ROOT) ? "HKCR" : "???"),					subkey, rv, errmsg);		g_free(errmsg);	}	return reg_key;}static gboolean _reg_read(HKEY reg_key, const char *valname, LPDWORD type, LPBYTE data, LPDWORD data_len) {	LONG rv;	if(G_WIN32_HAVE_WIDECHAR_API()) {		wchar_t *wc_valname = NULL;		if (valname)			wc_valname = g_utf8_to_utf16(valname, -1, NULL, NULL, NULL);		rv = RegQueryValueExW(reg_key, wc_valname, 0, type, data, data_len);		g_free(wc_valname);	} else {		char *cp_valname = NULL;		if(valname)			cp_valname = g_locale_from_utf8(valname, -1, NULL, NULL, NULL);		rv = RegQueryValueExA(reg_key, cp_valname, 0, type, data, data_len);		g_free(cp_valname);	}	if (rv != ERROR_SUCCESS) {		char *errmsg = g_win32_error_message(rv);		purple_debug_info("wpurple", "Could not read from reg key value '%s'.\nMessage: (%ld) %s\n",					valname, rv, errmsg);		g_free(errmsg);	}	return (rv == ERROR_SUCCESS);}gboolean wpurple_read_reg_dword(HKEY rootkey, const char *subkey, const char *valname, LPDWORD result) {	DWORD type;	DWORD nbytes;	HKEY reg_key = _reg_open_key(rootkey, subkey, KEY_QUERY_VALUE);	gboolean success = FALSE;	if(reg_key) {		if(_reg_read(reg_key, valname, &type, (LPBYTE)result, &nbytes))			success = TRUE;		RegCloseKey(reg_key);	}	return success;}char *wpurple_read_reg_string(HKEY rootkey, const char *subkey, const char *valname) {	DWORD type;	DWORD nbytes;	HKEY reg_key = _reg_open_key(rootkey, subkey, KEY_QUERY_VALUE);	char *result = NULL;	if(reg_key) {		if(_reg_read(reg_key, valname, &type, NULL, &nbytes) && type == REG_SZ) {			LPBYTE data;			if(G_WIN32_HAVE_WIDECHAR_API())				data = (LPBYTE) g_new(wchar_t, ((nbytes + 1) / sizeof(wchar_t)) + 1);			else				data = (LPBYTE) g_malloc(nbytes + 1);			if(_reg_read(reg_key, valname, &type, data, &nbytes)) {				if(G_WIN32_HAVE_WIDECHAR_API()) {					wchar_t *wc_temp = (wchar_t*) data;					wc_temp[nbytes / sizeof(wchar_t)] = '\0';					result = g_utf16_to_utf8(wc_temp, -1,						NULL, NULL, NULL);				} else {					char *cp_temp = (char*) data;					cp_temp[nbytes] = '\0';					result = g_locale_to_utf8(cp_temp, -1,						NULL, NULL, NULL);				}			}			g_free(data);		}		RegCloseKey(reg_key);	}	return result;}static void wpurple_refresh_proxy(void) {	gboolean set_proxy = FALSE;	DWORD enabled = 0;	wpurple_read_reg_dword(HKEY_CURRENT_USER, WIN32_PROXY_REGKEY,				"ProxyEnable", &enabled);	if (enabled & 1) {		char *c = NULL;		char *tmp = wpurple_read_reg_string(HKEY_CURRENT_USER, WIN32_PROXY_REGKEY,				"ProxyServer");		/* There are proxy settings for several protocols */		if (tmp && (c = g_strstr_len(tmp, strlen(tmp), "http="))) {			char *d;			c += strlen("http=");			d = strchr(c, ';');			if (d)				*d = '\0';			/* c now points the proxy server (and port) */		/* There is only a global proxy */		} else if (tmp && strlen(tmp) > 0 && !strchr(tmp, ';')) {			c = tmp;		}		if (c) {			purple_debug_info("wpurple", "Setting HTTP Proxy: 'http://%s'\n", c);			g_setenv("HTTP_PROXY", c, TRUE);			set_proxy = TRUE;		}		g_free(tmp);	}	/* If there previously was a proxy set and there isn't one now, clear it */	if (getenv("HTTP_PROXY") && !set_proxy) {		purple_debug_info("wpurple", "Clearing HTTP Proxy\n");		g_unsetenv("HTTP_PROXY");	}}static void watch_for_proxy_changes(void) {	LONG rv;	DWORD filter = REG_NOTIFY_CHANGE_NAME |			REG_NOTIFY_CHANGE_LAST_SET;	if (!proxy_regkey &&			!(proxy_regkey = _reg_open_key(HKEY_CURRENT_USER,				WIN32_PROXY_REGKEY, KEY_NOTIFY))) {		return;	}	if (!(proxy_change_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {		char *errmsg = g_win32_error_message(GetLastError());		purple_debug_error("wpurple", "Unable to watch for proxy changes: %s\n", errmsg);		g_free(errmsg);		return;	}	rv = RegNotifyChangeKeyValue(proxy_regkey, TRUE, filter, proxy_change_event, TRUE);	if (rv != ERROR_SUCCESS) {		char *errmsg = g_win32_error_message(rv);		purple_debug_error("wpurple", "Unable to watch for proxy changes: %s\n", errmsg);		g_free(errmsg);		CloseHandle(proxy_change_event);		proxy_change_event = NULL;	}}gboolean wpurple_check_for_proxy_changes(void) {	gboolean changed = FALSE;	if (proxy_change_event && WaitForSingleObject(proxy_change_event, 0) == WAIT_OBJECT_0) {		CloseHandle(proxy_change_event);		proxy_change_event = NULL;		changed = TRUE;		wpurple_refresh_proxy();		watch_for_proxy_changes();	}	return changed;}void wpurple_init(void) {	WORD wVersionRequested;	WSADATA wsaData;	const char *perlenv;	char *newenv;	purple_debug_info("wpurple", "wpurple_init start\n");	purple_debug_info("wpurple", "libpurple version: " VERSION "\n");	purple_debug_info("wpurple", "Glib:%u.%u.%u\n",		glib_major_version, glib_minor_version, glib_micro_version);	/* Winsock init */	wVersionRequested = MAKEWORD(2, 2);	WSAStartup(wVersionRequested, &wsaData);	/* Confirm that the winsock DLL supports 2.2 */	/* Note that if the DLL supports versions greater than	   2.2 in addition to 2.2, it will still return 2.2 in	   wVersion since that is the version we requested. */	if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {		purple_debug_error("wpurple", "Could not find a usable WinSock DLL.  Oh well.\n");		WSACleanup();	}	/* Set Environmental Variables */	/* Tell perl where to find Purple's perl modules */	perlenv = g_getenv("PERL5LIB");	newenv = g_strdup_printf("%s%s%s" G_DIR_SEPARATOR_S "perlmod;",		perlenv ? perlenv : "",		perlenv ? ";" : "",		wpurple_install_dir());	if (!g_setenv("PERL5LIB", newenv, TRUE))		purple_debug_warning("wpurple", "putenv failed for PERL5LIB\n");	g_free(newenv);	if (!g_thread_supported())		g_thread_init(NULL);	/* If the proxy server environment variables are already set,	 * we shouldn't override them */	if (!getenv("HTTP_PROXY") && !getenv("http_proxy") && !getenv("HTTPPROXY")) {		wpurple_refresh_proxy();		watch_for_proxy_changes();	} else {		purple_debug_info("wpurple", "HTTP_PROXY env. var already set.  Ignoring win32 Internet Settings.\n");	}	purple_debug_info("wpurple", "wpurple_init end\n");}/* Windows Cleanup */void wpurple_cleanup(void) {	purple_debug_info("wpurple", "wpurple_cleanup\n");	/* winsock cleanup */	WSACleanup();	g_free(app_data_dir);	app_data_dir = NULL;	if (proxy_regkey) {		RegCloseKey(proxy_regkey);		proxy_regkey = NULL;	}	libpurpledll_hInstance = NULL;}/* DLL initializer *//* suppress gcc "no previous prototype" warning */BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {	libpurpledll_hInstance = hinstDLL;	return TRUE;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?