Monday 18 August 2014

Placing Windows claimed by different process and changing Window Title in C#


Placing Windows claimed by different process and changing Window Title in C# 

To place windows possessed by different methodology is possible utilizing System.runtime.interopservices 

say for in this sample We need that when we click Button in our c# application it will change the Title of the Task Manage Window which is not the type of our application and we need to transform its Title Text to whatever we need 

The Process 

Place the Window Handle utilizing one of the user32.dll capacity called Findwindow() 

Change the Text on Title utilizing second capacity of user32.dll called Setwindowtext() 

I said that process is basic yet the capacity to do assignment about Locating Window Handle is not there in the .net Framework so we have to call them utilizing unmaneged code which is within Win32api 

To get outside dll funtion composed in Native Code we require model of the Function in out Application code to execute that capacities so we have to do that utilizing Importdll[...] 

alright, Now how to do all the errand I said in code. 

set up some fundamental UI like underneath with one catch and Textbox + Label


Import the name space
using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string ClassName,string WindowName);

[DllImport("user32.dll")]
public static extern IntPtr SetWindowText(IntPtr HWND, string Text);

private void btnChangeTitleText_Click(object sender, EventArgs e)
{
    /* Locate the Handle of TaskManager window */
    IntPtr HWND = FindWindow(null,"Windows Task Manager");

    if(HWND != IntPtr.Zero)
    {
       SetWindowText(HWND, textBox1.Text);
       MessageBox.Show("Text Changed Successfully ");
    }
    else 
    {
        MessageBox.Show("Open Task Manager First !");
    }
}
as I said we need that name space I added with using... statement. then we added prototype of function we want to call from User32.Dll and used it to locate the window of Task Manager and Changed the Title Text of it
Result : we changed the Title of Task Manager Window

ShareThis
Links to this post  
Tags Windows Forms
Encrypting/Decrypting Data With RSA Algorithm in C#
Monday, 28 November 2011 Posted by Kirtan Patel at 11/28/2011 09:30:00 PM 0 comments
Some time we need to hide our confidential data from the other user for that purpose we use encryption Algorithms to Encrypt our Data. RSA is public key Algorithm so it make use of two keys
Public Key
Private Key
Public Key is Shared to Public who want to send us Data.
Private Key is Kept Secrete with us so that when some one send us data encrypted by our Public Key,We can decrypt the data using that Private Key.
Here i will show you small Application that will show you how to encrypt and Decrypt data using RSA Algorithm 
Setup some Basic UI like below with One TextBox,One label and Two Buttons Encrypt and Decrypt

Encrypting/Decrypting Data with RSACryptoServiceProvider

When you Click Encrypt Button data in TextBox will be encrypted and Shown in Same TextBox.When you press Decrypt Button Encrypted data in TextBox will be Decrypted
Code is very easy to understand First we Created Private Key and Public Keyparameters to be used in Encryption Decryption Process by using ExportParameter() method.Here to demonstrate the use of RSA I used all the variable globally in class but KeyInfomation can be transmitted over Internet in actual situation.
then after that the button code is very simple when you encrypt the data Public Key Will be used and When you decrypt the data Private Key will be used to Decrypt the data.
RSAParameters PublicKeyInfo;
RSAParameters PrivateKeyInfo;
byte[] EncryptedByte;
byte[] DecryptedByte;

public Form1()
{
    InitializeComponent();
    
    /* Initialize RSA and Get Private and Public KeyInfo in paramters */
    RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider();
    Rsa.KeySize = 1024;
    
    /* Generating Info that Contain Public Key Info and Private Key Info*/
    PublicKeyInfo = Rsa.ExportParameters(false);
    PrivateKeyInfo = Rsa.ExportParameters(true);
}

/* Encrypt Data using Public Key info */
private void btnEncrypt_Click(object sender, EventArgs e)
{
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

    /* Encrypt Data using Public Key Info */
    rsa.ImportParameters(PublicKeyInfo);
    UnicodeEncoding ue = new UnicodeEncoding();
    byte[] DataToEncrypt = ue.GetBytes(textBox1.Text);
    EncryptedByte = rsa.Encrypt(DataToEncrypt, false);

    /* Show Encrypted Data into Text Box */
    textBox1.Text = ue.GetString(EncryptedByte);
    rsa.Dispose();
}

/* Decrypt Encrypted Data in TextBox1 using Private KeyInfo*/
private void btnDecrypt_Click(object sender, EventArgs e)
{
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.ImportParameters(PrivateKeyInfo);

    UnicodeEncoding ue = new UnicodeEncoding();

    /* Decrypt Data and Show in Same TextBox */
    DecryptedByte  = rsa.Decrypt(EncryptedByte, false);

    /* Show Decrypted Data into TextBox */
    textBox1.Text = ue.GetString(DecryptedByte);
    rsa.Dispose();

}
ShareThis
Links to this post  
Tags C# Language , Decrypt , Encrypt , RSA , Security , System.Security.Cryptography
Get list of all Forms in Project using c# Reflection
Posted by Kirtan Patel at 11/28/2011 05:31:00 PM 1 comments
What we want to do here is we have some forms listed in Solution explorer we need to get all those form names in listbox control as shown in Second Figure.


its simple and easy to do with Reflection Name Space classes
using System.Reflection;
private void btnGetformNames_Click(object sender, EventArgs e)
{
    Type[] AllTypesInProjects = Assembly.GetExecutingAssembly().GetTypes();
    for(int i=0;i<AllTypesInProjects.Length;i++)
    {
        if (AllTypesInProjects[i].BaseType == typeof(Form))
        {
            /* Convert Type to Object */
            Form f = (Form)Activator.CreateInstance(AllTypesInProjects[i]);
            string FormText =f.Text;

            listBox1.Items.Add(FormText);
        }

    }
}
ShareThis
Links to this post  
Tags Get List of All Forms of project , System.Reflection , Windows Forms
Show Form by its name in c# using Reflection
Posted by Kirtan Patel at 11/28/2011 01:30:00 AM 0 comments
some time we wants that we store names of all the available form names in database and as per user select the form name string from database we need to show form.

No comments:

Post a Comment