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
/*
/*
* 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