Google Search [MDB-Blog]

Loading...

Wednesday, February 13, 2013

c# Winforms WebBrowser - Clear all cookies

Hello,
I recently search for a method to delete all cookies from the build in .NET WinForms WebBrowser control.
I didn't found any working solution for it, nor working example.
It being told to use InternetSetOption, but nothing found about it.
So, i will write here my solution for clearing and deleting all cookies.
My solution using InternetSetOption with the option flag: INTERNET_OPTION_SUPPRESS_BEHAVIOR, which described as:

A general purpose option that is used to suppress behaviors on a process-wide basis. The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. This option cannot be queried with InternetQueryOption.

This option flag should be used together with INTERNET_SUPPRESS_COOKIE_PERSIST options, which means:

Suppresses the persistence of cookies, even if the server has specified them as persistent.


So the example code for it will be:
static void Main()
{
    SuppressWininetBehavior();

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

[System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);

private static unsafe void SuppressWininetBehavior()
{
    /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
        * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
        *      A general purpose option that is used to suppress behaviors on a process-wide basis. 
        *      The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. 
        *      This option cannot be queried with InternetQueryOption. 
        *      
        * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
        *      Suppresses the persistence of cookies, even if the server has specified them as persistent.
        *      Version:  Requires Internet Explorer 8.0 or later.
        */

    int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
    int* optionPtr = &option;

    bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
    if (!success)
    {
        MessageBox.Show("Something went wrong !>?");
    }
}

Please make sure your project is allows unsafe code. (under Properties => Build Tab)

This code is deleting the COOKIES per PROCESS on startup ONLY.
[tested on WIN-7 and working great]


Best Regards,
MDB-BLOG :)

Sunday, February 10, 2013

MySQL xpath ExtractValue with delimiter

Hello,
I recently noticed the XPATH option inside MySQL.
What a wonderfull way for handling XML. (http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html)
I used ExtractFunction, and noticed that this function is space delimited:
Because ExtractValue() returns multiple matches as a single space-delimited string

So I needed to create the same function with custom text delimted (comma, semi-colon or anything you want).

The function called: EXTRACTVALUE_ALL which is the same as EXTRACTVALUE just get a delimiter text to seperate between results. (seperated text).
Tested on MySQL 5.5.28.

 
Create Function CODE:
DROP FUNCTION IF EXISTS EXTRACTVALUE_ALL;

DELIMITER |
CREATE FUNCTION EXTRACTVALUE_ALL(p_xml TEXT, p_xpathExpr TEXT, p_delimiter TEXT) RETURNS TEXT
BEGIN
DECLARE total_elements INT;
DECLARE xpath_expression_count, xpath_expression_index  TEXT;
DECLARE single_tag, result  TEXT;

# calc TOTAL elements
SET xpath_expression_count = CONCAT('count(', p_xpathExpr, ')');
SELECT EXTRACTVALUE(p_xml, xpath_expression_count) INTO total_elements;

# run over elements (create long text)
SET result = '';
SET xpath_expression_index = CONCAT(p_xpathExpr, '[$@i]');
SET @i = 1;
WHILE @i <= total_elements DO
SET single_tag = EXTRACTVALUE(p_xml, xpath_expression_index);
SET result = IF(result='', single_tag, CONCAT(result, p_delimiter, single_tag));
SET @i = @i + 1;
END WHILE;

# return total result
RETURN result;
END |


Usage Code:
SET @XXXMMMMLLL = '      ';

SELECT EXTRACTVALUE_ALL(@XXXMMMMLLL, '/descendant-or-self::time', '  |  ');


Output:
time1  |  time2


Rememeber that you will need a MYSQL permission for creating new functions.

MDB-BLOG :)

 

Wednesday, January 2, 2013

NSIS - Launch a program as user from UAC elevated installer

NSIS - Launch a program as user from UAC elevated installer
==========================================

I noticed that if a program is running as UAC elevated (admin or high user privileges), 
any opening process by this process will get the same privileges as the executing program,
which means, that any process opened by this UAC elevated program will be elevated also.

I found a solution for it, for opening the process UN-ELEVATED from ELEVATED running program.
I show this information as for NSIS installer, but can be used in ANY development environment (C#, NSIS, C++, JAVA, VB, and any).

The idea is to run the process in UN-ELEVATED mode, using windows's file explorer process `explorer.exe` (info).
Lets say the process that we want to launch is on `$TEMP\MyUnElevatedProcess.exe`.
So, for NSIS code, I will just write:

Exec '"$WINDIR\explorer.exe" "$TEMP\MyUnElevatedProcess.exe"'

And this will do the work...
The process `MyUnElevatedProcess.exe` will run with same ELEVATION that have your windows login, as have `$WINDIR\explorer.exe`.


Execute with parameters:
In addition, if the UN-ELEVATED process need to executed with parameters, you will need to create another file that executes the UN-ELEVATED process (for example a BATCH file which just run the process with the command line parameters).
a good example can be:
; assuming that the file `MyUnElevatedProcess.exe` exists on `$TEMP\`

; create shortcut with ARGUMENTS
CreateShortCut "$TEMP\Shortcut.lnk" "$TEMP\MyUnElevatedProcess.exe" "/arg1 /arg2 /arg3"

; execute the file NON elevated
Exec '"$WINDIR\explorer.exe" "$TEMP\Shortcut.lnk"'


Remember,
if your main program (the executing), is not ELEVATED, this logic is not relevant, because then you can just run `Exec` (open-process function in NSIS) which will have the same elevation as your process.

I hope it helps,
MDB-BLOG

Sunday, May 22, 2011

c# WinForm | Check form window size and location

Hello,


I wanted to open a new window form, exactly near another form. (pinning one form to another).
Because of the border difference among Windows XP, Vista and Windows7 (win7), it didn't work easily.

So to determine windows size under win7/vista, I had to check if Windows Aero. (checking made by short&easy code)

Now, after checking this, I had to also support winXP (which does not have AERO technology).

For finding the left and right window border size (width) I used the SystemInformation Class which provides information about the current system environment.

Note:
I recommend to check if it works for other FormBorderStyle.

The code:
internal static bool IsAeroEnabled()
{
    bool isEnabled = false;
    if (Environment.OSVersion.Version.Major >= 6)
    {
        try
        {
            DwmIsCompositionEnabled(out isEnabled);
        }
        catch (Exception ex)
        {
            Logger.WriteLog(ex);
        }
    }
    return isEnabled;
}
[DllImport("dwmapi.dll")]
internal static extern int DwmIsCompositionEnabled(out bool enabled);

Best regard,
MDB Blog

Monday, May 2, 2011

Check HOSTS file - C# WinForms

Hello,

In today's post i'm giving a good (and simple) anti-hacking tool which allow your application to CHECK for any changes in the Windows HOSTS file.

So, the operation is simple:
Just read the hosts file content, and validate that your site DNS is not included in the list.
[basiclly, check if hosts file contains your dns name]
if so, the user probably is trying to bypass your server so you can stop him from doing so.

The code:
static bool IsDnsByPassed(string p_dnsName)
{
    string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts");
    string hostsText = File.ReadAllText(path);
    return hostsText.ToLower().Contains(p_dnsName.ToLower());
}

Note:
Although reading the HOSTS file is allowed to all user, do not try to edit the HOSTS file, because on Vista & Windows7 this operation is allowed only for administrators. if your application is NOT running as Admin, then you won't have permission for this (and an Exception will be thrown).

Best regard,
MDB-Blog

Thursday, February 10, 2011

MSI | Embed images inside MSI installer file

In this post I show how to embed images inside MSI installer file.

The first try i made was to try adding splash image and banners to the installer through Visual-Studio User-Interface. (just added two jpg files into "Common Files Folder", and set the SplashBitmap and BannerBitmap attributes). But doing so, created a problem - when changing the image, new version installer did NOT changed the images (or the images disappeared).
So i needed another solution...

The next solution is just to INSERT those images (banner&splash) directly into the MSI file created by Visual-Studio.
I created file run.js file, which automaticlly runs when the build process finished, and edits the MSI. This is achieved by setting the PostBuildEvent on the Windows Installer project.

The event that added is to the PostBuildEvent: (remember to copy the file to the main folder of the Windows Installer project)
cscript.exe "$(ProjectDir)run.js" "$(BuiltOuputPath)"

In this way, you don't have to add any image file to your project, and it s embedded in the msi file (which u can edit any time).

The run.js file:
// run.js // Performs a post-build fixup of an msi

if (WScript.Arguments.Length != 1)
{
       WScript.StdErr.WriteLine(WScript.ScriptName + " file");
       WScript.Quit(1);
}

var filespec = WScript.Arguments(0);
var installer = WScript.CreateObject("WindowsInstaller.Installer");
var database = installer.OpenDatabase(filespec, 1/*msiOpenDatabaseModeTransact*/);

var sql
var view
var record

try {

    WScript.Echo("UPDATE default banner image");

    sql = "UPDATE `Binary` SET `Data` = ? WHERE `Name`= 'DefBannerBitmap'";
    view = database.OpenView(sql)
    record = installer.CreateRecord(1);
    record.SetStream(1, "C:\\installer_banner_img.jpg");
    view.Execute(record);
    view.Close();
       WScript.Echo("ADD splash image");
    sql = "INSERT INTO `Binary` (`Name`, `Data`) VALUES ('DefSplashBitmap', ?)";
    view = database.OpenView(sql)
    record = installer.CreateRecord(1);
    record.SetStream(1, "C:\\Installer_splash_img.jpg");
    view.Execute(record);
    view.Close();

    WScript.Echo("Update splash control");
    sql = "UPDATE `Control` SET `Text`='DefSplashBitmap' WHERE `Dialog_`='SplashForm' AND `Control`='SplashBmp'";
    view = database.OpenView(sql)
    view.Execute();
    view.Close();
       database.Commit();
}
catch (e) {
    WScript.StdErr.Write("|||ERROR||| ");
    WScript.StdErr.WriteLine(e.Message);
    WScript.Quit(1);
}

Note:
Before running this script, add a splash screen to the installer (using User-Interface view in Visual Studio)

(*) this is also a good example how to add/update binary(image) into your MSI using a sql script.

Best regard,
MDB-Blog

Tuesday, February 1, 2011

C# | Determine if Adobe (Macromedia) Flash player installed.

All started when I needed to integrate an Adobe (Macromedia) Flash object in my C# WinForm application.
I saw several posts about it, where two solutions where presents:
1. Add WebBrowser Control and just nevigate to the "test.swf" file.
2. Create COM object of ADOBE MACROMEDIA.

I liked the 1st option more, because I don't need to handle COM objects (and to add another DLL to the app).
Here is the code I used:
WebBrowser wb = new WebBrowser();
//wb.Location = SOME LOCATION
//wb.Size = SOME SIZE
wb.AllowNavigation = false;
wb.IsWebBrowserContextMenuEnabled = false;
this.Controls.Add(wb); // this = FORM
wb.Navigate(@"C:\test.swf");

BUT, Then i noticed that when a pc do NOT have Adobe (Macromedia) Flash player installed, the webbrowser display a big red X.
SO, before I use the FLASH image in my winform application, I needed to check if FLASH player is INSTALLED or not!

The idea is to check the registry for:
HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPlayer\CurrentVersion

And indeed it worked!
Here is the full code for testing if FLASH installed (and also get the major Version):
internal static int? GetFlashPlayerVersion()
{
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Macromedia\FlashPlayer"))
    {
        if (rk != null)
        {
            string version = rk.GetValue("CurrentVersion") as string;
            if (string.IsNullOrEmpty(version) == false)
            {
                int idx = version.IndexOf(",");
                if (idx > 0)
                {
                    int value;
                    if (int.TryParse(version.Substring(0, idx), out value) == true)
                    {
                        return value;
                    }
                }
            }
        }
    }
    return null;
}

And the code which using the method:
int? flashVersion = WindowsUtils.GetFlashPlayerVersion();
if (flashVersion.HasValue == true && flashVersion > 6)
{
    WebBrowser wb = new WebBrowser();
    wb.AllowNavigation = false;
    wb.IsWebBrowserContextMenuEnabled = false;
    this.Controls.Add(wb); // this = FORM
    wb.Navigate(@"C:\test.swf");
}

Best regards,
MDB-Blog

Wednesday, December 8, 2010

C# | Check if app is installed for “All users” or "Just me"


Today I bring a very HARD-to-find topic about getting an installed application "Installation context".
An installation context may be Per-Machine Installation Context (ALLUSERS=1) and Per-User Installation Context (ALLUSERS="").

When installing an application, the user may select one of two options:
a. Install [APP] for yourself
b. or for anyone who uses this computer

[a] installs only for the current user(just me) while [b] installs for the Local System user which is for all-users(everyone).

In this post I will give a code for checking is an installed application was installed for "Everyone" or "Just me".

The validation process is check if the APPLICATION-NAME exists in the registry keys under UserData.
(*) Everyone - check under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\ (S-1-5-18 represents the Local System user)

(*) Just me (Current user) - check under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-?!?!?!?!\Products\ (S-?!?!?!?! represents the Current user. this value found by the code: WindowsIdentity.GetCurrent().User.Value.


[Code] Method's return type:
private enum InstallationContexts { NotInstalled, Everyone, JustMe };

[Code] Method declaration:
private InstallationContexts GetInstalledContext(string p_appDisplayName)
{
    //The S-1-5-18  =
Local System user (Everyone) | HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\

    //The S-XXXXXX  =
Current user (Just me)       |
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-?!?!?!?!\Products\

    string key;
    string keyFormat = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{0}\Products\";
 
    // Check if installed for -> Everyone
    key = string.Format(keyFormat, "S-1-5-18");
    bool res = GetInstalledContext_IsRegKeyExists(key, p_appDisplayName, StringComparison.OrdinalIgnoreCase);
    if (res == true)
    {
        return InstallationContexts.Everyone;
    }
 
    // Check if installed for -> Just me
    key = string.Format(keyFormat, WindowsIdentity.GetCurrent().User.Value);
    res = GetInstalledContext_IsRegKeyExists(key, p_appDisplayName, StringComparison.OrdinalIgnoreCase);
    if (res == true)
    {
        return InstallationContexts.JustMe;
    }
 
    return InstallationContexts.NotInstalled;
}
private bool GetInstalledContext_IsRegKeyExists(string p_regKey, string p_appDisplayName, StringComparison p_scompare)
{
    using (RegistryKey regkey = Registry.LocalMachine.OpenSubKey(p_regKey))
    {
        if (regkey != null)
        {
            RegistryKey rk;
            string[] arrProducs = regkey.GetSubKeyNames();
            for (int i = 0; i < arrProducs.Length; i++)
            {

               using (rk = regkey.OpenSubKey(arrProducs[i] + @"\InstallProperties"))
               {
if (rk != null &&
                        p_appDisplayName.Equals(rk.GetValue("DisplayName").ToString(), p_scompare) == true)
                   {
                        return true;
                   }
               }
            }
        }
    }
 
    return false;
}

[Code] Method's usage example:
InstallationContexts appContext = GetInstalledContext("APP-DISPLAY-NAME");

This code gives the ability to detect previously installed application or previously installed software is installed to ALLUSERS (everyone) or only to CURRENT USER (just me).



Addition on 19.12.2010:
(*) This checking operation is valid ONLY for an application and software which were installed using Microsoft's Windows Installer.
For general checking for installed application, check my post about check if program/application is installed.


Enjoy...
MDB-Blog

Thursday, December 2, 2010

C# | check if program/application is installed on remote computer

As an advance to my previous post about checking if a program or an application is installed on my comptuer, I bringing a code for doing this validation on a remote computer.

The code is the same as checking for installed app in local comptuer (as described here) except that checking remote machine registry.
The remote machine registry checking is preformed using the static method OpenRemoteBaseKey of the class RegistryKey.

The code:
public static bool IsAppInstalled(string p_machineName, string p_name)
{
   string keyName;
 
   // search in: CurrentUser
   keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
   if (ExistsInRemoteSubKey(p_machineName, RegistryHive.CurrentUser, keyName, "DisplayName", p_name) == true)
   {
      return true;
   }
 
   // search in: LocalMachine_32
   keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
   if (ExistsInRemoteSubKey(p_machineName, RegistryHive.LocalMachine, keyName, "DisplayName", p_name) == true)
   {
      return true;
   }
 
   // search in: LocalMachine_64
   keyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
   if (ExistsInRemoteSubKey(p_machineName, RegistryHive.LocalMachine, keyName, "DisplayName", p_name) == true)
   {
      return true;
   }
 
   return false;
}
private static bool ExistsInRemoteSubKey(string p_machineName, RegistryHive p_hive, string p_subKeyName, string p_attributeName, string p_name)
{
   RegistryKey subkey;
   string displayName;
 
   using (RegistryKey regHive = RegistryKey.OpenRemoteBaseKey(p_hive, p_machineName))
   {
      using (RegistryKey regKey = regHive.OpenSubKey(p_subKeyName))
      {
         if (regKey != null)
         {
            foreach (string kn in regKey.GetSubKeyNames())
            {
               using (subkey = regKey.OpenSubKey(kn))
               {
                  displayName = subkey.GetValue(p_attributeName) as string;
                  if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) // key found!
                  {
                     return true;
                  }
               }
            }
         }
      }
   }
   return false;
}
    

How to use this code:
string MACHINE_NAME = "MY-PC-NAME";
string APPLICATION_NAME = "APP-NAME";
try
{
bool isAppInstalled = IsAppInstalled(MACHINE_NAME, APPLICATION_NAME);
  string msg = string.Format("Application '{0}' is {1} on the remote-machine '{2}'!",
APPLICATION_NAME,
isAppInstalled ? "installed" : "NOT installed",
MACHINE_NAME);
MessageBox.Show(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}



Enjoy...
MDB-Blog

Sunday, November 28, 2010

c# | Check if VcRedist installed (x86/x64/IA64)

The following code checks if vcredist (Microsoft Visual C++ 2008 SP1 Redistributable Package) is installed.
This check is used when we want to assure that the Visual C++ 2008 SP1 is INSTALLED perior to the application launch.

The code is using the MsiQueryProductState function from the msi.dll.

We check the product codes of vcredist_x86, vcredist_x64 and vcredist_IA64 using MsiQueryProductState to validate if the product is installed or not!
(vcredist product codes are listed HERE)

The Code:
public enum INSTALLSTATE
{
   INSTALLSTATE_NOTUSED = -7,  // component disabled
   INSTALLSTATE_BADCONFIG = -6,  // configuration datacorrupt
   INSTALLSTATE_INCOMPLETE = -5,  // installationsuspended or in progress
   INSTALLSTATE_SOURCEABSENT = -4,  // run from source,source is unavailable
   INSTALLSTATE_MOREDATA = -3,  // return bufferoverflow
   INSTALLSTATE_INVALIDARG = -2,  // invalid functionargument
   INSTALLSTATE_UNKNOWN = -1,  // unrecognized productor feature
   INSTALLSTATE_BROKEN = 0,  // broken
   INSTALLSTATE_ADVERTISED = 1,  // advertised feature
   INSTALLSTATE_REMOVED = 1,  // component being removed(action state, not settable)
   INSTALLSTATE_ABSENT = 2,  // uninstalled (or actionstate absent but clients remain)
   INSTALLSTATE_LOCAL = 3,  // installed on local drive
   INSTALLSTATE_SOURCE = 4,  // run from source, CD ornet
   INSTALLSTATE_DEFAULT = 5,  // use default, local orsource
}
 
[DllImport("msi.dll")]
private static extern INSTALLSTATE MsiQueryProductState(string product);
 
public static boolIsVCRedistInstalled()
{
   string[] strCodes = newstring[]
   {
       //vcredist_x86 - ProductCode
       "{9A25302D-30C0-39D9-BD6F-21E6EC160475}",
       "{86CE1746-9EFF-3C9C-8755-81EA8903AC34}",
       "{CA8A885F-E95B-3FC6-BB91-F4D9377C7686}",
       "{820B6609-4C97-3A2B-B644-573B06A0F0CC}",
       "{6AFCA4E1-9B78-3640-8F72-A7BF33448200}",
       "{F03CB3EF-DC16-35CE-B3C1-C68EA09E5E97}",
       "{402ED4A1-8F5B-387A-8688-997ABF58B8F2}",
       "{887868A2-D6DE-3255-AA92-AA0B5A59B874}",
       "{527BBE2F-1FED-3D8B-91CB-4DB0F838E69E}",
       "{57660847-B1F7-35BD-9118-F62EB863A598}",
 
       //vcredist_x64 - ProductCode
       "{8220EEFE-38CD-377E-8595-13398D740ACE}",
       "{56F27690-F6EA-3356-980A-02BA379506EE}",
       "{14297226-E0A0-3781-8911-E9D529552663}",
       "{9B3F0A88-790D-3AD9-9F96-B19CF2746452}",
       "{D285FC5F-3021-32E9-9C59-24CA325BDC5C}",
       "{092EE08C-60DE-3FE6-B113-90076EC06D0D}",
       "{A96702F7-EFC8-3EED-BE46-22C809D4EBE5}",
       "{92B8FD1F-C1AE-3750-8577-631B0AA85DF5}",
       "{2DFD8316-9EF1-3210-908C-4CB61961C1AC}",
       "{E34002C7-8CE7-3F76-B36C-09FA973BC4F6}",
 
       //vcredist_IA64 - ProductCode
       "{5827ECE1-AEB0-328E-B813-6FC68622C1F9}",
       "{9363B366-8370-34F7-8164-25052EBF35FD}",
       "{4EC84186-70BB-3121-9C1B-C63512D7126E}",
       "{1F7B9797-A3C8-3B98-85C4-00620F221CE8}",
       "{6BE0A7C7-3462-30EE-8B77-D21D7848D967}",
       "{BF58DC07-38AB-3887-8000-70173F9650EA}",
       "{D289009A-2728-3D0A-833E-F08E0E58934C}",
       "{9476DC14-00C3-3C36-A435-00D714CF77B8}",
       "{678835D7-D524-3C0E-9C33-1D3767FDA6BF}"
   };
 
   INSTALLSTATE state;
   for (int i = 0;i < strCodes.Length; i++)
   {
       state = MsiQueryProductState(strCodes[i]);
       if (state == INSTALLSTATE.INSTALLSTATE_LOCAL||
           state == INSTALLSTATE.INSTALLSTATE_DEFAULT)
       {
           return true;
       }
   }
 
   return false;
}

You may also extent and improve this method to check only those Product Codes that matching current architecture (x86/x64/IA64).

Hope it helps,
MDB-Blog

Wednesday, November 24, 2010

Bootstrapper Package | detect vcredist installed | Microsoft Visual C++ 2008 SP1 Redistributable Package

As a countinue post of my previous http://mdb-blog.blogspot.com/2010/11/bootstrapper-package-visual-studio-2010.html about Bootstrapper Package ( Visual Studio 2010) of Microsoft .NET Framework Version 2.0 Redistributable, i give an example for:
Bootstrapper Package for Microsoft Visual C++ 2008 SP1 Redistributable Package
(also known as Visual C++ 9.0 runtime redistributable package)

As I searched for many sites, I couldn't find any GOOD article about detecting this resi package.
So, I built a bootstrapper package using the information I found on Aaron's post: "How to detect the presence of the Visual C++ 9.0 runtime redistributable package" at http://blogs.msdn.com/b/astebner/archive/2009/01/29/9384143.aspx.

First of all, we have to detect if one of (vcredist_x86, vcredist_x64, vcredist_IA64) is INSTALLED!
To do so, we will use the MsiQueryProductState API inside our Product.xml in the bootstrapper package folder. (for more information about bootstrapper package files watch my previous post.
Because there is a difference in the Product-Code between the localized versions of the redistributables for VC++ 2008, i checked and write down all the product codes in the following table:

vcredist_x86.exeProductCode
English{9A25302D-30C0-39D9-BD6F-21E6EC160475}
ChineseSimplified{86CE1746-9EFF-3C9C-8755-81EA8903AC34}
ChineseTraditional{CA8A885F-E95B-3FC6-BB91-F4D9377C7686}
French{820B6609-4C97-3A2B-B644-573B06A0F0CC}
German{6AFCA4E1-9B78-3640-8F72-A7BF33448200}
Italian{F03CB3EF-DC16-35CE-B3C1-C68EA09E5E97}
Japanese{402ED4A1-8F5B-387A-8688-997ABF58B8F2}
Korean{887868A2-D6DE-3255-AA92-AA0B5A59B874}
Russian{527BBE2F-1FED-3D8B-91CB-4DB0F838E69E}
Spanish{57660847-B1F7-35BD-9118-F62EB863A598}
UpgradeCode for all {AA783A14-A7A3-3D33-95F0-9A351D530011}

vcredist_x64.exeProductCode
English{8220EEFE-38CD-377E-8595-13398D740ACE}
ChineseSimplified{56F27690-F6EA-3356-980A-02BA379506EE}
ChineseTraditional{14297226-E0A0-3781-8911-E9D529552663}
French{9B3F0A88-790D-3AD9-9F96-B19CF2746452}
German{D285FC5F-3021-32E9-9C59-24CA325BDC5C}
Italian{092EE08C-60DE-3FE6-B113-90076EC06D0D}
Japanese{A96702F7-EFC8-3EED-BE46-22C809D4EBE5}
Korean{92B8FD1F-C1AE-3750-8577-631B0AA85DF5}
Russian{2DFD8316-9EF1-3210-908C-4CB61961C1AC}
Spanish{E34002C7-8CE7-3F76-B36C-09FA973BC4F6}
UpgradeCode for all {AA783A14-A7A3-3D33-95F0-9A351D530011}

vcredist_IA64.exeProductCode
English{5827ECE1-AEB0-328E-B813-6FC68622C1F9}
ChineseSimplified{9363B366-8370-34F7-8164-25052EBF35FD}
ChineseTraditional{4EC84186-70BB-3121-9C1B-C63512D7126E}
French{1F7B9797-A3C8-3B98-85C4-00620F221CE8}
German{6BE0A7C7-3462-30EE-8B77-D21D7848D967}
Italian{BF58DC07-38AB-3887-8000-70173F9650EA}
Japanese{D289009A-2728-3D0A-833E-F08E0E58934C}
Korean{9476DC14-00C3-3C36-A435-00D714CF77B8}
Russian[Installer NOT exists!]
Spanish{678835D7-D524-3C0E-9C33-1D3767FDA6BF}
UpgradeCode for all {AA783A14-A7A3-3D33-95F0-9A351D530011}


All left is to create the bootstrapper folder (name it Vcredist2008sp1):
(*) en (FOLDER)
    (*) package.xml
(*) Product.xml
(*) redist.txt
(*) vcredist_x86.exe (download here)
(*) vcredist_x64.exe (download here)
(*) vcredist_IA64.exe (download here)


package.xml file:
<?xml version="1.0" encoding="utf-8"?>
<Package Name="DisplayName" Culture="Culture" xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper">
  <Strings>
    <String Name="Culture">en</String>
    <String Name="DisplayName">Microsoft Visual C++ 2008 SP1 Redistributable Package</String>
    <String Name="AdminRequired">Administrator permissions are required to install the Microsoft Visual C++ 2008 SP1 Redistributable Package Contact your administrator.</String>
    <String Name="CancelledInstall">The user has cancelled the installation. The Microsoft Visual C++ 2008 SP1 Redistributable Package has not been installed.</String>
    <String Name="GeneralFailure">A failure occurred attempting to install the Microsoft Visual C++ 2008 SP1 Redistributable Package.</String>
    <String Name="ErrorDownloading">A failure occurred while downloading Microsoft Visual C++ 2008 SP1 Redistributable Package.</String>
  </Strings>
</Package>

Product.xml file:
1. PackageFiles (installation files):
<PackageFiles CopyAllPackageFiles="false">
  <PackageFile Name="vcredist_x86.exe" />
  <PackageFile Name="vcredist_x64.exe" />
  <PackageFile Name="vcredist_IA64.exe" />
</PackageFiles>

2. InstallChecks (valiadation prefurmed):
<InstallChecks>
  <!--VCRedistx86 - ProductCodes-->
  <MsiProductCheck Property="VCRedistx86_English_Installed" Product="{9A25302D-30C0-39D9-BD6F-21E6EC160475}" />
  <MsiProductCheck Property="VCRedistx86_ChineseSimplified_Installed" Product="{86CE1746-9EFF-3C9C-8755-81EA8903AC34}" />
  <MsiProductCheck Property="VCRedistx86_ChineseTraditional_Installed" Product="{CA8A885F-E95B-3FC6-BB91-F4D9377C7686}" />
  <MsiProductCheck Property="VCRedistx86_French_Installed" Product="{820B6609-4C97-3A2B-B644-573B06A0F0CC}" />
  <MsiProductCheck Property="VCRedistx86_German_Installed" Product="{6AFCA4E1-9B78-3640-8F72-A7BF33448200}" />
  <MsiProductCheck Property="VCRedistx86_Italian_Installed" Product="{F03CB3EF-DC16-35CE-B3C1-C68EA09E5E97}" />
  <MsiProductCheck Property="VCRedistx86_Japanese_Installed" Product="{402ED4A1-8F5B-387A-8688-997ABF58B8F2}" />
  <MsiProductCheck Property="VCRedistx86_Korean_Installed" Product="{887868A2-D6DE-3255-AA92-AA0B5A59B874}" />
  <MsiProductCheck Property="VCRedistx86_Russian_Installed" Product="{527BBE2F-1FED-3D8B-91CB-4DB0F838E69E}" />
  <MsiProductCheck Property="VCRedistx86_Spanish_Installed" Product="{57660847-B1F7-35BD-9118-F62EB863A598}" />

  <!--VCRedistx64 - ProductCodes-->
  <MsiProductCheck Property="VCRedistx64_English_Installed" Product="{8220EEFE-38CD-377E-8595-13398D740ACE}" />
  <MsiProductCheck Property="VCRedistx64_ChineseSimplified_Installed" Product="{56F27690-F6EA-3356-980A-02BA379506EE}" />
  <MsiProductCheck Property="VCRedistx64_ChineseTraditional_Installed" Product="{14297226-E0A0-3781-8911-E9D529552663}" />
  <MsiProductCheck Property="VCRedistx64_French_Installed" Product="{9B3F0A88-790D-3AD9-9F96-B19CF2746452}" />
  <MsiProductCheck Property="VCRedistx64_German_Installed" Product="{D285FC5F-3021-32E9-9C59-24CA325BDC5C}" />
  <MsiProductCheck Property="VCRedistx64_Italian_Installed" Product="{092EE08C-60DE-3FE6-B113-90076EC06D0D}" />
  <MsiProductCheck Property="VCRedistx64_Japanese_Installed" Product="{A96702F7-EFC8-3EED-BE46-22C809D4EBE5}" />
  <MsiProductCheck Property="VCRedistx64_Korean_Installed" Product="{92B8FD1F-C1AE-3750-8577-631B0AA85DF5}" />
  <MsiProductCheck Property="VCRedistx64_Russian_Installed" Product="{2DFD8316-9EF1-3210-908C-4CB61961C1AC}" />
  <MsiProductCheck Property="VCRedistx64_Spanish_Installed" Product="{E34002C7-8CE7-3F76-B36C-09FA973BC4F6}" />

  <!--VCRedistIA64 - ProductCodes-->
  <MsiProductCheck Property="VCRedistIA64_English_Installed" Product="{5827ECE1-AEB0-328E-B813-6FC68622C1F9}" />
  <MsiProductCheck Property="VCRedistIA64_ChineseSimplified_Installed" Product="{9363B366-8370-34F7-8164-25052EBF35FD}" />
  <MsiProductCheck Property="VCRedistIA64_ChineseTraditional_Installed" Product="{4EC84186-70BB-3121-9C1B-C63512D7126E}" />
  <MsiProductCheck Property="VCRedistIA64_French_Installed" Product="{1F7B9797-A3C8-3B98-85C4-00620F221CE8}" />
  <MsiProductCheck Property="VCRedistIA64_German_Installed" Product="{6BE0A7C7-3462-30EE-8B77-D21D7848D967}" />
  <MsiProductCheck Property="VCRedistIA64_Italian_Installed" Product="{BF58DC07-38AB-3887-8000-70173F9650EA}" />
  <MsiProductCheck Property="VCRedistIA64_Japanese_Installed" Product="{D289009A-2728-3D0A-833E-F08E0E58934C}" />
  <MsiProductCheck Property="VCRedistIA64_Korean_Installed" Product="{9476DC14-00C3-3C36-A435-00D714CF77B8}" />
  <!--RUSSIAN installer not exists -->
  <MsiProductCheck Property="VCRedistIA64_Spanish_Installed" Product="{678835D7-D524-3C0E-9C33-1D3767FDA6BF}" />
</InstallChecks>

3. for each installation check if any localized installed:
<BypassIf Property="VCRedistx86_English_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_ChineseSimplified_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_ChineseTraditional_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_French_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_German_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_Italian_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_Japanese_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_Korean_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_Russian_Installed" Compare="ValueEqualTo" Value="5" />
<BypassIf Property="VCRedistx86_Spanish_Installed" Compare="ValueEqualTo" Value="5" />

Download link for the full bootstrapper package is here:
download full package here

Hope it helps,
MDB-Blog

Tuesday, November 23, 2010

How to determine the Windows version by using Visual C# | windows 7 windows vista

Here is a good code includes Windows 7 and Windows Vista recognaizer.

Code:
 
 
 
 
internal enum WindowsVersions { UnKnown, Win95, Win98, WinMe, WinNT3or4, Win2000, WinXP, WinServer2003, WinVista, Win7, Win8, MacOSX, Unix, Xbox };
internal static WindowsVersions GetCurrentWindowsVersion()
{
    // Get OperatingSystem information from the system namespace.
    System.OperatingSystem osInfo = System.Environment.OSVersion;
 
    // Determine the platform.
    if (osInfo.Platform == System.PlatformID.Win32Windows)
    {
        // Platform is Windows 95, Windows 98, Windows 98 Second Edition, or Windows Me.
        switch (osInfo.Version.Minor)
        {
            case 0:
                //Console.WriteLine("Windows 95");
                return WindowsVersions.Win95;
 
            case 10:
                //if (osInfo.Version.Revision.ToString() == "2222A")
                //    Console.WriteLine("Windows 98 Second Edition");
                //else
                //    Console.WriteLine("Windows 98");
                return WindowsVersions.Win98;
 
            case 90:
                //Console.WriteLine("Windows Me");
                return WindowsVersions.WinMe;
        }
    }
    else if (osInfo.Platform == System.PlatformID.Win32NT)
    {
        // Platform is Windows NT 3.51, Windows NT 4.0, Windows 2000, or Windows XP.
        switch (osInfo.Version.Major)
        {
            case 3:
            case 4:
                //Console.WriteLine("Windows NT 3.51"); // = 3
                //Console.WriteLine("Windows NT 4.0");  // = 4
                return WindowsVersions.WinNT3or4;
 
            case 5:
                switch (osInfo.Version.Minor)
                {
                    case 0:
                        //name = "Windows 2000";
                        return WindowsVersions.Win2000;
                    case 1:
                        //name = "Windows XP";
                        return WindowsVersions.WinXP;
                    case 2:
                        //name = "Windows Server 2003";
                        return WindowsVersions.WinServer2003;
                }
                break;
 
            case 6:
                switch (osInfo.Version.Minor)
                {
                    case 0:
                        // Windows Vista or Windows Server 2008 (distinct by rpoduct type)
                        return WindowsVersions.WinVista;
 
                    case 1:
                        return WindowsVersions.Win7;
 
                    case 2:
                        return WindowsVersions.Win8;
                }
                break;
        }
    }
    else if (osInfo.Platform == System.PlatformID.Unix)
    {
        return WindowsVersions.Unix;
    }
    else if (osInfo.Platform == System.PlatformID.MacOSX)
    {
        return WindowsVersions.MacOSX;
    }
    else if (osInfo.Platform == PlatformID.Xbox)
    {
        return WindowsVersions.Xbox;
    }
    return WindowsVersions.UnKnown;
}
 
 
 
 
 
2013/07/02: added WIN8 support