File: platform_win_util.cpp

package info (click to toggle)
launchy 2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,536 kB
  • ctags: 1,561
  • sloc: cpp: 11,735; sh: 162; makefile: 45
file content (116 lines) | stat: -rw-r--r-- 3,266 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Launchy: Application Launcher
Copyright (C) 2007 Josh Karlin, Simon Capewell

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


#include "precompiled.h"
#include "platform_win.h"
#include "globals.h"
#include "main.h"


// Replace this process' environment with the current system environment
void UpdateEnvironment()
{
	// Fetch the current environment for the user
	HANDLE accessToken;
	if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &accessToken))
		return;

	wchar_t* environment;
	if (!CreateEnvironmentBlock((LPVOID*)&environment, accessToken, FALSE))
		return;

	// Empty the current environment
	QStringList variables;
	wchar_t* currentEnvironment = GetEnvironmentStrings();
	for (TCHAR* p = currentEnvironment; *p != 0;)
	{
		QString variable = QString::fromUtf16(p);
		QString name = variable.section("=", 0, 0);
		// Ignore entries for drive current directory entries that have no name
		if (name.size() > 0)
			variables.append(name);
		p += wcslen(p) + 1;
	}
	if (currentEnvironment)
		FreeEnvironmentStrings(currentEnvironment);

	// Now we've finished enumerating the current environment, we can safely delete variables
	foreach (QString name, variables)
	{
		SetEnvironmentVariable(name.utf16(), NULL);
	}

	// Recreate the environment using the fresh system copy
	for (wchar_t* p = environment; *p != 0;)
	{
		wchar_t* name = p;
		wchar_t* value = wcschr(p, L'=');
		p += wcslen(p) + 1;
		if (value)
		{
			*value = L'\0';
			SetEnvironmentVariable(name, value + 1);
		}
	}

	DestroyEnvironmentBlock(environment);
	CloseHandle(accessToken);
}


QString GetShellDirectory(int type)
{
	wchar_t buffer[_MAX_PATH];
	SHGetFolderPath(NULL, type, NULL, 0, buffer);
	return QString::fromUtf16(buffer);
}


bool EnumerateNetworkServers(QList<QString>& items, DWORD serverType, const wchar_t* domain)
{
	SERVER_INFO_100* serverInfo = 0;
	DWORD read, totalOnNetwork;

	NET_API_STATUS result = NetServerEnum(NULL, 100, (BYTE**)&serverInfo, MAX_PREFERRED_LENGTH, 
		&read, &totalOnNetwork, serverType, domain, 0);
	if (result == NERR_Success)
	{
		for (DWORD i = 0; i < read; ++i)
		{
			QString name = QString::fromUtf16((ushort*)serverInfo[i].sv100_name);
			items.push_back(name);
		}
	}

	// Possible error codes
	// ERROR_ACCESS_DENIED: "The user does not have access to the requested information."
	// NERR_InvalidComputer: "The computer name is invalid."
	// ERROR_NO_BROWSER_SERVERS_FOUND: "No browser servers found."
	// ERROR_MORE_DATA: "More entries are available with subsequent calls."

	if (serverInfo)
	{
		NetApiBufferFree((void*)serverInfo);
	}

	return result == NERR_Success;
}