.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Send Keys to Application Programmatically using C#

Posted By:Jean Paul       Posted Date: November 30, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we have explored a Send Key scenario with Windows API as the solution. You can comment on better ideas for the problem.
 

In this article we can explore a scenario I have encountered while working with SharePoint.

clip_image002

Scenario

My client had a Production issue where the Request Tracking report is too huge in size and cannot be downloaded. Although the problem is fixed, we need to test this in our UAT environment.

To test this we need 1000 requests logged in the SharePoint site. How to create the 1000 requests?

I had the following interesting ideas & solutions.

Idea 1: Create an SQL stored procedure that inserts 1000 records to the Content Database.

Problems: Content Database access issue, directly modifying is not recommended

Idea 2: Create a web part that inserts records into the Audit Log using Server Object Model

Problems: Some of the fields will remain null

Idea 3: Click the browser refresh button for 1000 times

Problems: Too hectic for a human to perform

Idea 4: Place a lock on the F5 key

Problems: The site requires 3 seconds for loading, so skips all the key requests if continuous

Idea 5: Create a Web Client automation

Problems: The site is under security proxies. So the URL automation is not possible.

Idea 6: Send Keys to Browser

Action: Feasible

I selected the Idea 6 as the site is authenticated in a browser window. Through a separate windows application we can send Refresh keys (F5) on regular intervals using Windows API.

clip_image004

Windows API invocation can be done in .Net using Platform Invoke infrastructure.

The Steps

Following are the steps involved in the application.

1. Find the Window Handle using the Title

a. For example: "Bing - Windows Internet Explorer" is the title of Internet Explorer while opening website http://www.bing.com/

2. Make the Window as Active Window

a. Use the SetForegroundWindow API method to make the window as active, so that we can send keys to active window

3. Send the Key to the window

a. Send Key (F5) to the active window

4. Repeat the loop

a. Continue the loop with an interval specified by the user.

The Code

Following are the Windows API methods required by our application.

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]

public static extern IntPtr FindWindow (IntPtr ZeroOnly, string lpWindowName);

[DllImport("user32.dll")]

public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, IntPtr dwExtraInfo);

[DllImport("user32.dll")]

[return: MarshalAs(UnmanagedType.Bool)]

static extern bool SetForegroundWindow(IntPtr hWnd);

Following is the code to get the Window Handle by caption.

_pointer = FindWindow(new IntPtr(0), TitleText.Text);

If the _pointer has a value greater than 0, we are good to have a valid Handle.

clip_image004[1]

A handle (HWND) is the return value from CreateWindowEx which the Windows Operating System uses to identify a window.

 

The UI

Create a windows forms application and set the following controls.

clip_image005

On the start button click add the following code.

 

private void StartButton_Click(object sender, EventArgs e)
{
    Start();
}

private bool _start;
private int _count;

private void Start()
{
    _start = true;
    StartButton.Enabled = false;
    StopButton.Enabled = true;

    SetForegroundWindow(_pointer);

    while (_start)
    {
        SendKeys();

        Thread.Sleep(int.Parse(textBox3.Text) * 1000);
        Application.DoEvents();
    }
}

public void SendKeys()
{
    int key = int.Parse(KeyText.Text);

    const int KEYEVENTF_KEYUP = 0x0002;

    keybd_event((byte)key, 0, 0, IntPtr.Zero);
    keybd_event((byte)key, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
}

// The keybd_event() method SENDS the key

The Implementation

Open a browser and browse to our SharePoint site. Ensure you have logged in correctly.

clip_image007

Now execute our application and enter the title of the Browser in the application. In this case it is: "Reports - All Documents - Windows Internet Explorer"

clip_image008

Enter the Key value. For F5 key, it is ASCII 116. Set the Interval as 4 seconds which will create around 900 requests per hour.

After setting the values click the Start button. You will see that the browser is made as foreground window and the Refresh process starts repeating every 4 seconds. (Observe the Refresh of browser status bar)

References

http://tinyurl.com/sp2010-winapi

Summary

In this article we have explored a Send Key scenario with Windows API as the solution. You can comment on better ideas for the problem.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend