irondsd
irondsd

Reputation: 1240

How can I call from one app to another simply?

I'm new to C# and programming. I have 2 apps with different processes and I need to be able to call a method of 1 app from another as simply as possible. I don't need to exchange data or anything else, just need to call a method. I googled about it. I know there are many different ways like pipes, but I need the simplest one. I also found I can send message like this:

const uint WM_COPY = 0x301;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, uint uMsg, int wParam, int lParam);

Process p = Process.GetProcessesByName("appname").FirstOrDefault();
if(p != null)
{
    IntPtr hWnd = p.MainWindowHandle;
    SendMessage(hWnd, WM_COPY , 0, 0);
}

And I was able to receive the message with this code:

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_COPY)
    {
        //my code
    }
    else
    {
        base.WndProc(ref m);
    }
}

But there is a problem. App that should receive the message don't have a form, it's just a process. I've used this.Hide(), so MainWindowHandle won't work.

Is it possible to send a message to an app with a hidden form? Or maybe there is a better way to do call a method on the second app? Thanks.

Upvotes: 1

Views: 169

Answers (1)

zmbq
zmbq

Reputation: 39013

Well, if all you need to do is call a single method with no parameters at all, the easiest would be to create a named Mutex, set it from the calling process and check it from the receiving process.

If you need something more elaborate, take a look at WCF .

Upvotes: 1

Related Questions