Calling a WinForms app from another app?

OK. So I want to implement a new feature in VB2004 that I'm not ready to talk about yet. You may be able to infer what I want to do from the situation I'm about to describe, but I won't give any specifics until the next beta release. Here's what I need to do:

Inside an assembly, which will be loaded and executed by App#1, I need to be able to check and see if VB2004 is running. If it is, I need to be able to access the running instance, pass data to the editor, and activate the form. Does anyone have any frickin clue how to do this? I have no idea where to even begin, but I really want this feature in the next drop. Any help would be appreciated.

-Robert

4 Comments

  • Back in the dark days I'd have used an Event to check if it was running and a bit of shared memory to throw data/instructions at it.

    In the brave new world, I'm not so sure!

    I presume C# (are you working in C#?) still has events, so that takes care of the 'are you there' issue.



    Passing it data. Sounds like you want VB2004 to listen out for a web service request, or is that too heavyweight for your needs? if you don't want to go the whole hog a simple socket listen (at vb2004) for a blob of data would be pretty quick/easy. would also mean it *could* work remotely if you dropped the event idea used the socket on a known ip address to figure out if vb2004 was there..

  • Whenever you need to communicate across appDomains, it always boils down to remoting. VB2004 should set itself up as a remoting server, App#x is the client. There are lots of samples, but if this your first time with remoting be prepared for some ramp up time (Which typically includes subtle-but-deadly bugs relating to lease timeouts, security, thread affinity, etc)

  • Two things are needed to do what you want. One is a Mutex the other is remoting. I have used this to handle double clicking on an application's data file in explorer. If the app isn't running, you want the app to start and load the file. If it is running, you want to load the app in the existing instance of the app. Basically, when the app first starts, you grab a mutex and setup the remoting server. If you can't grab the mutex, you know the app is already running and you pass it a command to open a file through remoting. Here's an example of what I'm talking about. It isn't exactly what you are trying to do, but you get the idea:



    static void Main(string[] args) {

    Mutex _mutex;

    try{

    _mutex = new Mutex(false, "someuniquestring");

    if(_mutex.WaitOne(TimeSpan.Zero, false)){

    // if we get the mutex that means there is

    // no other copy of our app running,

    // so set up the remoting server and run the app

    SETUPREMOTINGSERVER();

    Application.Run(new FormMain());

    }

    else{

    // can't get the mutex, so try to

    // process commands sent in

    // set the mutex to null so we don't try

    // to release it later

    _mutex = null;

    SENDCOMMANDSTOREMOTINGSERVER();



    }

    }

    catch(Exception ex){

    // handle your exception

    }

    finally{

    if(_mutex != null){

    _mutex.ReleaseMutex();

    _mutex.Close();

    _mutex = null;

    }

    }

    }

  • I took a look at some of the shared memory stuff and it looks cool. One question though, how does the receiving app know that the data is there? I think Robert needs to know when the data is being sent and I don't see a way for these shared memory implementation to notify the form that it needs to process data. So, maybe DDE or remoting are the only ways to do it? I suppose you could send a message with DDE and then go get the data from the shared memory if it is something too large to be passing around with DDE.

Comments have been disabled for this content.