Well, for example I have some simple console application. My purpose is to generate some report and represent it to the user in some external application such as notepad or a web browser.
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
static void Main()
{
OpenNotepadAndInputTextInIt("Message in notepad");
OpenHtmlFileInBrowser(@"c:\temp\test.html");
}
private static string GetDefaultBrowserPath()
{
string key = @"HTTP\shell\open\command";
using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false))
{
return ((string)registrykey.GetValue(null, null)).Split('"')[1];
}
}
private static void OpenHtmlFileInBrowser(string localHtmlFilePathOnMyPc)
{
Process browser = Process.Start(new ProcessStartInfo(GetDefaultBrowserPath(), string.Format("file:///{0}", localHtmlFilePathOnMyPc)));
browser.WaitForInputIdle();
}
private static void OpenNotepadAndInputTextInIt(string textToInputInNotepad)
{
Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
notepad.WaitForInputIdle();
if(notepad != null)
{
IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, textToInputInNotepad);
}
}
}
This solution works fine, But as you can see I have two methods; GetDefaultBrowserPath()
and GetDefaultBrowserPath(string localHtmlFilePathInMyPc)
. The first one passes message string directly to notepad window, but the second one needs to create a file, some html
page, and than pass this html file as parameter for the web browser. This is kind of a slow solution. I want to generate an html
string report and pass it directly to a web browser without creating intermediate html files.