#include <windows.h>
#include <tchar.h>
#include <rapi.h>

#include "..\common.h"

BOOL CALLBACK ModuleDlgProc(HWND hwndDlg, 
                            UINT uMsg, 
                            WPARAM wParam, 
                            LPARAM lParam); 


int WINAPI WinMain (HINSTANCE hInstance,
		            HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int nCmdShow)

{

    RAPIINIT ri = { sizeof(RAPIINIT) };

    if ( SUCCEEDED(CeRapiInitEx(&ri)) )
    {
        // wait for 15 seconds for the connection...
        if ( (WaitForSingleObject(ri.heRapiInit, 15000) == WAIT_OBJECT_0) && 
             SUCCEEDED(ri.hrRapiInit) )
        {
            HRESULT hr;
            DWORD   dwBuf;
            BYTE    *pbuf;


            hr = CeRapiInvoke(_T("cridll.dll"), 
                              _T("GetLoadedModules"), 
                              0,
                              NULL,
                              &dwBuf,
                              &pbuf,
                              NULL,
                              0);

            if ( CRI_Success(hr) )
            {
                if ( pbuf == NULL )
                {
                    MessageBox(NULL, _T("Loaded Module List"), _T("No loaded modules found on device"), MB_OK);
                }
                else
                {
                    // display the dialog and pass the pointer to the module list
                    DialogBoxParam(hInstance,
                                    MAKEINTRESOURCE(IDD_BLOCKDLG), 
                                    NULL, 
                                    (DLGPROC)ModuleDlgProc,
                                    (LPARAM)pbuf);
                }

                // free the memory allocated by the call to CeRapiInvoke
                CeRapiFreeBuffer(pbuf);
            }
            else
            {

                MessageBox(NULL, L"CeRapiInvoke failed", L"CeRapiInvoke failed", MB_OK);
            }
        }
        else
        {
            MessageBox(NULL, _T("CeRapiInitEx failed"), _T("CeRapiInitEx failed"), MB_OK);
        }

        // have to call uninit even if the connection did not succeed
        CeRapiUninit();
    }
    else
    {
        MessageBox(NULL, _T("CeRapiInitEx failed"), _T("CeRapiInitEx failed"), MB_OK);
    }

    return 0;


    if ( CeRapiInit() != E_FAIL )
    {
    }
    else
    {
        MessageBox(NULL, L"CeRapiInit failed", L"CeRapiInit failed", MB_OK);
    }

    return 0;
}


// window function for the dialog box

BOOL CALLBACK ModuleDlgProc(HWND hwndDlg, 
                      UINT uMsg, 
                      WPARAM wParam, 
                      LPARAM lParam)
{
    BOOL    bRet = FALSE;


    switch( uMsg )
    {
        case WM_INITDIALOG:
        {
            LPWSTR lpwszMods = (LPWSTR)lParam;
            
            // parse the modules from the passed list and display 
            // in the listbox
            LPWSTR lpwsz = _tcstok(lpwszMods, _T("\n"));
            while (lpwsz)
            {
                SendMessage(GetDlgItem(hwndDlg, IDC_MODLIST), LB_ADDSTRING, 0, (LPARAM)lpwsz);
                lpwsz = _tcstok(NULL, _T("\n"));
            }
        }
        break;

        case WM_COMMAND:
        {
            int     nID = LOWORD(wParam);
            int     wNotify = HIWORD(wParam);

            // handle click on OK or Cancel
            if ( wNotify == BN_CLICKED )
            {
              switch( nID )
              {
                case IDCANCEL:
                case IDOK:
                    EndDialog(hwndDlg, nID);
                    bRet = TRUE;
                    break;
              }
            }
        }
        break;
    }

    return bRet;
}



