/*
 * TezzNative SDK Installer  —  tools/lang_installer_win.c
 * Self-contained: all SDK files are embedded via payload.h (generated by bin2h.exe).
 * The installer requires no external files at run time.
 *
 * Build via:  tools\build_lang_installer.ps1
 */
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlobj.h>
#include <commctrl.h>
#include <string.h>
#include <stdio.h>

#pragma comment(lib,"user32.lib")
#pragma comment(lib,"shell32.lib")
#pragma comment(lib,"advapi32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"comctl32.lib")

/* ---- embedded SDK payload ---- */
#include "payload.h"

/* ---- config ---- */
#define APP_NAME    "TezzNative"
#define APP_VER     "1.0.0"
#define VENDOR      "TezzCorp"
#define REG_UNINST  "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\TezzNative"
#define ACCENT_COL  0x00B87A   /* BGR: emerald green */
#define BG_COL      0xF8F8F8
#define BG_DARK     0xEAEAEA
#define TEXT_COL    0x1A1A1A
#define MUTED_COL   0x555555
/* ---- end config ---- */

static char  g_dest[MAX_PATH];
static HWND  g_hwnd, g_progress, g_status, g_btn_install, g_btn_cancel, g_chk_path;
static int   g_page = 0; /* 0=welcome  1=progress  2=done */
static HFONT g_fontTitle, g_fontBody, g_fontSmall, g_fontBtn;

/* ------------------------------------------------------------------ */
/* Helpers                                                              */
/* ------------------------------------------------------------------ */
static void set_status(const char* msg){ SetWindowTextA(g_status, msg); }

static BOOL write_file_from_buf(const char* path, const unsigned char* data, unsigned long sz)
{
    /* ensure parent dir exists */
    char dir[MAX_PATH]; lstrcpyA(dir, path);
    char* sep = strrchr(dir, '\\');
    if (sep) { *sep = 0; CreateDirectoryA(dir, NULL); }

    HANDLE h = CreateFileA(path, GENERIC_WRITE, 0, NULL,
                           CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (h == INVALID_HANDLE_VALUE) return FALSE;
    DWORD wr; WriteFile(h, data, (DWORD)sz, &wr, NULL);
    CloseHandle(h);
    return (wr == (DWORD)sz);
}

/* ------------------------------------------------------------------ */
/* Install thread                                                       */
/* ------------------------------------------------------------------ */
static DWORD WINAPI do_install(void* _)
{
    CreateDirectoryA(g_dest, NULL);

    int total = 0;
    for (int i = 0; g_payload[i].path; i++) total++;
    SendMessageA(g_progress, PBM_SETRANGE, 0, MAKELPARAM(0, total + 2));
    SendMessageA(g_progress, PBM_SETSTEP,  1, 0);
    SendMessageA(g_progress, PBM_SETPOS,   0, 0);

    /* extract every embedded file */
    for (int i = 0; g_payload[i].path; i++) {
        char dst[MAX_PATH];
        snprintf(dst, MAX_PATH, "%s\\%s", g_dest, g_payload[i].path);
        char msg[MAX_PATH + 32];
        snprintf(msg, sizeof(msg), "Extracting  %s ...", g_payload[i].path);
        set_status(msg);

        if (!write_file_from_buf(dst, g_payload[i].data, g_payload[i].size)) {
            char err[MAX_PATH + 64];
            snprintf(err, sizeof(err), "Failed to extract:\n%s\n\nError %lu", dst, GetLastError());
            MessageBoxA(g_hwnd, err, "Install Error", MB_ICONERROR);
            PostMessageA(g_hwnd, WM_CLOSE, 0, 0);
            return 1;
        }
        SendMessageA(g_progress, PBM_STEPIT, 0, 0);
    }

    /* PATH */
    if(SendMessageA(g_chk_path, BM_GETCHECK, 0, 0) == BST_CHECKED) {
        set_status("Updating system PATH ...");
        HKEY k;
        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,
                "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
                0, KEY_READ | KEY_WRITE, &k) == ERROR_SUCCESS) {
            char cur[32768] = {0};
            DWORD sz = sizeof(cur);
            RegQueryValueExA(k, "Path", NULL, NULL, (BYTE*)cur, &sz);
            if(!strstr(cur, g_dest)) {
                char newp[32768];
                snprintf(newp, sizeof(newp), "%s;%s", cur, g_dest);
                RegSetValueExA(k, "Path", 0, REG_EXPAND_SZ,
                               (BYTE*)newp, (DWORD)strlen(newp)+1);
            }
            /* Always set TEZZ_HOME so stdlib is always findable */
            RegSetValueExA(k, "TEZZ_HOME", 0, REG_SZ,
                           (BYTE*)g_dest, (DWORD)strlen(g_dest)+1);
            SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
                                (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, NULL);
            RegCloseKey(k);
        }
    } else {
        /* Even if PATH checkbox unchecked, still set TEZZ_HOME */
        HKEY k;
        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,
                "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
                0, KEY_READ | KEY_WRITE, &k) == ERROR_SUCCESS) {
            RegSetValueExA(k, "TEZZ_HOME", 0, REG_SZ,
                           (BYTE*)g_dest, (DWORD)strlen(g_dest)+1);
            SendMessageTimeoutA(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
                                (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, NULL);
            RegCloseKey(k);
        }
    }
    SendMessageA(g_progress, PBM_STEPIT, 0, 0);

    /* registry */
    set_status("Writing registry ...");
    {
        HKEY k;
        RegCreateKeyExA(HKEY_LOCAL_MACHINE, REG_UNINST, 0, NULL, 0, KEY_WRITE, NULL, &k, NULL);
        char dname[] = APP_NAME " " APP_VER;
        RegSetValueExA(k,"DisplayName",   0,REG_SZ,(BYTE*)dname,  (DWORD)strlen(dname)+1);
        RegSetValueExA(k,"DisplayVersion",0,REG_SZ,(BYTE*)APP_VER,(DWORD)strlen(APP_VER)+1);
        RegSetValueExA(k,"Publisher",     0,REG_SZ,(BYTE*)VENDOR,  (DWORD)strlen(VENDOR)+1);
        RegSetValueExA(k,"InstallLocation",0,REG_SZ,(BYTE*)g_dest,(DWORD)strlen(g_dest)+1);
        char uninst[MAX_PATH];
        snprintf(uninst,MAX_PATH,"\"%s\\Uninstall.exe\"",g_dest);
        RegSetValueExA(k,"UninstallString",0,REG_SZ,(BYTE*)uninst,(DWORD)strlen(uninst)+1);
        RegCloseKey(k);
    }

    /* copy self as uninstaller */
    set_status("Finalising ...");
    {
        char self[MAX_PATH]; GetModuleFileNameA(NULL, self, MAX_PATH);
        char unst[MAX_PATH]; snprintf(unst,MAX_PATH,"%s\\Uninstall.exe",g_dest);
        CopyFileA(self, unst, FALSE);
        HKEY k; RegOpenKeyExA(HKEY_LOCAL_MACHINE,REG_UNINST,0,KEY_WRITE,&k);
        DWORD m=1; RegSetValueExA(k,"_IsUninstaller",0,REG_DWORD,(BYTE*)&m,4);
        RegCloseKey(k);
    }
    SendMessageA(g_progress, PBM_STEPIT, 0, 0);

    set_status("Installation complete!");
    g_page = 2;
    PostMessageA(g_hwnd, WM_USER+1, 0, 0);
    return 0;
}

/* ------------------------------------------------------------------ */
/* Uninstall mode                                                       */
/* ------------------------------------------------------------------ */
static void do_uninstall(void)
{
    if (MessageBoxA(NULL,
            "Are you sure you want to remove " APP_NAME " " APP_VER "?\n\n"
            "This will delete the compiler, standard library, and all installed files.",
            "Uninstall " APP_NAME, MB_YESNO|MB_ICONQUESTION) != IDYES) return;

    char inst[MAX_PATH] = {0};
    HKEY k;
    if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,REG_UNINST,0,KEY_READ,&k)==ERROR_SUCCESS){
        DWORD sz=MAX_PATH;
        RegQueryValueExA(k,"InstallLocation",NULL,NULL,(BYTE*)inst,&sz);
        RegCloseKey(k);
    }
    if (inst[0]) {
        /* delete known payload files */
        for (int i=0; g_payload[i].path; i++){
            char f[MAX_PATH]; snprintf(f,MAX_PATH,"%s\\%s",inst,g_payload[i].path);
            DeleteFileA(f);
        }
        /* remove lib dir */
        char lib[MAX_PATH]; snprintf(lib,MAX_PATH,"%s\\lib",inst);
        WIN32_FIND_DATAA fd;
        char pat[MAX_PATH]; snprintf(pat,MAX_PATH,"%s\\*.*",lib);
        HANDLE h = FindFirstFileA(pat,&fd);
        if(h!=INVALID_HANDLE_VALUE){
            do { char f[MAX_PATH]; snprintf(f,MAX_PATH,"%s\\%s",lib,fd.cFileName); DeleteFileA(f); }
            while(FindNextFileA(h,&fd)); FindClose(h);
        }
        RemoveDirectoryA(lib);
        char unst[MAX_PATH]; snprintf(unst,MAX_PATH,"%s\\Uninstall.exe",inst);
        MoveFileExA(unst,NULL,MOVEFILE_DELAY_UNTIL_REBOOT);
        RemoveDirectoryA(inst);

        /* remove from PATH */
        if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,
                "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
                0,KEY_READ|KEY_WRITE,&k)==ERROR_SUCCESS){
            char cur[32768]={0}; DWORD sz=sizeof(cur);
            RegQueryValueExA(k,"Path",NULL,NULL,(BYTE*)cur,&sz);
            char* p=strstr(cur,inst);
            if(p){
                char newp[32768]={0};
                size_t pfx=p-cur;
                if(pfx>0&&cur[pfx-1]==';') pfx--;
                strncpy(newp,cur,pfx);
                char* sfx=p+strlen(inst); if(*sfx==';') sfx++;
                strcat(newp,sfx);
                RegSetValueExA(k,"Path",0,REG_EXPAND_SZ,(BYTE*)newp,(DWORD)strlen(newp)+1);
                SendMessageTimeoutA(HWND_BROADCAST,WM_SETTINGCHANGE,0,
                                    (LPARAM)"Environment",SMTO_ABORTIFHUNG,5000,NULL);
            }
            RegCloseKey(k);
        }
    }
    RegDeleteKeyA(HKEY_LOCAL_MACHINE, REG_UNINST);
    MessageBoxA(NULL, APP_NAME " has been removed from your computer.",
                "Uninstall Complete", MB_OK|MB_ICONINFORMATION);
}

/* ------------------------------------------------------------------ */
/* Painting helpers                                                     */
/* ------------------------------------------------------------------ */
static void paint_sidebar(HDC dc, RECT* wr)
{
    RECT sb = {0, 0, 200, wr->bottom};
    HBRUSH bg = CreateSolidBrush(RGB(18,24,38)); FillRect(dc,&sb,bg); DeleteObject(bg);

    /* logo / brand text */
    SetBkMode(dc,TRANSPARENT);
    SelectObject(dc, g_fontTitle);
    SetTextColor(dc, RGB(16,185,129));
    RECT lr={20,40,185,90}; DrawTextA(dc,"TezzNative",-1,&lr,DT_LEFT|DT_TOP|DT_SINGLELINE);

    SelectObject(dc, g_fontSmall);
    SetTextColor(dc,RGB(100,116,139));
    RECT vr={20,82,185,105}; DrawTextA(dc,"SDK " APP_VER " Setup",-1,&vr,DT_LEFT|DT_TOP);

    /* step dots */
    const char* steps[] = {"Welcome","Install","Complete",NULL};
    for(int i=0;steps[i];i++){
        int y = 160+i*44;
        HBRUSH dot = CreateSolidBrush(g_page==i ? RGB(16,185,129) : RGB(40,52,70));
        RECT dr={20,y,32,y+12}; FillRect(dc,&dr,dot); DeleteObject(dot);
        SetTextColor(dc, g_page==i ? RGB(248,250,252) : RGB(100,116,139));
        SelectObject(dc,g_fontSmall);
        RECT tr={40,y-2,185,y+16}; DrawTextA(dc,steps[i],-1,&tr,DT_LEFT|DT_TOP);
    }

    /* version badge at bottom */
    SelectObject(dc,g_fontSmall);
    SetTextColor(dc,RGB(51,65,85));
    RECT br={20,wr->bottom-30,185,wr->bottom}; DrawTextA(dc,"tn.tezzcorp.com",-1,&br,DT_LEFT|DT_TOP);
}

static void paint_main(HDC dc, RECT* wr)
{
    RECT mr={200,0,wr->right,wr->bottom};
    HBRUSH bg=CreateSolidBrush(RGB(248,250,252)); FillRect(dc,&mr,bg); DeleteObject(bg);

    /* thin accent top line */
    HBRUSH ac=CreateSolidBrush(RGB(16,185,129));
    RECT al={200,0,wr->right,3}; FillRect(dc,&al,ac); DeleteObject(ac);

    SetBkMode(dc,TRANSPARENT);

    if(g_page==0){
        SelectObject(dc,g_fontTitle);
        SetTextColor(dc,RGB(15,23,42));
        RECT tr={230,40,wr->right-20,90};
        DrawTextA(dc,"Welcome to TezzNative",-1,&tr,DT_LEFT|DT_TOP|DT_SINGLELINE);

        SelectObject(dc,g_fontBody);
        SetTextColor(dc,RGB(71,85,105));
        RECT dr={230,95,wr->right-20,270};
        DrawTextA(dc,
            "This installer will set up the TezzNative SDK on your system.\n\n"
            "What will be installed:\n"
            "   \xB7  tezzc.exe — the single-pass compiler\n"
            "   \xB7  Standard library (lib/*.tn)\n"
            "   \xB7  tezz.mod and tezz.lock\n\n"
            "Destination:\n",
            -1,&dr,DT_LEFT|DT_TOP|DT_WORDBREAK);

        /* dest box */
        SelectObject(dc,g_fontSmall);
        SetTextColor(dc,RGB(30,41,59));
        RECT dbr={230,260,wr->right-20,280}; DrawTextA(dc,g_dest,-1,&dbr,DT_LEFT|DT_TOP);

        /* bottom separator */
        HPEN pen=CreatePen(PS_SOLID,1,RGB(226,232,240)); SelectObject(dc,pen);
        MoveToEx(dc,210,wr->bottom-72,NULL); LineTo(dc,wr->right-10,wr->bottom-72);
        DeleteObject(pen);

    } else if(g_page==1){
        SelectObject(dc,g_fontTitle);
        SetTextColor(dc,RGB(15,23,42));
        RECT tr={230,40,wr->right-20,90};
        DrawTextA(dc,"Installing...",-1,&tr,DT_LEFT|DT_TOP|DT_SINGLELINE);

        SelectObject(dc,g_fontBody);
        SetTextColor(dc,RGB(71,85,105));
        RECT dr={230,95,wr->right-20,125};
        DrawTextA(dc,"Extracting SDK files, please wait.",-1,&dr,DT_LEFT|DT_TOP);

        HPEN pen=CreatePen(PS_SOLID,1,RGB(226,232,240)); SelectObject(dc,pen);
        MoveToEx(dc,210,wr->bottom-72,NULL); LineTo(dc,wr->right-10,wr->bottom-72);
        DeleteObject(pen);

    } else if(g_page==2){
        SelectObject(dc,g_fontTitle);
        SetTextColor(dc,RGB(16,185,129));
        RECT tr={230,40,wr->right-20,90};
        DrawTextA(dc,"Installation Complete!",-1,&tr,DT_LEFT|DT_TOP|DT_SINGLELINE);

        SelectObject(dc,g_fontBody);
        SetTextColor(dc,RGB(71,85,105));
        RECT dr={230,95,wr->right-20,280};
        DrawTextA(dc,
            "TezzNative SDK has been installed successfully.\n\n"
            "You can now open a new terminal and type:\n\n"
            "   tezzc run hello.tn --bc\n\n"
            "to start building native applications.\n\n"
            "Visit tn.tezzcorp.com for documentation.",
            -1,&dr,DT_LEFT|DT_TOP|DT_WORDBREAK);

        HPEN pen=CreatePen(PS_SOLID,1,RGB(226,232,240)); SelectObject(dc,pen);
        MoveToEx(dc,210,wr->bottom-72,NULL); LineTo(dc,wr->right-10,wr->bottom-72);
        DeleteObject(pen);
    }
}

/* ------------------------------------------------------------------ */
/* Window procedure                                                     */
/* ------------------------------------------------------------------ */
static LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp, LPARAM lp)
{
    switch(msg){
    case WM_CREATE:{
        g_fontTitle = CreateFontA(-22,0,0,0,FW_BOLD,0,0,0,0,0,0,CLEARTYPE_QUALITY,0,"Segoe UI");
        g_fontBody  = CreateFontA(-14,0,0,0,FW_NORMAL,0,0,0,0,0,0,CLEARTYPE_QUALITY,0,"Segoe UI");
        g_fontSmall = CreateFontA(-12,0,0,0,FW_NORMAL,0,0,0,0,0,0,CLEARTYPE_QUALITY,0,"Segoe UI");
        g_fontBtn   = CreateFontA(-14,0,0,0,FW_SEMIBOLD,0,0,0,0,0,0,CLEARTYPE_QUALITY,0,"Segoe UI");

        /* default install dir */
        char def[MAX_PATH];
        SHGetFolderPathA(NULL,CSIDL_PROGRAM_FILES,NULL,0,def);
        snprintf(def+lstrlenA(def),MAX_PATH-lstrlenA(def),"\\%s",APP_NAME);
        lstrcpyA(g_dest, def);

        /* PATH checkbox */
        g_chk_path = CreateWindowExA(0,"BUTTON","Add TezzNative to system PATH (recommended)",
                        WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
                        215,288,420,20,hw,(HMENU)12,NULL,NULL);
        SendMessageA(g_chk_path,WM_SETFONT,(WPARAM)g_fontSmall,TRUE);
        SendMessageA(g_chk_path,BM_SETCHECK,BST_CHECKED,0); /* on by default */

        /* progress bar */
        g_progress = CreateWindowExA(0,PROGRESS_CLASSA,"",
                        WS_CHILD|PBS_SMOOTH,215,145,420,14,hw,(HMENU)2,NULL,NULL);
        ShowWindow(g_progress,SW_HIDE);

        /* status label */
        g_status = CreateWindowExA(0,"STATIC","",
                        WS_CHILD|SS_LEFT,215,165,420,18,hw,(HMENU)3,NULL,NULL);
        SendMessageA(g_status,WM_SETFONT,(WPARAM)g_fontSmall,TRUE);
        ShowWindow(g_status,SW_HIDE);

        /* buttons */
        RECT wr; GetClientRect(hw,&wr);
        int bw=110, bh=32, by=wr.bottom-52, bx=wr.right-bw-20;
        g_btn_install = CreateWindowExA(0,"BUTTON","Install Now",
                          WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                          bx,by,bw,bh,hw,(HMENU)10,NULL,NULL);
        SendMessageA(g_btn_install,WM_SETFONT,(WPARAM)g_fontBtn,TRUE);

        g_btn_cancel = CreateWindowExA(0,"BUTTON","Cancel",
                         WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                         bx-bw-10,by,bw,bh,hw,(HMENU)11,NULL,NULL);
        SendMessageA(g_btn_cancel,WM_SETFONT,(WPARAM)g_fontBtn,TRUE);
        break;
    }
    case WM_CTLCOLORBTN:
    case WM_CTLCOLORSTATIC:{
        HDC hdc=(HDC)wp; HWND ctrl=(HWND)lp;
        SetBkMode(hdc,TRANSPARENT);
        if(ctrl==g_chk_path||ctrl==g_status){
            SetTextColor(hdc,RGB(71,85,105));
            return (LRESULT)CreateSolidBrush(RGB(248,250,252));
        }
        SetTextColor(hdc,RGB(71,85,105));
        return (LRESULT)GetStockObject(WHITE_BRUSH);
    }
    case WM_SETCURSOR:
        SetCursor(LoadCursorA(NULL,IDC_ARROW));
        return TRUE;
    case WM_ERASEBKGND: return 1;
    case WM_PAINT:{
        PAINTSTRUCT ps; HDC dc=BeginPaint(hw,&ps);
        RECT wr; GetClientRect(hw,&wr);
        paint_sidebar(dc,&wr);
        paint_main(dc,&wr);
        EndPaint(hw,&ps);
        return 0;
    }
    case WM_SIZE: InvalidateRect(hw,NULL,TRUE); break;
    case WM_COMMAND:
        if(LOWORD(wp)==11){ PostQuitMessage(0); break; }
        if(LOWORD(wp)==10){
            if(g_page==0){
                g_page=1;
                ShowWindow(g_chk_path,SW_HIDE);
                ShowWindow(g_progress,SW_SHOW);
                ShowWindow(g_status,SW_SHOW);
                EnableWindow(g_btn_install,FALSE);
                InvalidateRect(hw,NULL,TRUE);
                CreateThread(NULL,0,do_install,NULL,0,NULL);
            } else if(g_page==2){
                PostQuitMessage(0);
            }
            break;
        }
        break;
    case WM_USER+1: /* install done */
        ShowWindow(g_progress,SW_HIDE);
        SetWindowTextA(g_btn_install,"Finish");
        EnableWindow(g_btn_install,TRUE);
        SetWindowTextA(g_btn_cancel,"");
        EnableWindow(g_btn_cancel,FALSE);
        ShowWindow(g_btn_cancel,SW_HIDE);
        InvalidateRect(hw,NULL,TRUE);
        break;
    case WM_DESTROY: PostQuitMessage(0); break;
    }
    return DefWindowProcA(hw,msg,wp,lp);
}

/* ------------------------------------------------------------------ */
/* Entry point                                                          */
/* ------------------------------------------------------------------ */
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cmd, int show)
{
    (void)hp;(void)cmd;(void)show;

    /* uninstall mode? */
    HKEY k; DWORD mode=0,sz=4;
    if(RegOpenKeyExA(HKEY_LOCAL_MACHINE,REG_UNINST,0,KEY_READ,&k)==ERROR_SUCCESS){
        RegQueryValueExA(k,"_IsUninstaller",NULL,NULL,(BYTE*)&mode,&sz);
        RegCloseKey(k);
    }
    if(mode){ do_uninstall(); return 0; }

    INITCOMMONCONTROLSEX icc={sizeof(icc),ICC_PROGRESS_CLASS};
    InitCommonControlsEx(&icc);
    CoInitialize(NULL);

    WNDCLASSEXA wc={sizeof(wc)};
    wc.lpszClassName = "TzLangSetup";
    wc.hInstance     = hi;
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursorA(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.hIcon         = LoadIconA(hi, MAKEINTRESOURCEA(1));
    if(!wc.hIcon) wc.hIcon = LoadIconA(NULL,IDI_APPLICATION);
    RegisterClassExA(&wc);

    int W=680, H=440;
    int x=(GetSystemMetrics(SM_CXSCREEN)-W)/2;
    int y=(GetSystemMetrics(SM_CYSCREEN)-H)/2;
    g_hwnd = CreateWindowExA(0,"TzLangSetup",
        APP_NAME " " APP_VER " SDK Setup",
        WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,
        x,y,W,H,NULL,NULL,hi,NULL);
    ShowWindow(g_hwnd,SW_SHOW);
    UpdateWindow(g_hwnd);

    MSG m;
    while(GetMessageA(&m,NULL,0,0)){ TranslateMessage(&m); DispatchMessageA(&m); }
    CoUninitialize();
    return 0;
}
