473,383 Members | 1,805 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,383 software developers and data experts.

Passing a string from one instance of C# app to another using PostMessage

Hi all,

I am working on my single instance application that now also has a URL
type associated with it, according to the Asynchronous Pluggable
Protocols information from MSDN. Now, if my application is not already
started, everything works fine. The APP setup starts my program with the
URL as the command line argument, and it displays the proper information
to the user. This works great and was a pleasure to program.

However, my challenge comes when the application is already running.
Thanks to some tips from Nicholas Paldino here on the group, I
originally handled this by using RegisterWindowMessage to obtain a
unique msg id, then I PostMessage'd it to HWND_BROADCAST in order to
tell the existing instance to show itself. Then the second instance
would simply exit.

Now, however, Windows is starting a second instance along these lines;

"C:\Program Files\MyApp\MyApp.exe" "MyProtocol://Heyheyhey/gunk"

What I need is for the second instance of the application to pass
"MyProtocol://Heyheyhey/gunk" to the existing instance before it dies.
My first idea was simply to do another RegisterWindowMessage and pass
the string as one of the parameters, but it seems after intense googling
that it's not going to be that easy.

What would you guys say is The Right Way to do something like this? My
application is in C# with VS 2003 and .Net 1.1. If possible, I would
like to make this as simple as I can, but I realize I might have to give
up that comfort to get something that actually works. :)

Any advice will be accepted with gratitude!

Keep up the good work!

Rune
Jan 16 '06 #1
3 6745
> Now, however, Windows is starting a second instance along these lines;

"C:\Program Files\MyApp\MyApp.exe" "MyProtocol://Heyheyhey/gunk"

What I need is for the second instance of the application to pass
"MyProtocol://Heyheyhey/gunk" to the existing instance before it dies. My
first idea was simply to do another RegisterWindowMessage and pass the
string as one of the parameters, but it seems after intense googling that
it's not going to be that easy.

What would you guys say is The Right Way to do something like this? My
application is in C# with VS 2003 and .Net 1.1. If possible, I would like
to make this as simple as I can, but I realize I might have to give up
that comfort to get something that actually works. :)


yes, indeed, this is not as easy as it looks like because the memory is
addressed virtually (the same virtual address in two separate processes
points to different place in physical memory).
however, since you do know how to pass messages between instances, one of
the simplest solutions (altough not very efficient with really long
messages) would be to pass the string char-by-char since passing integer
values IS easy (you just pass the integer value as one of the message
parameters). the actual code would be then:

// pass consecutive letters
foreach ( letter c in string_to_pass )
PostMessage( where, whatmessage, c, 0 );
// indicate the end of the string
PostMessage( where, whatmessage, 0, 0 );

there are of course also other possibilities that for example involve
allocating/freeing global memory but I do not think you need anything like
that for your simple task.

Regards,
Wiktor Zychla

Jan 16 '06 #2

Wiktor Zychla [C# MVP] wrote:
Now, however, Windows is starting a second instance along these lines;

"C:\Program Files\MyApp\MyApp.exe" "MyProtocol://Heyheyhey/gunk"

What I need is for the second instance of the application to pass
"MyProtocol://Heyheyhey/gunk" to the existing instance before it dies. My
first idea was simply to do another RegisterWindowMessage and pass the
string as one of the parameters, but it seems after intense googling that
it's not going to be that easy.

What would you guys say is The Right Way to do something like this? My
application is in C# with VS 2003 and .Net 1.1. If possible, I would like
to make this as simple as I can, but I realize I might have to give up
that comfort to get something that actually works. :)
yes, indeed, this is not as easy as it looks like because the memory is
addressed virtually (the same virtual address in two separate processes
points to different place in physical memory).
however, since you do know how to pass messages between instances, one of
the simplest solutions (altough not very efficient with really long
messages) would be to pass the string char-by-char since passing integer
values IS easy (you just pass the integer value as one of the message
parameters). the actual code would be then:

// pass consecutive letters
foreach ( letter c in string_to_pass )
PostMessage( where, whatmessage, c, 0 );
// indicate the end of the string
PostMessage( where, whatmessage, 0, 0 );

there are of course also other possibilities that for example involve
allocating/freeing global memory but I do not think you need anything like
that for your simple task.


Ack. I can't imagine doing something like that :)

The two easy possibilities (easy being relative in Windows) are:

1) A named pipe
2) Create a private data type on the clipboard and send a message
indicating it is there.

Oh, you could also just write it to a file :)

Matt

Regards,
Wiktor Zychla


Jan 16 '06 #3
Rune,
What would you guys say is The Right Way to do something like this? My
application is in C# with VS 2003 and .Net 1.1. If possible, I would like
to make this as simple as I can, but I realize I might have to give up
that comfort to get something that actually works. :)


See
http://www.vbaccelerator.com/home/NE...on/article.asp
for an example of using WM_COPYDATA to communicate between processes. I am
currently using this technique in production code and it works like a charm.

--
Colin Neller
http://www.colinneller.com/blog
Jan 17 '06 #4

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
3
by: Str Noetika | last post by:
Hi, How can I send ctrl keys (like ctrl A, ctrl C) from a VB application to another existing application (like word, excel...) ? I tried to send postmessages like...
3
by: Rune Jacobsen | last post by:
Hey all, Maybe someone knows this problem; I have a C# WinForms app that every now and then has a new version that I deploy to my users using an installation script written with the NSIS...
2
by: swartzbill2000 | last post by:
I want to PostMessage from a class instance to its containing form. I want the WPARAM to point to the class instance. I am using the declaration of PostMessage from www.pinvoke.net. WPARAM is an...
9
by: itarizin | last post by:
Today I found as ignorance owe me.. Let me explain what I'm tring to do and fail (fail: in my needs) For example, I've my stupid class: public class Hello { private int x = 0; public...
4
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi, just wondering if anyone can provide a brief example of passing parameters from one webpage to another, C# VS2005? I need to pass several selected values of dropdown list boxes to a secondary...
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.