Click here to Skip to main content
15,892,643 members
Home / Discussions / C#
   

C#

 
GeneralRe: newbie handling user controls click event in form Pin
S. Senthil Kumar6-May-05 8:02
S. Senthil Kumar6-May-05 8:02 
GeneralLZX or MSZIP Compressing Algorithms Imprementations Pin
Stanimir_Stoyanov6-May-05 4:26
Stanimir_Stoyanov6-May-05 4:26 
GeneralHaving problems writing text to a ListView Pin
Anonymous6-May-05 4:23
Anonymous6-May-05 4:23 
GeneralRe: Having problems writing text to a ListView Pin
ChesterPoindexter6-May-05 5:28
professionalChesterPoindexter6-May-05 5:28 
GeneralSerial Port communication Pin
Subrahmanyam K6-May-05 4:16
Subrahmanyam K6-May-05 4:16 
GeneralRe: Serial Port communication Pin
Gary Thom6-May-05 4:30
Gary Thom6-May-05 4:30 
GeneralAuto Login Pin
parsleylion6-May-05 3:59
parsleylion6-May-05 3:59 
GeneralRe: Auto Login Pin
jklucker6-May-05 5:40
jklucker6-May-05 5:40 
I use the following code to do what you want to do. I have changed some names to protect the innocent, so where you values inside <>, just change it to reflect what you need.

Usage: Just copy and paste into a class file.

Good Luck!Big Grin | :-D

<br />
using System;<br />
using System.Text;<br />
using System.Runtime.InteropServices;<br />
using System.Diagnostics;<br />
<br />
namespace APPLoginWindow<br />
{<br />
	/// <summary><br />
	/// Summary description for Class1.<br />
	/// </summary><br />
	public class APPLogin<br />
	{<br />
<br />
		private object data;<br />
<br />
		#region Contructor, dispose, main<br />
		public APPLogin()<br />
		{<br />
			getAPPLogin();<br />
		}<br />
		#endregion<br />
<br />
		#region win32 imports<br />
		public delegate bool EnumWindowsCallback(int hwnd, int lParam);<br />
		public delegate bool EnumThreadProc(IntPtr hWnd, IntPtr lParam);<br />
<br />
		[DllImport("User32.dll")] public static extern int GetWindowText(int hwnd,StringBuilder lpString, uint bufferSize);<br />
		 <br />
		[DllImport("User32.dll")] public static extern int EnumWindows (EnumWindowsCallback callback, int lParam);<br />
		[DllImport("User32.dll")] public static extern int EnumChildWindows (int hWndParent, EnumWindowsCallback callback, int lParam);<br />
		[DllImport("User32.dll")] public static extern bool EnumThreadWindows (uint threadId, EnumThreadProc pfnEnum, IntPtr lParam);<br />
<br />
		[DllImport("User32.dll")] public static extern int RealGetWindowClass(int hWnd,StringBuilder pszType,uint bufferSize);<br />
		[DllImport("User32.dll")] public static extern int SendMessage( int hwnd, int uMsg, int wParam, [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lParam);<br />
		<br />
		[DllImport("User32.dll") ] public static extern int FindWindow (string lpClassName,string WindowName);<br />
		[DllImport("User32.dll") ] public static extern int FindWindowEx (int hWnd,int hWnd2,string lpsz,string lpsz2);<br />
		[DllImport("User32.dll") ] public static extern int SetWindowText(int hWnd,string lpsz);<br />
		#endregion<br />
<br />
		#region Constants + fields<br />
		public static int GW_HWNDNEXT = 2;<br />
		public static int GW_CHILD = 5;<br />
		public static int GW_OWNER = 4;<br />
		public static int GWL_HWNDPARENT = -8;<br />
		public static int WM_SETTEXT =  12;<br />
		public const int HWND_MESSAGE = -3;<br />
		public const int BM_CLICK = 0x00F5; //Clicking a button<br />
		public const int WM_CHAR = 258;<br />
		private string _windowText = "";<br />
		#endregion<br />
<br />
		#region Callback functions for listing windows<br />
		private bool displayAPPLogin(int hWnd, int lParam)<br />
		{<br />
			StringBuilder windowName = new StringBuilder(255);<br />
			StringBuilder className  = new StringBuilder(255);<br />
<br />
			GetWindowText(hWnd, windowName, 255);<br />
			RealGetWindowClass(hWnd,className,255);<br />
			if ( windowName.ToString().ToLower().IndexOf("<Name of Login Window>") != -1 )<br />
			{<br />
				data = hWnd;<br />
			}<br />
<br />
			return true;<br />
		}<br />
<br />
		#endregion<br />
<br />
		#region Sendmessage clicks<br />
		public void PerformAPPLogin()<br />
		{<br />
			int hWndAPPLogin;<br />
<br />
			hWndAPPLogin = (int) data;<br />
<br />
			if ( hWndAPPLogin != 0 )<br />
			{<br />
				int hWndAPPLoginUserName;<br />
				int hWndAPPLoginPassWord;<br />
				int hWndAPPLoginOKButton;<br />
<br />
				hWndAPPLoginUserName = FindWindowEx(hWndAPPLogin,0,"Edit",null); // Find the UserName Edit Field in the APP Login Window<br />
				SendMessage(hWndAPPLoginUserName,WM_SETTEXT,0,"<UserName>"); // Enter the username of <UserName><br />
				hWndAPPLoginPassWord = FindWindowEx(hWndAPPLogin,hWndAPPLoginUserName,"Edit",null); // Find the PassWord Edit Field in the APP Login Window<br />
				SendMessage(hWndAPPLoginPassWord,WM_SETTEXT,0,"<Password>"); // Enter the password of <Password><br />
				hWndAPPLoginOKButton = FindWindowEx(hWndAPPLogin,0,"Button", null); // Find the OK button to perform login<br />
				SendMessage(hWndAPPLoginOKButton,BM_CLICK,0,null); // Press the OK button to perform login<br />
			}<br />
		}<br />
		<br />
		#endregion<br />
<br />
		#region Get APP Login Window<br />
		private void getAPPLogin()<br />
		{<br />
			bool APPRunning = false;<br />
<br />
			// Get all processes<br />
			Process[] processes = Process.GetProcesses();<br />
			for (int i=0;i <= processes.Length -1;i++)<br />
			{<br />
				if (processes[i].ProcessName.ToLower().IndexOf("<Name of Process>") != -1)<br />
				{<br />
					APPRunning = true;<br />
				}<br />
			}<br />
<br />
			if ( APPRunning )<br />
			{<br />
				// Get all APP Login windows<br />
<br />
				EnumWindowsCallback EnumWCB = new EnumWindowsCallback(displaySAPLogin);<br />
				EnumWindows(EnumWCB ,0);<br />
<br />
			}<br />
		}<br />
		<br />
		#endregion<br />
<br />
	}<br />
}<br />


I reject your reality and substitute my own!
- Adam Savage, Mythbuster
- George W Bush

life is like a roll of toilet paper. The closer it gets to the end, the faster it goes.
GeneralTexture mapping and color replacing with GDI Pin
DaViL836-May-05 3:39
DaViL836-May-05 3:39 
GeneralRe: Texture mapping and color replacing with GDI Pin
DaViL836-May-05 11:10
DaViL836-May-05 11:10 
General3d graph in C# Pin
Member 17766006-May-05 3:32
Member 17766006-May-05 3:32 
Generalnumber to alpha Pin
boruu6-May-05 3:06
boruu6-May-05 3:06 
GeneralRe: number to alpha Pin
keith maddox6-May-05 6:15
keith maddox6-May-05 6:15 
GeneralRe: number to alpha Pin
DavidNohejl6-May-05 6:17
DavidNohejl6-May-05 6:17 
Questionhow to set CustomDocumentproperyies for Word in C# Pin
Member 18788066-May-05 1:04
Member 18788066-May-05 1:04 
AnswerRe: how to set CustomDocumentproperyies for Word in C# Pin
Jon Merrifield6-May-05 1:42
Jon Merrifield6-May-05 1:42 
GeneralRe: how to set CustomDocumentproperyies for Word in C# Pin
Member 18788066-May-05 1:56
Member 18788066-May-05 1:56 
GeneralRe: how to set CustomDocumentproperyies for Word in C# Pin
Jon Merrifield6-May-05 2:06
Jon Merrifield6-May-05 2:06 
GeneralRe: how to set CustomDocumentproperyies for Word in C# Pin
Member 18788066-May-05 3:16
Member 18788066-May-05 3:16 
GeneralRe: how to set CustomDocumentproperyies for Word in C# Pin
Jon Merrifield6-May-05 4:20
Jon Merrifield6-May-05 4:20 
Generalerror while using SAP.Net Connector Pin
Dilip Vyas6-May-05 0:45
Dilip Vyas6-May-05 0:45 
GeneralRe: error while using SAP.Net Connector Pin
Sebastian Schneider6-May-05 2:59
Sebastian Schneider6-May-05 2:59 
GeneralDynamic Dll Pin
Shashidharreddy6-May-05 0:10
Shashidharreddy6-May-05 0:10 
GeneralRe: Dynamic Dll Pin
Dan_P6-May-05 0:14
Dan_P6-May-05 0:14 
GeneralRe: Dynamic Dll Pin
Anonymous6-May-05 4:13
Anonymous6-May-05 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.