Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
Build a console application that automates a Windows based application which adds , subtracts, multiplies and divides two integers.The automation should be able to launch the application, enter two integers and select an operation and click equals button


What I have tried:

I was able to code the application which performs operations between integers. Couldn't write code for automating it through console.Code i wrote for first part is:
private void btnTotal_Click(object sender,EventArgs e)
        {
            try
            {
                double firstnum= Convert.ToDouble(txtFirstNumber.Text);
				double secondnum=Convert.ToDoubletxtSeconNumber.Text);
				char operator;
                switch (operator)
                {
                    case "+":
                        {                     
							lblResult.visible=true;
							lblResult.text="Result is: " + (firstnum+secondnum).ToString();
                            break;
                        }
                    case "-":
                        {
                            lblResult.visible=true;
							lblResult.text="Result is: " + (firstnum-secondnum).ToString();
                            break;
                        }
                    case "*":
                        {
							lblResult.visible=true;
							lblResult.text="Result is: " + (firstnum*secondnum).ToString();
                            break;
                        }
                    case "/":
                        {
							if (secondnum != 0) 
							{ 
								lblResult.visible=true;
								lblResult.text="Result is: " + (firstnum*secondnum).ToString(); 
							} 
							else 
							{ 
								MessageBox.Show("Can't divide by zero"); 
							} 
							break;
                        }
					default:
                        {
                            MessageBox.Show("Invalid Input"); 
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);  
            }
        }
Posted
Updated 6-Apr-17 9:01am
Comments
[no name] 4-Apr-17 10:59am    
Again.... what is your specific question?
Member 13097868 4-Apr-17 11:11am    
My question is how to call my windows form (i created above) from my console application using WinAPI functions and User32.dll( this is what is mentioned in my assignment)
F-ES Sitecore 4-Apr-17 11:16am    
It's your homework for your course so they want you to use the techniques you've been taught. We don't know what those techniques are and even if we did we still aren't going to do your homework for you.
Member 13097868 4-Apr-17 11:21am    
This is an assignment , I don't have any experience in automation. I have tried my level best and have been working on it since thursday. I am not expecting a code rather a guidance. If you can guide I'll appreciate it. I have 0 year exp in dotnet, still tried to write the above code. So atleast dont demotivate if not help.
Member 13097868 4-Apr-17 11:22am    
And yes for your information I am doing self study to learn dotnet andthese sort of assignments i try to search so that i can learn more

1 solution

I would suggest you use the task scheduler to execute a windows console application or similar but you will need to investigate "findwindow" and "sendmessage" api calls.

-- Calling Application

[DllImport("user32.dll")]    
public static extern IntPtr FindWindow(string lpClassName, String lpWindowName);    
[DllImport("user32.dll")]    
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);    
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);

public void SendMessageToApp
{        
     uint id = RegisterWindowMessage("MyUniqueMessageIdentifier");
     IntPtr WindowToFind = FindWindow(null, "Form1");    
     Debug.Assert(WindowToFind != IntPtr.Zero);
     SendMessage(WindowToFind, id, IntPtr.Zero, IntPtr.Zero);
}

-- Receiving Application

class Form1 : Form
{
    [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
    static extern uint RegisterWindowMessage(string lpString);

    private uint _messageId = RegisterWindowMessage("MyUniqueMessageIdentifier");

    protected override void WndProc(ref Message m)
    {
       if (m.Msg == _messageId)
       {
           // do stuff

       }
       base.WndProc(ref m);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900