Results 1 to 2 of 2

Thread: Enumerating programs installed

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default Enumerating programs installed

    I'm looking into writing a Win32 API app that lists programs that are installed on a local machine. I have found that a method of doing this would be going through the registry and seeing the uninstall strings to see what programs are on the machine.

    Now the issue, found a piece of code that works fine if I compile in a console app, but for some reason when I put it into my win32 api project, it prints NULL and I'm not sure why.

    Any ideas?

    Here is the source

    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(HWNDUINTWPARAMLPARAM);

    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)sRoot0KEY_READ, &hUninstKey) != ERROR_SUCCESS)
        {
            return 
    false;
        }

        for(
    DWORD dwIndex 0lResult == ERROR_SUCCESSdwIndex++)
        {
            
    //Enumerate all sub keys...
            
    dwBufferSize sizeof(sAppKeyName);
            if((
    lResult RegEnumKeyEx(hUninstKeydwIndex, (LPSTR)sAppKeyName, &dwBufferSizeNULLNULLNULLNULL)) == ERROR_SUCCESS)
            {
                
    //Open the sub key.
                
    wsprintf((LPSTR)sSubKey"%s\\%s"sRootsAppKeyName);
                if(
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCSTR)sSubKey0KEY_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 WinMainHINSTANCE hInstanceHINSTANCE hPrevInstanceLPSTR lpCmdLineint 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(NULLtrue"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        LoadCursorNULLIDC_ARROW );
        
    wcex.hbrBackground    CreateSolidBrush(RGB(255255255)); 
        
    wcex.lpszClassName  szWindowClass;
        
    RegisterClassEx( &wcex );

        
    HWND hWnd CreateWindowEx0szWindowClass"Test"WS_OVERLAPPED WS_CLIPCHILDREN WS_CAPTION WS_SYSMENUCW_USEDEFAULT0CW_USEDEFAULT0NULLNULLhInstanceNULL );
        
    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, (HMENUID_MYSTATIChInstanceNULL);

        if(!
    hWnd)
        {
            return 
    FALSE;
        }

        
    CenterWindow(hWnd);
        
    ShowWindow(hWndnCmdShow);
        
    UpdateWindow(hWnd);

        while(
    GetMessage(&msgNULL00))
        {
            
    TranslateMessage( &msg );
            
    DispatchMessage( &msg );
        }

        return (int) 
    msg.wParam;
    }

    LRESULT CALLBACK WndProc(HWND hWndUINT messageWPARAM wParamLPARAM lParam)
    {
        
    int wmIdwmEvent;
        
    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(hWndmessagewParamlParam);
                }

                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(NULLTEXT("Are you sure to quit?"), TEXT("Message"), MB_OKCANCEL);
                     
                     if ( 
    ret == IDOK
                     {
                         
    SendMessage(hWndWM_CLOSE00);
                     }
                 }
                 break;
            }

            case 
    WM_DESTROY:
                
    PostQuitMessage(0);
            break;

            default:
                return 
    DefWindowProc(hWndmessagewParamlParam);
        }
        return 
    0;


  2. #2
    Banned
    Join Date
    May 2010
    Location
    fuck off?
    Posts
    1,145

    Default

    then its probably a problem with the printing and format, how youre pritning it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •