473,787 Members | 2,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 RegisterWindowM essage 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\MyA pp.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 RegisterWindowM essage 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 6775
> Now, however, Windows is starting a second instance along these lines;

"C:\Program Files\MyApp\MyA pp.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 RegisterWindowM essage 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\MyA pp.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 RegisterWindowM essage 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
34378
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
2
15128
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 the outer function, the string.format sets the string-value "System.Object" at the position of {0}. How can i get it work, so that all my values, passed to the outer
17
9393
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 commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of SqlConnection, so when object A closes its connection, it remains open for object B's copy. Is this
6
3259
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 query-string flag is used to indicate to the page whether it should instantiate a new instance of the object or access an existing instance from the calling page. On the both pages I have a property of the page which is an instance of this custom...
3
8259
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 postmessage(hwnd,WM_KEYDOWN,vk_control,code) postmessage(hwnd,WM_KEYDOWN,"A",code) postmessage(hwnd,WM_KEYUP,"A",code) postmessage(hwnd,WM_KEYUP,vk_control,code)
3
5938
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 installer. To improve on this, I want the installer to shut down any running instances of my application when it does the installation. To do this, I have implemented the following function to kill any instances I find (yeah, I have the pinvoke for...
2
2184
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 IntPtr. VB2005 won't let me do this: CType(Me, IntPtr) How can I pass Me as the WPARAM arg? Bill
9
1769
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 Hello()
4
2815
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 page. I have a response.redirect("secondpage") in a button click event. Thanks. -- Paul G Software engineer.
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.