PHP Code:
// testx.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "testx.h"
#include <stdio.h>
#include <winreg.h>
// Global Variables:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CenterWindow(HWND hwnd)
{
RECT rc;
GetWindowRect(hwnd,&rc);
int nWidth = rc.right - rc.left;
int nHeight = rc.bottom - rc.top;
int X = ((int) GetSystemMetrics(SM_CXFULLSCREEN) - nWidth) /2; // center window horizontally
int Y = ((int) GetSystemMetrics(SM_CYFULLSCREEN) - nHeight) /2; // and vertically
MoveWindow(hwnd,X,Y,nWidth,nHeight,TRUE);
}
bool EnumInstalledSoftware(void)
{
HKEY hUninstKey = NULL;
HKEY hAppKey = NULL;
WCHAR sAppKeyName[1024];
WCHAR sSubKey[1024];
WCHAR sDisplayName[1024];
WCHAR *sRoot = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
long lResult = ERROR_SUCCESS;
DWORD dwType = KEY_ALL_ACCESS;
DWORD dwBufferSize = 0;
//Open the "Uninstall" key.
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)sRoot, 0, KEY_READ, &hUninstKey) != ERROR_SUCCESS)
{
return false;
}
for(DWORD dwIndex = 0; lResult == ERROR_SUCCESS; dwIndex++)
{
//Enumerate all sub keys...
dwBufferSize = sizeof(sAppKeyName);
if((lResult = RegEnumKeyEx(hUninstKey, dwIndex, (LPSTR)sAppKeyName, &dwBufferSize, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
{
//Open the sub key.
wsprintf((LPSTR)sSubKey, "%s\\%s", sRoot, sAppKeyName);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)sSubKey, 0, KEY_READ, &hAppKey) != ERROR_SUCCESS)
{
RegCloseKey(hAppKey);
RegCloseKey(hUninstKey);
return false;
}
//Get the display name value from the application's sub key.
dwBufferSize = sizeof(sDisplayName);
if(RegQueryValueEx(hAppKey, "DisplayName", NULL, &dwType, (unsigned char*)sDisplayName, &dwBufferSize) == ERROR_SUCCESS)
{
wsprintf("%s\n", (LPCSTR)sDisplayName);
}
else
{
//Display name value doe not exist, this application was probably uninstalled.
}
RegCloseKey(hAppKey);
}
}
RegCloseKey(hUninstKey);
return true;
}
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
char szBuffer[1024];
sprintf_s(szBuffer, "%s\n", EnumInstalledSoftware());
//This checks if the process is already running so you don't have multiple instances running.
HANDLE handle = CreateMutex(NULL, true, "testx");
if(GetLastError() != ERROR_SUCCESS)
{
MessageBox(0, "Process is already running", "Warning", MB_ICONWARNING);
return false;
}
char szWindowClass[] = "test_WindowClass";
MSG msg;
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
wcex.lpszClassName = szWindowClass;
RegisterClassEx( &wcex );
HWND hWnd = CreateWindowEx( 0, szWindowClass, "Test", WS_OVERLAPPED | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL );
HWND hwndLabel = CreateWindow(
TEXT("STATIC"),
szBuffer,//TEXT("Label 1"),
WS_CHILD | WS_VISIBLE | SS_LEFT, /*Styles (continued)*/
0, /*X co-ordinates*/
0, /*Y co-ordinates*/
1024, /*Width*/
768, /*Height*/
hWnd, (HMENU) ID_MYSTATIC, hInstance, NULL);
if(!hWnd)
{
return FALSE;
}
CenterWindow(hWnd);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
}
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
}
case WM_KEYDOWN:
{
if (wParam == VK_ESCAPE)
{
int ret = MessageBox(NULL, TEXT("Are you sure to quit?"), TEXT("Message"), MB_OKCANCEL);
if ( ret == IDOK)
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}