PDA

View Full Version : [Win32/C] Environment Variable Setter



Anthony`
November 29th, 2010, 01:35
Im relatively new to the C language, so I decided to do something which isnt too heavy on C itself but focuses more on the windows API. Here is the source and the exe is attatched,

/*
/*
* File: main.c
* Author: Anthony Calandra
*
* Created on November 27, 2010, 4:34 PM
*/
#include <windows.h>
#include <stdio.h>
const char class_name[] = "EnvironmentVarSetter";
char buffer[] = {0};
HINSTANCE handle_instance;
HWND hbutton = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int ModifyEnvironmentVar(const char* module, char* new_val, char* buff);
int GetEnvironmentVar(const char* module, char* buff);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
WNDCLASSEX wc;
MSG msg;
HWND hwnd, textbox, textbox2, textbox3, button;
handle_instance = hInstance;
LoadLibrary("RICHED32.DLL");
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszMenuName = NULL;
wc.lpszClassName = class_name;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, class_name,
"Environment Variable Setter - Anthony`",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT,
CW_USEDEFAULT, 550, 300, NULL, NULL, hInstance, NULL);
GetEnvironmentVar("CLASSPATH", buffer);
textbox = CreateWindowEx(WS_EX_CLIENTEDGE, "RICHEDIT", (LPCSTR) buffer,
WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
25, 25, 500, 25, hwnd, (HMENU) 2, GetModuleHandle(NULL), NULL);
GetEnvironmentVar("Path", buffer);
textbox2 = CreateWindowEx(WS_EX_CLIENTEDGE, "RICHEDIT", (LPCSTR) buffer,
WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
25, 75, 500, 25, hwnd, (HMENU) 3, GetModuleHandle(NULL), NULL);
textbox3 = CreateWindowEx(WS_EX_CLIENTEDGE, "RICHEDIT", "",
WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
25, 150, 500, 25, hwnd, (HMENU) 4, GetModuleHandle(NULL), NULL);
button = CreateWindowEx((DWORD) NULL, "BUTTON", "Set Variables",
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 25, 200, 100, 24,
hwnd, (HMENU) 5, GetModuleHandle(NULL), NULL);
hbutton = textbox3;
if (hwnd == NULL || textbox == NULL || textbox2 == NULL || textbox3 == NULL
|| button == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
LPWSTR textbox_buffer[256];
switch(msg)
{
case WM_CREATE:
MessageBox(NULL, "Thanks for using!\nCreated by Anthony`",
"Environment Variable Setter", MB_OK);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, 5, 5, "Current CLASSPATH", 18);
TextOut(hdc, 5, 55, "Current Path", 13);
TextOut(hdc, 5, 125, "Input JDK version (Example: 18, 20, 21)", 40);
EndPaint(hwnd, &ps);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case 5:
SendMessage(hbutton, WM_GETTEXT,
sizeof(textbox_buffer) / sizeof(textbox_buffer[0]),
(LPARAM)textbox_buffer);
WIN32_FIND_DATA result;
HANDLE list;
char jdk_path[] = "C:\\Program Files (x86)\\Java\\jdk1.6.0_";
strcat(jdk_path, (const char *) (LPWSTR) textbox_buffer);
strcat(jdk_path, "\\bin (file://\\bin);");
list = FindFirstFile((LPCSTR) jdk_path, &result);
if (list == INVALID_HANDLE_VALUE) {
char jdk_path2[] = "C:\\Program Files\\Java\\jdk1.6.0_";
strcat(jdk_path2, (const char *) (LPWSTR) textbox_buffer);
strcat(jdk_path2, "\\bin (file://\\bin);");
list = FindFirstFile((LPCSTR) jdk_path2, &result);
ModifyEnvironmentVar("CLASSPATH", jdk_path2, buffer);
ModifyEnvironmentVar("Path", jdk_path2, buffer);
FindClose(list);
break;
}
ModifyEnvironmentVar("CLASSPATH", jdk_path, buffer);
ModifyEnvironmentVar("Path", jdk_path, buffer);
FindClose(list);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int GetEnvironmentVar(const char* module, char* buff) {
HKEY key;
const char* key_string = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
LONG return_val;
DWORD val_type = REG_EXPAND_SZ, buff_size = 1024;
return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key_string, 0, KEY_ALL_ACCESS, &key);
if (return_val != ERROR_SUCCESS) {
printf("Error opening registry key: %s \nUser permissions perhaps?", key_string);
return 0;
}
return_val = RegQueryValueEx(key, module, NULL, &val_type, (LPBYTE) buff, &buff_size);
if (return_val != ERROR_SUCCESS) {
LPVOID error_buff;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &error_buff, 0, NULL);
printf("Error querying registry value - %s", error_buff);
return 0;
}
return 1;
}
int ModifyEnvironmentVar(const char* module, char* new_val, char* buff) {
HKEY key;
const char* key_string = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
LONG return_val;
DWORD val_type = REG_EXPAND_SZ, buff_size = 1024;
return_val = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key_string, 0, KEY_ALL_ACCESS, &key);
if (return_val != ERROR_SUCCESS) {
printf("Error opening registry key: %s \nUser permissions perhaps?", key_string);
return 0;
}
return_val = RegQueryValueEx(key, module, NULL, &val_type, (LPBYTE) buff, &buff_size);
if (return_val != ERROR_SUCCESS) {
LPVOID error_buff;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &error_buff, 0, NULL);
printf("Error querying registry value - %s", error_buff);
return 0;
}
if (buff[strlen(buff)-1] != ';')
strcat(buff, ";");
strcat(buff, new_val);
return_val = RegSetValueEx(key, module, 0, val_type, (LPBYTE) buff, strlen(buff)+1);
if (return_val == ERROR_SUCCESS)
MessageBox(NULL, "Registry Successfully modified!", "Complete", MB_OK);
else
MessageBox(NULL, "Registry Successfully modified!", "Incomplete", MB_OK | MB_ICONERROR);
return 1;
}

suggestions pls

fourshorty
November 29th, 2010, 02:16
No Virus Scanned with AVG Checking out now...
EDIT : Nice Work Continue With what you are doing!

Anthony`
November 29th, 2010, 02:17
if you dont trust it simply dont use it, im not providing millions of virus scans for all the skeptics.

Trey
November 29th, 2010, 02:58
Good work, glad you're venturing into the wonderful world of C. <3.

Anthony`
November 29th, 2010, 02:59
ty babe, more to cum when im bored out of mind <3

Trey
November 29th, 2010, 03:01
ty babe, more to cum when im bored out of mind <3

I may start posting some of the things I've written on the side, since I've released literally nothing on these forums, luuuul.

We need to bring these boards to life.

Anthony`
November 29th, 2010, 03:05
Exactly, usually I dont like posting anything ive written here for some reason but I will probably begin to now.

Faab234
November 29th, 2010, 06:44
I'm also new at it, I'll try to look and learn. Thanks.

Anthony`
November 29th, 2010, 21:15
change
GetEnvironmentVar("Path", &buffer);
to
GetEnvironmentVar("Path", buffer);

and

GetEnvironmentVar("CLASSPATH", &buffer);
to
GetEnvironmentVar("CLASSPATH", buffer);

for maximum points!! (and the gifted ability to compile this)

ps i'd recommend casting the void* to char* just to omit the ugly format warnings


For some reason I was able to compile this perfectly whether passing its address was done or not. And im not sure which format warnings your talking about as that isnt showing up either. :P

Trey
November 29th, 2010, 21:19
change
GetEnvironmentVar("Path", &buffer);
to
GetEnvironmentVar("Path", buffer);

and

GetEnvironmentVar("CLASSPATH", &buffer);
to
GetEnvironmentVar("CLASSPATH", buffer);

for maximum points!! (and the gifted ability to compile this)

ps i'd recommend casting the void* to char* just to omit the ugly format warnings

edit ps i'd ditch the winapi while you're still sane, it's just that bad (try wxwidgets (c++), qt (c++), gtk+ (c), gtkmm (c++) etc.)

&buffer and buffer do the same thing, arrays are passed as pointers in the first place.
Passing &buffer would be the address of the first position in the array, and the compiler will implicitly do that anyway.

Trey
November 29th, 2010, 23:26
gcc is a great creature.


|46|error: cannot convert 'char (*)[1]' to 'char*' for argument '2' to 'int GetEnvironmentVar(const char*, char*)'
|50|error: cannot convert 'char (*)[1]' to 'char*' for argument '2' to 'int GetEnvironmentVar(const char*, char*)'|

you were saying?



test.c:187:2: warning: no newline at end of file
Press any key to continue . . .




#include <stdio.h>

void doshit(int *array) {
int i = 0;
for(; i < 4; i++)
printf("%d\n", array[i]);
}

int main() {
int array[] = {2,5,6,6};
int *ptr = array;
doshit(ptr);
ptr = &array;
doshit(ptr);
ptr = &array[0];
doshit(ptr);
return 0;
}




2
5
6
6
2
5
6
6
2
5
6
6


You were saying? I'm using GCC as well, without any issues.

Anthony`
November 30th, 2010, 00:05
All I removed was the address operator (or whatever the fuck its called) because it is not necessarily needed, but I wont post my say on this little discussion since I still need to grasp my pointers. :D

Trey
November 30th, 2010, 00:14
that compiled fine for me, except a little 'assignment from incompatible pointer type' warning on exactly the same settings as i compiled the environment variable setter. does your evs compile fine with the &'s because it seems Anthony` has removed them from the original code.

Yup, it compiles for me. It mentions the same warning that you received about the example I posted, nothing a quick cast wont fix.

Trey
November 30th, 2010, 00:24
what gcc version do you use

edit: on what platform?

version 3.4.4 as provided by MinGW on Windows.