473,573 Members | 5,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4236
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("user 32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(Int Ptr hWnd, uint msg, int wParam,
IntPtr lParam);

[StructLayout(La youtKind.Sequen tial)]
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.GetPro cesses()) 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(IntP tr hwnd, ArgumentInfo argInfo)
{
COPYDATA cd = new COPYDATA();
cd.dwData = 0;
cd.cbData = (uint) (argData.Length + 1);
cd.lpData = Marshal.StringT oHGlobalAnsi(ar gData.CreateByt eArray());
IntPtr lpPtr = Marshal.AllocHG lobal(Marshal.S izeOf(cd));

Marshal.Structu reToPtr(cd, lpPtr, true);

// Send WM_COPYDATA
SendMessage(hwn d, 0x004A, 0, lpPtr);

Marshal.FreeHGl obal(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.COPYDA TA) Marshal.PtrToSt ructure(m.LPara m,
typeof(MainApp. COPYDATA));
string data = Marshal.PtrToSt ringAnsi(cd.lpD ata);
string[] jobData = DataSplit(data) ;
ArgumentInfo newArgs = new ArgumentInfo(jo bData);
ProcessArgument s(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.comwro te in message
news:11******** **************@ e9g2000prf.goog legroups.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
1569
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? Current code : try:
1
8361
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 been using sys.argv to pick up command line arguments, and it works fine when I call the python program from the command line. Unfortunately, when...
0
2077
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 @uservar1=1 @uservar2=2 < parameterised_script.sql So far the best I can find is to prepare a separate script (parameters.sql) containing only the...
1
18269
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 how to start the console app to read from the command line. Any assistance would be appreciated! Michael Hetrick
5
5823
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 Start method. When I hardcode the application parameter such as "/name=c:\\myFile.txt" all is well in the C++ application. When I use the...
4
2978
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 pass it to the Db and retrieveing data from a stored procedure, but I can't get the hang of parameters. I have a method where I can get the...
6
1477
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? Thanks Regards
7
1534
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. The situation is this: I am writing fairly large console scripts in Python. They have quite a few command-line options, which lead to configuration...
4
5490
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 = None): file1= "directory1" file2 = "directory2"
0
8218
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6435
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.