Showing posts with label OpenSource. Show all posts
Showing posts with label OpenSource. Show all posts

Tuesday 7 May 2013

Get Windows Architecture in Dot NET

How to Determine if 64bit or 32bit OS Version


There are a lot of ways to Get Windows Architecture in .NET, the .NET Framework v4.0 already has this method on their libraries, but many people still are using previous versions (as v3.5 or v2.0).

For .NET v4.0 or higher, it is really simple, just call the ‘Environment.Is64BitOperatingSystem’ property and check if is true or false.:

C# Code :
if (Environment.Is64BitOperatingSystem)
    Console.WriteLine("Your OS is 64 bits.");
else Console.WriteLine("Your OS is 32 bits.");

But for the people using v3.5 or previous, We have this simple way compatible with ‘x86′, ‘x64′ and ‘AnyCPU’ architectures:

//C# Code :
public static bool Is64BitOperatingSystem()
{
    if (!Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).ToUpper().Contains("SYSTEM32"))
        return true; //Check for the SystemX86 variable. If is not 'SYSTEM32' then the Operating System is x64 (SystemX86 = SYSWOW64).
    return false; //Return false when the method was not blocked by the if statement.
}


Public Shared Function Is64BitOperatingSystem() As Boolean
    If Not Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).ToUpper().Contains("SYSTEM32") Then
        Return True
    End If
    'Check for the SystemX86 variable. If is not 'SYSTEM32' then the Operating System is x64 (SystemX86 = SYSWOW64).
    Return False
    'Return false when the method was not blocked by the if statement.
End Function

We also have a way to do it by checking the IntPtr Size:

if (IntPtr.Size == 8)
    Console.WriteLine("Your OS is 64 bits.");
else Console.WriteLine("Your OS is 32 bits.");

‘IntPtr.Size’ always will be eight when the process is running in memory as x64, but only will work if your project was made using the ‘AnyCPU’ platform.


For another Class that using 'kernel32'

//C# Code :
using System;
using System.Runtime.InteropServices;
 
namespace com0do99.library
{
    public static class OsVersionHelper
    {
 
        // Will return result of 64but OS on all versions of windows that support .net
        public static bool Is64BitOperatingSystem()
        {
            if ( IntPtr.Size == 8 ) //64bit will only run on 64bit
            {
                return true;
            }
            bool flag;
            return (DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out flag)) && flag;
        }
 
        private static bool DoesWin32MethodExist(string moduleName, string methodName)
        {
            IntPtr moduleHandle = GetModuleHandle(moduleName);
            if (moduleHandle == IntPtr.Zero)
            {
                return false;
            }
 
            return GetProcAddress(moduleHandle, methodName) != IntPtr.Zero;
        }
 
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetCurrentProcess();
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr GetModuleHandle(string moduleName);
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetProcAddress(IntPtr module, [MarshalAs(UnmanagedType.LPStr)]string procName);
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool IsWow64Process(IntPtr process, out bool wow64Process);
    }
}



Thank You

Monday 8 April 2013

Passing Value Between Form in Csharp (C#) and VB.Net

Passing Value Between Form in Csharp (C#) and VB.Net

In C# and vb.net, there are many situations the new programmers face the same problem about how to pass data and values from one form to another. We can pass values from one form to another in a number of ways. Here you can see one of the easiest method to pass values from one form to another.

Passing Value Form1 to Form2 in Csharp (C#) and VB.Net
  • Here we send the value with calling object properties.
Csharp (C#)
In Form1
private void button1_Click(object sender, EventArgs e)
   {
      Form2 frm = new Form2();
            frm.textBox1.Text = "Hi";
            frm.Show();
        }

  • Send the value as arguments of the constructor.

Csharp (C#)
In Form2
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {

        }
        public Form2(string txt)
        {
            InitializeComponent();
            textBox1.Text = txt;
        }
    }
}

VB.Net
Private Sub button1_Click(sender As Object, e As EventArgs)
 Dim frm As New Form2()
 frm.textBox1.Text = "Hi"
 frm.Show()
End Sub

Public Class Form2
    Public Sub New(ByVal sText As String)
        InitializeComponent()
        Me.textBox1.Text = sText
    End Sub
End Class


Passing Value Form2 to Form1 in Csharp (C#) and VB.Net

Csharp (C#)
In Form1
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 frm;
        public Form1()
        {
            InitializeComponent();
        }

        public string GetValueForm2
        {
            get { return m_value; }
            set
            {
                m_value = value;
            }
        }
        private string m_value;
        
        //Open Form2
        private void Button1_Click(object sender, EventArgs e)
        {
            frm = new Form2(this);
            frm.Show();
        }
        //Click Button2 to Get Passed Value
        private void Button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = m_value;
        }

    }
}

In Form2
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 ParForm;
        public Form2(Form1 myParForm)
        {
            ParForm = myParForm;
            InitializeComponent();
        }
        
        //Click Button1 to Passing Value to Form1
        private void Button1_Click(object sender, EventArgs e)
        {
            ParForm.GetValueForm2 = textBox1.Text;
        }

    }
}



VB.Net
Public Class Form1

 Private frm As Form2

 Public Property GetValueForm2() As String
  Get
   Return m_value
  End Get
  Set
   m_value = value
  End Set
 End Property
 Private m_value As String

   'Open Form2
 Private Sub Button1_Click(sender As Object, e As EventArgs)
  frm = New Form2(Me)
  frm.Show()
 End Sub
 'Click Button2 to Get Passed Value
 Private Sub Button2_Click(sender As Object, e As EventArgs)
  TextBox1.Text = m_value
 End Sub

End Class

In Form2
Public Class Form2

    'In Form2 add a "New" constructor
    Dim ParForm As Form1
    Public Sub New(ByVal myParForm As Form1)
        MyBase.New()
        ParForm = myParForm
        InitializeComponent()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ParForm.GetValueForm2 = TextBox1.Text
    End Sub
End Class

Thank You

Download Sample Application using Microsoft Visual Studio 2010
Csharp | VB.Net