473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Passing command line parameters between instances

I have an application that takes a few command line parameters. As
recommended by others in this group, I'm using a named mutex to ensure
that only one instance of the application is running. My question is
how to elegantly pass a command line parameter from Instance_B to
Instance_A where Instance_A was running prior to Instance_B.

For example, the user can launch the program by passing a file name as
a command line argument. The program will display the file after the
program launches. If a second instance is launched, I want the first
instance to display the file and the second instance to close.

How I'm doing it now is clumsy. When Instance_B starts and notices
Instance_A is already running, I write a temp file to the user's area
that contains the name of the user's file to open. Instance_A polls
the temp file and opens the user's file when temp file is created.
Ugly, but it works.

Have any of you done something like this before? I can think of a few
other solutions but they are overly complex.

Thank you,
Bill

Aug 22 '07 #1
2 4221
There are a few ways to do this. The problem of course, is each process has
its own address space, so you can't directly pass data between them

You can use sockets or some other IPC mechanism. But that seems like a lot
of trouble for passing some arguments.

The way I normally do it is to actually use the WM_COPYDATA message. It
requires some marshalling of the data, but I think it's a lot easier than
setting up sockets and all the error handlign and stuff that come with doing
that.

Basically, what you do is figure out a way to pack your arguments into a
byte[] array. Lay them out however you want.

You'll need these two definitions:

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

[StructLayout(LayoutKind.Sequential)]
public struct COPYDATA
{
public uint dwData;
public uint cbData;
public IntPtr lpData;
}
Then for sending from the second app to the first, I first go through the
process list (Process.GetProcesses()) and find the first instance and get
its main window handle.

I also have a class calld ArgumentInfo which simply contains my parsed
arguments in properties and fields and has two methods, CreateByteArray()
and ParseByteArray(). CreateByteArray() simply creates a byte array from the
arguments in the fields and a constructor override that takes byte[] and
populates the fields based on that.

So to send the message from the second app to the first, I do:

private static void SendWMData(IntPtr hwnd, ArgumentInfo argInfo)
{
COPYDATA cd = new COPYDATA();
cd.dwData = 0;
cd.cbData = (uint) (argData.Length + 1);
cd.lpData = Marshal.StringToHGlobalAnsi(argData.CreateByteArra y());
IntPtr lpPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cd));

Marshal.StructureToPtr(cd, lpPtr, true);

// Send WM_COPYDATA
SendMessage(hwnd, 0x004A, 0, lpPtr);

Marshal.FreeHGlobal(lpPtr);
}
Then to receive the message in the second app, in the second app's main
window, I override WndProc. You can do a switch/case, but in my case, it's
the only message I handle so it's like this:

protected override void WndProc(ref Message m)
{
if (m.HWnd == Handle && m.Msg == 0x004A)
{
COPYDATA cd = (MainApp.COPYDATA) Marshal.PtrToStructure(m.LParam,
typeof(MainApp.COPYDATA));
string data = Marshal.PtrToStringAnsi(cd.lpData);
string[] jobData = DataSplit(data);
ArgumentInfo newArgs = new ArgumentInfo(jobData);
ProcessArguments(newArgs)
}
base.WndProc (ref m);
}

Anyway, this works pretty well for me and saves me from having to deal with
sockets any more than I already have to...

<wi**************@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
>I have an application that takes a few command line parameters. As
recommended by others in this group, I'm using a named mutex to ensure
that only one instance of the application is running. My question is
how to elegantly pass a command line parameter from Instance_B to
Instance_A where Instance_A was running prior to Instance_B.

For example, the user can launch the program by passing a file name as
a command line argument. The program will display the file after the
program launches. If a second instance is launched, I want the first
instance to display the file and the second instance to close.

How I'm doing it now is clumsy. When Instance_B starts and notices
Instance_A is already running, I write a temp file to the user's area
that contains the name of the user's file to open. Instance_A polls
the temp file and opens the user's file when temp file is created.
Ugly, but it works.

Have any of you done something like this before? I can think of a few
other solutions but they are overly complex.

Thank you,
Bill

Aug 22 '07 #2
That worked well. Thank you!!!

Aug 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Leslie | last post by:
I am porting a script from Korn Shell to python and want to pass named parameters like -JOB 123456 -DIR mydir I can get it to work passing --JOB and --DIR but not -JOB and -DIR Any ideas? ...
1
by: Casey Bralla | last post by:
I've got a python cgi-bin application which produces an apache web page. I want to pass arguments to it on the URL line, but the parameters are not getting passed along to python properly. I've...
0
by: Keith Wall | last post by:
Is there any way to pass mysql user variables on the command line? I'd like to do this to enable me to parameterised a mysql script. I'd like to be able to use a command line such as: mysql...
1
by: Michael Hetrick | last post by:
How would I pass parameters to a console application? I would like to do something like this: consoleapp.exe /o \\fileshare\origindirectory /d \\fileshare\destinationdirectory I'm not sure...
5
by: jlea | last post by:
I'm trying to pass a filename, obtained with using the fileName property from the OpenFileDialog, as a application parameter in Process.StartInfo.Arguments and run a MFC/C++ application using the...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and...
6
by: John | last post by:
Hi How can I process parameters passed to my vb.net? Also, is it possible to use the desktop shortcut created by setup to pass parameters or do I need to create my own desktop shortcut? ...
7
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
4
by: cjt22 | last post by:
Hi there. I just wondered whether anyone could recommend the correct way I should be passing command line parameters into my program. I am currently using the following code: def main(argv =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.