Jump to content


This is a ready-only archive of the InstallSite Forum. You cannot post any new content here. / Dies ist ein Archiv des InstallSite Forums. Hier können keine neuen Beiträge veröffentlicht werden.
Photo

Populate Listbox with SQLServers using C++ CA


2 replies to this topic

SPV

SPV
  • Full Members
  • 5 posts

Posted 08 May 2007 - 12:59

I have written the following function to retrive list of all SQL Servers found on the n/w to be populated in a listbox appearing in Basic MSI project. The CA is executed in immediate execution in UI mode. The problem with this code is that its not able to get the active handle returned by MsiGetActiveDatabase which is suppose to return a Read Only handle.(But i have to update the msi in MSIMODIFY_INSERT_TEMPORARY mode which is possible i suppose). I'm not able to locate the problem with this code. Can anybody check this code and suggest. Thanks In Advance!!!

SQLServerList(MSIHANDLE hInstall)
{
CLog::WriteInstallLog(_T("Enter SQLServerList"));
DWORD dwRc = ERROR_SUCCESS;
MSIHANDLE hViewList;
PMSIHANDLE hRecList;
LPWSTR szValue;
DWORD nBuffer;
int r;

try
{
_SQLServerPtr spSQLServer;

HRESULT hr2 = spSQLServer.CreateInstance(__uuidof(SQLServer));
if (FAILED(hr2))
_com_issue_error(hr2);

if (SUCCEEDED(hr2))
{
try
{
// Get the Application Ptr
long lServerCount(0);
_ApplicationPtr pApplication = spSQLServer->GetApplication();

if (pApplication)
{
NameListPtr pServerNameList = pApplication->ListAvailableSQLServers();
if (pServerNameList)
{
HRESULT hr = pServerNameList->get_Count(&lServerCount);

BSTR bstrServerName;
// m_ctlComboBox.ResetContent();
r=0;
_variant_t vIndex(long(0));
for (long i = 0; i < lServerCount; i++)
{
vIndex.lVal = i;
hr = pServerNameList->raw_Item(vIndex, &bstrServerName);
_bstr_t bstrValue(bstrServerName);
CString sValue((LPCSTR)bstrValue);
if (!sValue.IsEmpty())
{
// open and execute a view to the ListBox table
MSIHANDLE hDatabase = MsiGetActiveDatabase(hInstall);
DWORD viewlist = MsiDatabaseOpenView(hDatabase, _T("SELECT * FROM `ListBox` WHERE `Property`='IS_SQLSERVER_LIST'"), &hViewList);
if (viewlist == ERROR_SUCCESS)
CLog::WriteInstallLog(_T("MsiDatabaseOpenView returned ERROR_SUCCESS"));
else if (viewlist == ERROR_BAD_QUERY_SYNTAX)
{
CLog::WriteInstallLog(_T("MsiDatabaseOpenView returned ERROR_BAD_QUERY_SYNTAX"));
return !dwRc;
}
else if (viewlist == ERROR_INVALID_HANDLE)
{
CLog::WriteInstallLog(_T("MsiDatabaseOpenView returned ERROR_INVALID_HANDLE"));
return !dwRc;
}
if (ERROR_SUCCESS==MsiViewExecute(hViewList, 0))
CLog::WriteInstallLog(_T("MsiViewExecute executed successfully"));
else
{
CLog::WriteInstallLog(_T("MsiViewExecute execution failure"));
return !dwRc;
}
hRecList = MsiCreateRecord(4);
r = r + 1;
MsiRecordSetString(hRecList, 1, _T("IS_SQLSERVER_LIST")); // Column1: Property tied to the entry
MsiRecordSetInteger(hRecList, 2, r); // Column2: Display order of the item
MsiRecordSetString(hRecList, 3, (LPCTSTR)sValue); //Column3: Value to set property to
MsiRecordSetString(hRecList, 4, (LPCTSTR)sValue); //Column4: Display text for item
CLog::WriteInstallLog(_T("Server found %s"),(LPCTSTR)sValue);
if (ERROR_SUCCESS==MsiViewModify(hViewList, MSIMODIFY_INSERT_TEMPORARY, hRecList))
CLog::WriteInstallLog(_T("MsiViewModify executed successfully"));
else
{
CLog::WriteInstallLog(_T("MsiViewModify execution failure"));
return !dwRc;
}
if(hViewList)
MsiViewClose(hViewList);
}
}
}
}
pApplication = NULL;
spSQLServer.Release();
}
catch (_com_error e)
{
CLog::WriteInstallLogMsgBx(MSG_SQL_SERVERLIST_NA, MB_OK);
spSQLServer.Release(); // Free the interface.
}
}
else
{
CLog::WriteInstallLogMsgBx(MSG_SQL_SERVER_OBJECT_CREATEFAILED, MB_OK);

}
}
catch (_com_error e)
{
CLog::WriteInstallLog(_T("error returned=%d"), e);
}

CLog::WriteInstallLog(_T("Exit SQLServerList"));
return dwRc;
}

------------------------------------------
Regards
SPV
---------------------------------------
Regards,
SPV.
"A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort"

Zweitze

Zweitze
  • Full Members
  • 522 posts

Posted 08 May 2007 - 13:57

Look at this line:

DWORD viewlist = MsiDatabaseOpenView(... , &hViewList);

MsiDatabaseOpenView returns the view handle in the last parameter, and returns an error/success code too.
You should use different variables here, since these results have different meanings. Eg.

MSIHANDLE hView;
HRESULT ResultCode = MsiDatabaseOpenView(... , &hView);



SPV

SPV
  • Full Members
  • 5 posts

Posted 10 May 2007 - 07:36

Thanks Zweitze...You are right on that one...since c++ code is case-sensitive thats why it runs without compilation errors...

Earlier i was trying to execute the methods in following sequence
MsiGetActiveDatabase
MsiDatabaseOpenView
MsiViewExecute
MsiCreateRecord
MsiViewModify

Finally I have got the answer for this one..
The proper sequence of methods to be executed are
MsiGetActiveDatabase
MsiDatabaseOpenView
MsiCreateRecord
MsiViewModify
MsiViewExecute
So once the view is modified it should be updated and refreshed....
Given bellow is the code that worked for me....
if (!sValue.IsEmpty())
{
hRecList = MsiCreateRecord(4);
if (!hRecList)
return !dwRc;
else
CLog::WriteInstallLog(_T("MsiCreateRecord executed successfully"));
r = r + 1;
MsiRecordSetString(hRecList, 1, _T("IS_SQLSERVER_LIST")); // Column1: Property tied to the entry
MsiRecordSetInteger(hRecList, 2, r); // Column2: Display order of the item
MsiRecordSetString(hRecList, 3, (LPCTSTR)sValue); //Column3: Value to set property to
MsiRecordSetString(hRecList, 4, (LPCTSTR)sValue); //Column4: Display text for item
CLog::WriteInstallLog(_T("Server found %s"),(LPCTSTR)sValue);
if (ERROR_SUCCESS==MsiViewModify(hViewList, MSIMODIFY_INSERT_TEMPORARY, hRecList))
{
CLog::WriteInstallLog(_T("MsiViewModify executed successfully"));
if (ERROR_SUCCESS==MsiViewExecute(hViewList, 0))
CLog::WriteInstallLog(_T("MsiViewExecute executed successfully"));
else
{
CLog::WriteInstallLog(_T("MsiViewExecute execution failure"));
return !dwRc;
}
}
else
{
CLog::WriteInstallLog(_T("MsiViewModify execution failure"));
return !dwRc;
}
}

Edited by SPV, 10 May 2007 - 07:37.

---------------------------------------
Regards,
SPV.
"A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort"