views:

336

answers:

5

I have a system tray icon that receives incoming phone calls. When a call comes in, I want to send the phone number into a web app that does stuff on that phone number. We can launch the web app with the phone number in a query string, but if I launch a new window for every call, it will junk up the user's computer with lots of browser instances, would not preserve the location/size the user moved the browser window to, and would take longer than just refreshing the page. Instead, I'd like to make the Win32 app re-use the same IE browser window and just send the web app the new phone number every time a new call comes in. I'm envisioning somehow sending a Windows message, or somehow instructing the IE browser to run a certain javascript event with some data? I can see why doing the reverse (javascript out to Win32) would be a security issue, but this would be just sending a message from Win32 to javascript.

So I'm specifically NOT asking how to do what's been answered in this question: How to Interact Between Web App and Windows Form Application

That user was asking how to launch a Win32 app from Javascript and pass data to the win32 app. Roughly, I need to do the opposite. I need to send data from a Win32 app into a running javascript program.

Note also that I'm not asking how to launch one IE window with arguments to Javascript one time; I can easily do that with query strings. What I'm asking is how can I / is it possible to pass data from a running Win32 app outside the browser to a running Javascript app inside a browser window.

A: 

You should be able to find the handle of the IE window, and then send messages (keypresses perhaps) to it. Use a bit of javascript to capture all keypress activity and you have a very simple method to transfer information.

It won't be easy (the devil is in the details - issues of focus, etc) but it should be possible.

Adam Davis
Thanks. Not to split hairs but if that is the only way, my boss will probably veto sending keypresses as too much of a hack. Was hoping there might be a "standard" way. I appreciate your answer, but I'm hoping to see some more answers.
Oh yes, this is certainly a hack. Most other methods aren't going to be much different - they'll all send messages of one sort or another.
Adam Davis
A: 

Since you can send the phone number to the site through a querystring, all you really need to do is tell IE to navigate to a URL of your choosing.

To that end, you can use what is in this KB article to help you find the instance of IE you want to connect to.

http://support.microsoft.com/kb/299356/de

Granted, its in Visual J++ of all things, but since you are interacting through COM Automation, the calls should be easy to translate.

Once you have the instance of IE (in the form of an IWebBrowser2 interface implementation) you can simply call the Navigate or Navigate2 method with the URL you need (with the phone number in the query string of course).

casperOne
A: 

What you need is a way to How To Connect to a Running Instance of Internet Explorer (caveat: this one is in Java, but you should be able to translate it to C# quite easy).

Or you could enumerate the top-level windows to find a particular instance of IE and then Get IHTMLDocument2 from a HWND (this one is in C++, so you might need to do some Win32 interop; you can find all necessary declarations on PInvoke.net).

If you search on your friendly local search engine for How to connect to a running instance of Internet Explorer, you will find a lot more info.

Once you get to the document, you can either invoke the JavaScript function through the document scripting interface, or you can just navigate it to your page and pass the phone number as a parameter.

Franci Penov
Thanks; your answer was more detailed but I accepted casperOne's answer because redirecting the browser with a new query string is a simpler solution that still would work; we ended up doing something completely different from both.
Lol, did you bother to look at my first link? It points to exactly the same KB article he pointed as well. :-)
Franci Penov
A: 

you could possibly get around the security and other issues with this method by using a web service on the web server, and have the win32 app update the web service and have the web page poll the same web service every however many seconds. Then you the the option of mapping that number to a database and getting additional information.

The only draw back is that instead of being immediate, there is a delay to get the information displayed in the browser.

Roy Rico
A: 

How about you write an active-x control that you create in the browser using Javascript. This is effectively an Explorer browser plugin. Same idea for Mozilla, etc., except they use a different plug-in structure. This lets you support other browsers in the future.

The control can talk to your win32 app using a pipe or a socket or whatever type of inter-process communications and is then accessible as a Javascript object.

billmcc