Tuesday, September 28, 2010

Check if a windows process is running...

Hello Friends,
     During one of my project a requirement comes across to know whether a process is currently running on your machine or not. I looked across a number of sites, but none proved to be useful to me. But in the last I got some useful scripts which proved to be very efficient when applied through installscipt code.

The below script is in vb, which you could use through vbscript type custom action in your installshield project. In th variable PROCESS_NAME, you need to replace it by your process name, which you are looking for.

set service = GetObject ("winmgmts:")
for each Process in Service.InstancesOf ("Win32_Process")
      If Process.Name = "PROCESS_NAME" then
          wscript.echo "mmc running"
          wscript.quit             -- Here script will terminate
      End If
next

wscript.echo "mmc not running"         -- This code will execute if process is not running

Tuesday, September 21, 2010

Check if database exist...

Friends, today I have share one of my useful database script which I had implemented in Installshield code to check the existence of a database.

Often we come across scenario, where we either need to create a new database if it does not exist, or to update an existing one. Below script consist of a function named DoesDatabaseExist, which will make a call to the database using all the credentials and database name. I bet, you will find it very useful.

///////////////////////////////////////////////////////////////////////////////
//
// Function: DoesDatabaseExist
//
// Purpose: This function will determine whether a given database exists.
//
// Arguments: svServerName - The name of the SQL server to run the script on
// svDatabaseName - The name of the SQL database to run the script on
// svDriver - ADO requires this, but for SQL server you just send in "SQL Server"
// svUserName - The system account for SQL server
// svUserPassword - The password for the system account
//
// Usage:
// if (DoesDatabaseExist("SQLServer", "MyDatabase", "SQL Server", "sa", "saPassword") = FALSE) then
//
///////////////////////////////////////////////////////////////////////////////
function BOOL DoesDatabaseExist(svServerName, svDatabaseName, svDriver, svUserName, svUserPassword)
OBJECT pADOConnObj, pADORecordSetObj;
STRING szADOConnObjID, szADORecordSetObjID, szConnString, szSQL;
BOOL bExists;
begin
try
bExists = FALSE;

// Create ADO Connection Object to connect to the SQL server
szADOConnObjID = "ADODB.Connection";
set pADOConnObj = CreateObject(szADOConnObjID);

// Create the SQL string to complete the connection
svDriver = "SQL Server";

if(gbWinAuthLogin == FALSE) then
szConnString = "driver={" + svDriver + "};";
szConnString = szConnString + "server=" + svServerName + ";";
szConnString = szConnString + "uid=" + svUserName + ";";
szConnString = szConnString + "pwd=" + svUserPassword + ";";
szConnString = szConnString + "database=master";
else
szConnString = "driver={" + svDriver + "};";
szConnString = szConnString + "server=" + svServerName + ";";
szConnString = szConnString + "database=master;";
szConnString = szConnString + "Trusted_connection=Yes";
//ConnectionString = "Data Source="+gsDBServer+";Initial Catalog="+gsDBCatalog+"; Integrated Security=True;";

endif;
// Open the ADO Connection
pADOConnObj.Open(szConnString);

// Create ADO Recordset object for the return
szADORecordSetObjID = "ADODB.Recordset";
set pADORecordSetObj = CreateObject(szADORecordSetObjID);

// Set some ADO Recordset properties
pADORecordSetObj.CursorType = 3;
pADORecordSetObj.ActiveConnection = pADOConnObj;

// Create the SQL string to retrieve the database if it exists
szSQL = "Select name from sysdatabases where name='" + svDatabaseName + "'";

// Use the recordset to see if the database exists
pADORecordSetObj.Open(szSQL);
if (pADORecordSetObj.RecordCount = 1) then
bExists = TRUE;
endif;

return bExists;
catch
MessageBoxEx(Err.Description + ":Error occured","",SEVERE);
endcatch;
end;

Tuesday, September 14, 2010

Creating an Uninstall Shortcut for an InstallScript MSI Project

Hi All, Today I need to share how we can create shortcuts in our installshield projects for uninstalling existing installed product. When we use the below information, we were able to install a shortcut along with our application. This shortcut will trigger the uninstallation process.

A Basic MSI installation package can be uninstalled using this command line:
[SystemFolder]Msiexec.exe /x{PRODUCT_CODE}

However, using the same command line for an InstallScript MSI will not uninstall some registry entries, nor would the entry in the Add/Remove Programs applet be removed. This article explains the proper command line to execute the uninstall for an InstallScript MSI project via a shortcut.



Discussion:
InstallShield 12 and newer
Uninstall shortcuts for InstallScript MSI projects are launched through a cached version of the setup.exe. This file is cached in the InstallShield Installation Information folder when the installation has completed. Below are the steps to add a shortcut to launch the cached setup.exe for an uninstall shortcut:


1. Browse to the Shortcuts view or the Shortcuts view of a component.
2. Right-click the desired folder location for the shortcut and select New Shortcut to Preexisting File.
3. In the Browse for Shortcut Target dialog, select the [ProgramFilesFolder] folder. Click OK. Name the shortcut created.
4. Fill in the Target field for the new shortcut as follows:
[ProgramFilesFolder]InstallShield Installation Information\[ProductCode]\setup.exe
Note: Quotes are not required for the shortcut target, they will automatically be placed around the filename and path by Windows Installer at runtime. [ProductCode] will also be replaced at runtime with the Product Code of the MSI package being installed.
5. Fill in the Arguments field for the shortcut as follows:
/runfromtemp /x

For InstallShield 11.5 and earlier
When adding an uninstallation shortcut for an InstallScript MSI project, the file that would be launched is IDriver.exe rather than Msiexec.exe. IDriver.exe is installed automatically, and there is no need to manually install this file. Below are the steps to add a shortcut to launch IDriver and uninstall the InstallScript MSI project:


1. Browse to the Shortcuts view or the Shortcuts view of a component.
2. Right-click and select New Shortcut to Preexisting File.
3. In the Browse for Shortcut Target dialog, the directories that will point to 
    Idriver.exe should be made. The subfolders InstallShield\Driver\ x \Intel 32
    should be added under the [CommonFilesFolder]. The x is where the
    appropriate version of IDriver.exe should be indicated; that being a 7 or 8
    for Developer 7 or 8 projects respectively, or 9 or 10 for DevStudio 9 or X
    projects respectively.

4. Fill in the Arguments field as follows:
    
Arguments: /M{Product Code}
    
{Product Code} would be replaced with your actual product code. It might
    look similar to the following:

    Arguments: /M{7C071035-F334-11D5-818A-00C04F288311}


To find the Product Code for a particular project, follow these steps:
1. Open project in the InstallShield IDE.
2. Select the General Information view.
3. Select Product Properties.
4. Note the value of the Product Code field in the right panel