Creating modal dialog in vc++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhanashivam
    New Member
    • Mar 2007
    • 31

    Creating modal dialog in vc++

    Hi All,

    I have developed a VC+ dll file which provides a product key for my msi setup file. This VC++ dll work fine together with my setup file. but,

    my problem is, i have given a message box inside the dll for showing error message while an invalid key is entered. but the messagebox is not shown as modal dialog. i must want to show this as modal dialog.

    in my VC++ method i have passed the MSIHANDLE from the msi file.

    how can i change the message as modal dialog?

    my code is:

    #include "windows.h"
    #include "msi.h"
    #include "msiquery.h "

    extern "C" _declspec(dllex port) UINT __stdcall VerifyPID(MSIHA NDLE hInstall);

    TCHAR* GetPIDValue(TCH AR*);

    extern "C" UINT __stdcall VerifyPID(MSIHA NDLE hInstall)
    {
    UINT nRetVal = 0;
    UINT uiMsiRc;
    TCHAR szPidKey[MAX_PATH];
    TCHAR szSourceDir[MAX_PATH];
    TCHAR* lpszPidValue;
    DWORD dwBuffer;

    dwBuffer = sizeof(szSource Dir)/sizeof(TCHAR);

    uiMsiRc = MsiGetProperty( hInstall, TEXT("SourceDir "), szSourceDir, &dwBuffer);

    if (ERROR_SUCCESS != uiMsiRc)
    {
    MessageBox(NULL , "Not able to retrieve the SourceDir property. The setup may be corrupt. Please contact Technical Support.", "Setup Error", MB_OK | MB_ICONEXCLAMAT ION);
    return 0;
    }

    lpszPidValue = GetPIDValue(szS ourceDir);

    dwBuffer = sizeof(szPidKey )/sizeof(TCHAR);

    uiMsiRc = MsiGetProperty( hInstall, TEXT("PIDKEY"), szPidKey, &dwBuffer);

    if (ERROR_SUCCESS != uiMsiRc)
    {
    MessageBox(NULL , "Not able to retrieve PIDKEY property. The setup may be corrupt. Please contact Technical Support.", "Setup Error", MB_OK | MB_ICONEXCLAMAT ION);
    return 0;
    }

    int str = lstrcmp(szPidKe y, lpszPidValue);

    if (str == 0)
    MsiSetProperty( hInstall, "PIDCHECK", "TRUE");
    else
    {
    MsiSetProperty( hInstall, "PIDCHECK", "FALSE");
    MessageBox(NULL ,"Please enter the correct product registration code!", "Invalid Key", MB_OK | MB_ICONINFORMAT ION);
    }
    return 0;
    }

    TCHAR* GetPIDValue(TCH AR* lpszSourceDir)
    {
    return "123-456-789";
    }

    Please help to resolve it.

    Thanks in advance.

    Regards,
    Dhanasekaran. G
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your code works for me after I changed it this way:
    [code=cpp]
    void __stdcall DisplayFromDll( )
    {
    MessageBox(NULL , TEXT("Not able to retrieve the SourceDir property. The setup may be corrupt. Please contact Technical Support."), TEXT("Setup Error"), MB_OK | MB_ICONEXCLAMAT ION);

    }
    [/code]

    Notice I used the TEXT macro. Otherwise, your code won't compile.

    Next, be sure your DLL project has a DEF file that exports the C++ name as a name you can use with GetProcAddress:
    [code=cpp]
    ;BEGIN ADLL.DEF FILE
    ;This DEF file is required becuase the argument to GetProcAddress( )
    ;for the function is a C-string and it will never be equal to the
    ;C++ mangled name for the function
    ;This DEF file maps the mangled name to a name that can be used with GetProcAddress( )
    ;Note also: Change project settings in Visual Studio to send the LINK this def file.
    ;Visual Studio.NET: Project Properties/Linker/Input/Module Definition File/...Path to the def file\Adll.def
    LIBRARY ADll
    EXPORTS
    ;Exported Name C++ Mangled Name
    DisplayFromDll = ?DisplayFromDll @@YGXXZ
    ;END DEF FILE
    [/code]

    Next, be certain to set the path to the DEF file in the linker input project property.

    Finally, I called the funciton:
    [code=cpp]
    //First, load the dll into memory
    HMODULE theDll = LoadLibrary(TEX T("C:\\Scratch\ \Instructor\\De bug\\DLLExample .dll"));
    if (!theDll)
    {
    MessageBox(NULL , TEXT("Dll Failed to Load"), TEXT("Fatal Error"), MB_OK | MB_ICONEXCLAMAT ION);
    return 1;
    }

    //Second, get the address of the desried function from the dll
    FARPROC addr = GetProcAddress( theDll, "DisplayFromDll ");
    if (!addr)
    {

    //Look up the error in the system errors list
    unsigned int what = GetLastError();
    if (what == ERROR_PROC_NOT_ FOUND)
    {
    MessageBox(NULL , TEXT("Function not found in the dll"), TEXT("Fatal Error"), MB_OK | MB_ICONEXCLAMAT ION);

    }
    return 2;
    }
    cout << "The function has been located in the dll" << endl;
    //Declare a function pointer that can accept the address of the function.
    //You will need to know the function prototype to do this.
    //Dll function prototypes should be provided by the vendor of the dll
    void (__stdcall *DisplayFromDll )();
    //Type-cast the address returned from GetProcAddress to the function pointer type
    DisplayFromDll = reinterpret_cas t<void (__stdcall *)()> (addr);
    //Now use the function pointer to call the function:
    DisplayFromDll( );
    [/code]

    Comment

    Working...