cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
vigorvick
Level 4

Invalid handle passed from msi - (always)pls help

Hi,
I have written a sample dll and the code is given below,. It is a win32 dll.

void WINAPI Display(MSIHANDLE hInstall)
{
int i;
TCHAR* szValueBuf = NULL;
DWORD cchValueBuf = 0;
MessageBox(NULL,"creating dll","Success",MB_OK);
TCHAR *szPackagePath="D:\\LM7.0.msi";
i = MsiGetProperty(hInstall, TEXT("INSTALLDIR"),szValueBuf,&cchValueBuf);
if (i==ERROR_SUCCESS)
{
}
else
{
switch (i)
{
case ERROR_INVALID_HANDLE:
MessageBox(NULL,"Invalid handle","failure",MB_OK);
break;
case ERROR_INVALID_PARAMETER MessageBox(NULL,"Invalid parameter","failure",MB_OK);
break;
case ERROR_MORE_DATA:
MessageBox(NULL,"MORE_DATA","failure",MB_OK);
break;
default:
break;
}
}
MSIHANDLE *hProduct=NULL;
i=MsiOpenPackage(szPackagePath,hProduct);
if (i==ERROR_SUCCESS)
{
}
else
{
switch (i)
{
case ERROR_BAD_CONFIGURATION:
MessageBox(NULL,"ERROR_BAD_CONFIGURATION","failure",MB_OK);
break;
case ERROR_INSTALL_FAILURE:
MessageBox(NULL,"ERROR_INSTALL_FAILURE","failure",MB_OK);
break;
case ERROR_INVALID_PARAMETER:
MessageBox(NULL,"ERROR_INVALID_PARAMETER","failure",MB_OK);
break;
default:
break;
}
}
}

When I debug the code after the messagebox appears, I see that the MsiGetProperty returns ERROR_INVALID_HANDLE.

Now when I try to get the handle using MsiOpenPackage, then it returns ERROR_INVALID_PARAMETER, but I can see during run time that all the parameters are proper.

The Installscript code is shown below

MessageBox("Indise myfunction",WARNING);
szDll = INSTALLDIR^"test.dll";
MessageBox(szDll,WARNING);
if (0==UseDLL(szDll)) then
MessageBox("loaded test.dll",WARNING);
g_nRet = test.Display(hMSI);
Sprintf(g_szMsg,"DoInstall returned %d",g_nRet);
else
MessageBox("Unable to load dll",WARNING);
g_nRet = -1;
endif;

P.S: This invalid handle comes only when using IS2008, with IS2.01, the same code works fine.

Could anyone please let me know what may the cause, I'm banging my head with this for the past two days.

Thanks.
Labels (1)
0 Kudos
(3) Replies
joshstechnij
Level 10 Flexeran
Level 10 Flexeran

Loading and calling this DLL from an InstallScript custom action, and then passing the hMSI parameter from your script action to the DLL, will result in an invalid handle error from any MSI functions that take an install handle. The reason this happens is because the handle is maintained by the InstallScript engine and is not usable outside of the InstallScript defined MSI functions (these functions are basically wrapper functions around the real MSI functions; the handle is translated from the InstallScript maintained handle into the real install handle).

You could use an MSI DLL custom action if you wanted to use C/C++ code directly. Windows Installer will then call your DLL passing the install handle. Note that the DLL functions need to return a value to Windows Installer indicating success or error.
0 Kudos
vigorvick
Level 4

Thanks a lot for your reply. It was really helpful to me.
0 Kudos
vigorvick
Level 4

I had passed the ISMSI_HANDLE inside the call to the dll function and it works properly.
0 Kudos