[e56fab]: src / GenFixEnv / GenFixEnv.c  Maximize  Restore  History

Download this file

147 lines (131 with data), 4.2 kB

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// GenFixEnv.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <windows.h>
#include <shlobj.h>
static void GenerateFromReg(FILE *BatchFile)
{
LONG Err;
HKEY UserEnvironment;
DWORD Index;
char ValueName[256];
DWORD ValueNameSize;
DWORD Type;
char Data[1024];
DWORD DataSize;
Err = RegOpenKeyExA(HKEY_CURRENT_USER, "Environment", 0, KEY_QUERY_VALUE,
&UserEnvironment);
if (Err != ERROR_SUCCESS)
return;
Index = 0;
do
{
ValueNameSize = sizeof(ValueName);
DataSize = sizeof(Data);
Err = RegEnumValueA(UserEnvironment, Index, ValueName, &ValueNameSize,
NULL, &Type, (LPBYTE) Data, &DataSize);
if (Err == ERROR_SUCCESS)
{
if (Type == REG_EXPAND_SZ)
{
char Expanded[sizeof(Data)];
DWORD ExpandedSize;
ExpandedSize = ExpandEnvironmentStringsA(Data, Expanded,
sizeof(Expanded));
memcpy(Data, Expanded, ExpandedSize);
Type = REG_SZ;
}
if (Type == REG_SZ)
{
if (strcmp(ValueName, "TEMP") != 0 && strcmp(ValueName, "TMP") != 0)
fprintf(BatchFile, "SET \"%s=%s\"\n", ValueName, Data);
else
{
char Short[sizeof(Data)];
GetShortPathNameA(Data, Short, sizeof(Short));
fprintf(BatchFile, "SET \"%s=%s\"\n", ValueName, Short);
}
}
}
Index++;
}
while (Err == ERROR_SUCCESS);
RegCloseKey(UserEnvironment);
}
static void GenerateUserProfile(FILE *BatchFile)
{
BOOL (WINAPI *pOpenProcessToken)(HANDLE,DWORD,PHANDLE);
BOOL (WINAPI *pGetUserProfileDirectoryA)(HANDLE,LPSTR,LPDWORD);
HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
HMODULE huserenv = LoadLibraryA("userenv.dll");
BOOL NoErr;
HANDLE Token;
char Data[1024];
DWORD DataSize;
pOpenProcessToken = (void *)GetProcAddress(hadvapi32, "OpenProcessToken");
pGetUserProfileDirectoryA = (void *)GetProcAddress(huserenv,
"GetUserProfileDirectoryA");
if (pOpenProcessToken == NULL || pGetUserProfileDirectoryA == NULL)
return;
if (! pOpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token))
return;
DataSize = sizeof(Data);
if (! pGetUserProfileDirectoryA(Token, Data, &DataSize))
return;
fprintf(BatchFile, "SET \"USERPROFILE=%s\"\n", Data);
CloseHandle(Token);
}
static void GenerateCSIDL(FILE *BatchFile)
{
char Path[_MAX_PATH];
HMODULE Mod;
HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
HRESULT Res;
Mod = LoadLibraryA("shell32.dll");
pSHGetFolderPathA = (void *) GetProcAddress(Mod, "SHGetFolderPathA");
if (pSHGetFolderPathA == NULL)
{
FreeLibrary(Mod);
Mod = LoadLibraryA("shfolder.dll");
pSHGetFolderPathA = (void *) GetProcAddress(Mod, "SHGetFolderPathA");
}
if (pSHGetFolderPathA != NULL)
{
if (GetEnvironmentVariable("APPDATA", Path, sizeof(Path)) == 0)
{
Res = pSHGetFolderPathA(NULL, CSIDL_APPDATA, NULL,
SHGFP_TYPE_CURRENT, Path);
if (SUCCEEDED(Res))
fprintf(BatchFile, "SET \"APPDATA=%s\"\n", Path);
}
if (GetEnvironmentVariable("LOCALAPPDATA", Path, sizeof(Path)) == 0)
{
Res = pSHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL,
SHGFP_TYPE_CURRENT, Path);
if (SUCCEEDED(Res))
fprintf(BatchFile, "SET \"LOCALAPPDATA=%s\"\n", Path);
}
}
FreeLibrary(Mod);
}
int main(int argc, char *argv[])
{
FILE *BatchFile;
if (argc != 2)
{
fprintf(stderr, "Usage: GenFixEnv <BatchFile>\n");
exit(1);
}
BatchFile = fopen(argv[1], "w");
if (BatchFile == NULL)
{
perror("Unable to open output file");
exit(1);
}
fprintf(BatchFile, "@echo off\n");
GenerateFromReg(BatchFile);
GenerateUserProfile(BatchFile);
GenerateCSIDL(BatchFile);
fclose(BatchFile);
return 0;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks