Solved

Passing information between two program instances

Posted on 2004-08-24
14
251 Views
Last Modified: 2010-04-15
I am developing a command line program which needs to get information from command line switches every now and then. So instead of starting a new instance when the program is executed, it will pass an event or message to the already running program telling it what it would do.

For example:

First program.exe is executed and the program is started. Then we run program.exe /cleardatabase and the running process receives this
request and acts accordingly.

It's the messaging part that's troublesome. I've looked into the windows API(GetMessage, SendMessage) but I haven't been able to create a C# solution, and I don't think there are stuff in the .NET framework for this..

It's pretty normal for a program to act this way, but I haven't been able to find a tutorial on the matter..

What I need is a C# solution that checks if the program is running, if it is, send a message containing a string to it which the main program then can interpret. Thanks!
0
Comment
Question by:c03dn
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 11883286
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11888420
wow there are a tons of ways of doing this ...

1) Do you only need it to work on the same machine ?
2) Do you need gaurenteed delivery of messages (even if first program is hung etc)
0
 
LVL 5

Accepted Solution

by:
jjacksn earned 500 total points
ID: 11888615
c03dn, if its just something quick and dirty, you can easily send windows messages in .NET.  


to listen for them, override WndProc

protected override void WndProc(ref Message aMessage)
{
      if(aMessage.Msg == your message)
      {
            //do something
      }
else
{
base.WndProc(ref aMessage);
}

to send them, just use:

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int command, int lPARAM, int WParam);
            
0
 
LVL 22

Expert Comment

by:cookre
ID: 11888719
jjacksn - I had no idea you could get to WndProc() like that.  Wunnerful.

(and all this time I've been doing all the old stuff (InitInstance() CreateWindow()... to a hidden window just to get a message pump)
 Live and learn.  Tnx...

c03dn - this is the way to go.  Make up your own message number above the value of 0x0400 (WM_USER).
0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11888888
cookre, if it makes you feel better, I had no idea about MSMQ and found that very interesting reading.  Although it is totally overkill for a small app.  

c03dn - FYI, at the tyop of WinProc you should filter out everything

if(aMessage.Msg < 0x0400)
{
base.WndProc(ref aMessage)

}
else
{

}

otherwise debugging is a royal pain because the focus message gets sent everytime you step through the function in the debugger.  its also just good practice.  
0
 

Author Comment

by:c03dn
ID: 11913803
I'm looking into the windows messages now. Is it possible to send a string with sendmessage or just a code? I am sorry if my questions are newbieish, it's my first shot at this..
0
 

Author Comment

by:c03dn
ID: 11913978
Also, how do I use the WndProc in a console environment? It complains about "Message" not being found..

I've found examples on msdn, but they're all of window forms..
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:cookre
ID: 11914189
The standard window message adjuncts, wParam and lParam, are passed along in the aMessage object:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsmessagememberstopic.asp?frame=true ,

but the standard windows message has no built-in mechanism for passing arbitrarily sized info.
0
 
LVL 22

Expert Comment

by:cookre
ID: 11914214
0
 

Author Comment

by:c03dn
ID: 11915594
Got it working now.

Now I'll just have to find out how to get a window handle with just knowing the process name.. The program is going to be used to maintain focus on certain programs. Thanks again!
0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11917158
c03dn, you're in luck:

Process[] procs = Process.GetProcesses();

int len = procs.GetLength(0);
for (int i=0; i<len; i++)
{
               Process p = procs[i];
               if (p.Id!=0)
                        {
                              if(iCurrentProcessId == p.Id) continue; //that's this program.

                              if(p.ProcessName == "your name")
                              {
                                    p.MainWindowHandle; //do something with that
                                    
                              }
                        }
                  }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11917182
0
 
LVL 22

Expert Comment

by:cookre
ID: 11917261
Or, if both programs are your code, message the pid back.
0
 
LVL 5

Expert Comment

by:jjacksn
ID: 11918290
that's cool, I didn't know that.  when are you allowed to see processes on another machine?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 56
c# string handling 1 27
C# application error "Parameter Is Not Valid". 3 80
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 31
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now