SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page [Win32/C] Environment Variable Setter


[Win32/C] Environment Variable Setter
Old
  (#1 (permalink))
Evolution is Offline
Also known as Anthony`
image
 
Evolution's Avatar
 
Posts: 43
Cookies Given: 0
Cookies Received: 0
Time Online: 2 h 50 m
   
Lightbulb [Win32/C] Environment Variable Setter - 02-25-2011, 11:08 PM

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,
Code:
/*
/*
 * 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[/URL];");
                    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[/URL];");
                        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
Attached Files
File Type: zip cstuff.zip (14.5 KB, 0 views)
  
adsense code2.3
Closed Thread

Bookmarks

[Win32/C] Environment Variable Setter

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:59 PM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.