Click here to Skip to main content
16,005,169 members
Home / Discussions / C#
   

C#

 
GeneralRe: Xml in C# Pin
Heath Stewart25-Mar-04 3:41
protectorHeath Stewart25-Mar-04 3:41 
GeneralTrapping abnormal process termination Pin
Bruce Duncan18-Mar-04 20:11
Bruce Duncan18-Mar-04 20:11 
GeneralRe: Trapping abnormal process termination Pin
Rhys__66618-Mar-04 22:51
Rhys__66618-Mar-04 22:51 
GeneralRe: Trapping abnormal process termination Pin
Bruce Duncan19-Mar-04 3:02
Bruce Duncan19-Mar-04 3:02 
GeneralRe: Trapping abnormal process termination Pin
Heath Stewart19-Mar-04 3:31
protectorHeath Stewart19-Mar-04 3:31 
GeneralRe: Trapping abnormal process termination Pin
Heath Stewart19-Mar-04 3:45
protectorHeath Stewart19-Mar-04 3:45 
Generalgrab a combo in another app... Pin
Jumpin' Jeff18-Mar-04 14:59
Jumpin' Jeff18-Mar-04 14:59 
GeneralRe: grab a combo in another app... Pin
Heath Stewart19-Mar-04 3:29
protectorHeath Stewart19-Mar-04 3:29 
You can P/Invoke the FindWindowEx API to first find the application and then to find the edit box. Once you get the HWND back (it'll be an IntPtr in managed code), you could also P/Invoke SendMessage to send WM_SETTEXT to it with the lParam parameter set to a string reference:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class Test
{
  static void Main(string[] args)
  {
    if (args.Length < 1) return;

    string s = args.Length > 1 ? args[1] : "Hello, world!";

    IntPtr ptr = FindWindowEx(IntPtr.Zero, IntPtr.Zero,
      null, args[0]);
    if (ptr != IntPtr.Zero)
    {
      ptr = FindWindowEx(ptr, IntPtr.Zero, "Edit", null);
      if (ptr != IntPtr.Zero)
        SendMessage(ptr, WM_SETTEXT, IntPtr.Zero, s);
    }
  }

  private const int WM_SETTEXT = 0x000c;
  
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  private static extern IntPtr FindWindowEx(
    IntPtr hWndParent,
    IntPtr hWndChildAfter,
    [MarshalAs(UnmanagedType.LPTStr)] string lpszClass,
    [MarshalAs(UnmanagedType.LPTStr)] string lpszWindow);
    
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  private static extern int SendMessage(
    IntPtr hWnd,
    [MarshalAs(UnmanagedType.U4)] int msg,
    IntPtr wParam,
    [MarshalAs(UnmanagedType.LPTStr)] string text);
}
The one thing you will have to keep in mind is that many times a control is nested. For example, the code above worked on "Calculator" (calc.exe) but not on "Microsoft Internet Explorer" (iexplore.exe) because the first "Edit" control is nested in a "ComboBox" which is nested in a "ReBarWindow32". You could easily modify the code to traverse the children, though. See the doucmentation for the FindWindowEx function for more information.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: grab a combo in another app... Pin
Jumpin' Jeff19-Mar-04 8:18
Jumpin' Jeff19-Mar-04 8:18 
GeneralTyped Datasets Pin
Kant18-Mar-04 12:55
Kant18-Mar-04 12:55 
GeneralRe: Typed Datasets Pin
Heath Stewart18-Mar-04 13:31
protectorHeath Stewart18-Mar-04 13:31 
GeneralMi Web Page en cache !!! Pin
ElJerry18-Mar-04 12:17
ElJerry18-Mar-04 12:17 
GeneralRe: Mi Web Page en cache !!! Pin
Heath Stewart18-Mar-04 13:34
protectorHeath Stewart18-Mar-04 13:34 
GeneralI've a little doubt about decimal fraction !! Pin
ElJerry18-Mar-04 12:07
ElJerry18-Mar-04 12:07 
GeneralRe: I've a little doubt about decimal fraction !! Pin
Christian Graus18-Mar-04 12:36
protectorChristian Graus18-Mar-04 12:36 
GeneralRe: I've a little doubt about decimal fraction !! Pin
Werdna18-Mar-04 16:13
Werdna18-Mar-04 16:13 
GeneralaxWebBrowser DocumentComplete Pin
Jasper4C#18-Mar-04 10:57
Jasper4C#18-Mar-04 10:57 
GeneralRe: axWebBrowser DocumentComplete Pin
John Fisher18-Mar-04 11:35
John Fisher18-Mar-04 11:35 
GeneralRe: axWebBrowser DocumentComplete Pin
Jasper4C#25-Mar-04 11:05
Jasper4C#25-Mar-04 11:05 
GeneralRe: axWebBrowser DocumentComplete Pin
John Fisher25-Mar-04 13:11
John Fisher25-Mar-04 13:11 
GeneralRe: axWebBrowser DocumentComplete Pin
Jasper4C#25-Mar-04 22:54
Jasper4C#25-Mar-04 22:54 
GeneralRe: axWebBrowser DocumentComplete Pin
John Fisher26-Mar-04 5:20
John Fisher26-Mar-04 5:20 
GeneralRe: axWebBrowser DocumentComplete Pin
Jasper4C#26-Mar-04 7:12
Jasper4C#26-Mar-04 7:12 
GeneralRe: axWebBrowser DocumentComplete Pin
John Fisher26-Mar-04 15:08
John Fisher26-Mar-04 15:08 
GeneralControl Confussion Pin
Matthew Hazlett18-Mar-04 10:27
Matthew Hazlett18-Mar-04 10:27 

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.