473,699 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I just want to send a string as a message to a .NET program :(

Right, I've got a 2 c# programs here. Lets call them A and B. My aim is
to send a simple string from B to A.

A is always running. I've overridden the WndProc method to give me
messages that are sent to it. B is a program that loads, sends a
message and then quits. Let me give you the code to B (bits are missed
out, but I've got the important stuff there):

private const uint WM_USER_SENDTEX T = 0x8001;
[DllImport("user 32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(Int Ptr hWnd, uint Msg, IntPtr
wParam, ref StringBuilder lParam);
public arghandler(stri ng assemblyname,st ring[] args)
{
IntPtr hWnd = FindWindow(null , "ProgramA") ;
if (hWnd!=IntPtr.Z ero)
{
StringBuilder sb = new StringBuilder(" hi there");
IntPtr res =
SendMessage(hWn d,WM_USER_SENDT EXT,IntPtr.Zero ,ref sb);
bool rah = SetForegroundWi ndow(hWnd);
}
this.Close();
}
So there we go. I send a reference to a stringbuilder that contains the
string that I want to send "hi there".

Here's the relevant bit of A's WndProc:

case WM_USER_SENDTEX T:
StringBuilder sb = new StringBuilder() ;
try
{
sb =
(StringBuilder) Marshal.PtrToSt ructure(m.LPara m,typeof(String Builder));
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
MessageBox.Show ("Hey! Got a message here
"+sb.ToString() );
break;
Now, when I run A in debug mode and run B, I can always catch the code
getting to this case. I step it through, and it throws an exception at
the PtrToStructure line:

_message "The specified structure must be blittable or have
layout information." string
Ok, so some reading about tells me that a stringbuilder is not a
blittable structure. So lets try with an int. I send a ref int and try
to read it as an int (using typeof(int) and everything). This time, the
int I read out is the same as the int value of lParam. So it's reading
itself as an int and not the int object I put in in program B (42,
btw). Ok, so lets send an int and not a ref int.

_message "Object reference not set to an instance of an object."
string

WTF? What the hell is null here? certainly not lParam, that's 1274539
or something. Why can't I send even a simple int from one .NET program
to another?

Anyone got any ideas?

P.S. I had one idea but not been able to test this one out - is there a
danger that B is sending a pointer to the memory for A to get hold of,
but then quitting before A can do anything with it and therefore
de-allocating it's memory making the pointer not valid?

Jul 26 '06 #1
3 2663
I am not exactly sure what you are doing, but if I were you, I will use
sockets to send those message. This case, if you want to scale program A
and B on different machines, it will still work.

gr****@gmail.co m wrote:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is
to send a simple string from B to A.

A is always running. I've overridden the WndProc method to give me
messages that are sent to it. B is a program that loads, sends a
message and then quits. Let me give you the code to B (bits are missed
out, but I've got the important stuff there):

private const uint WM_USER_SENDTEX T = 0x8001;
[DllImport("user 32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(Int Ptr hWnd, uint Msg, IntPtr
wParam, ref StringBuilder lParam);
public arghandler(stri ng assemblyname,st ring[] args)
{
IntPtr hWnd = FindWindow(null , "ProgramA") ;
if (hWnd!=IntPtr.Z ero)
{
StringBuilder sb = new StringBuilder(" hi there");
IntPtr res =
SendMessage(hWn d,WM_USER_SENDT EXT,IntPtr.Zero ,ref sb);
bool rah = SetForegroundWi ndow(hWnd);
}
this.Close();
}
So there we go. I send a reference to a stringbuilder that contains the
string that I want to send "hi there".

Here's the relevant bit of A's WndProc:

case WM_USER_SENDTEX T:
StringBuilder sb = new StringBuilder() ;
try
{
sb =
(StringBuilder) Marshal.PtrToSt ructure(m.LPara m,typeof(String Builder));
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
MessageBox.Show ("Hey! Got a message here
"+sb.ToString() );
break;
Now, when I run A in debug mode and run B, I can always catch the code
getting to this case. I step it through, and it throws an exception at
the PtrToStructure line:

_message "The specified structure must be blittable or have
layout information." string
Ok, so some reading about tells me that a stringbuilder is not a
blittable structure. So lets try with an int. I send a ref int and try
to read it as an int (using typeof(int) and everything). This time, the
int I read out is the same as the int value of lParam. So it's reading
itself as an int and not the int object I put in in program B (42,
btw). Ok, so lets send an int and not a ref int.

_message "Object reference not set to an instance of an object."
string

WTF? What the hell is null here? certainly not lParam, that's 1274539
or something. Why can't I send even a simple int from one .NET program
to another?

Anyone got any ideas?

P.S. I had one idea but not been able to test this one out - is there a
danger that B is sending a pointer to the memory for A to get hold of,
but then quitting before A can do anything with it and therefore
de-allocating it's memory making the pointer not valid?
Jul 27 '06 #2
A socket is an interesting idea, but in reality, B and A are the same
codebase. B is just another version of A, that knows that A exists and
wants to tell it something before it quits. In this scenario, sockets
would seem a bit of a faff.
Jianwei Sun wrote:
I am not exactly sure what you are doing, but if I were you, I will use
sockets to send those message. This case, if you want to scale program A
and B on different machines, it will still work.

gr****@gmail.co m wrote:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is
to send a simple string from B to A.

A is always running. I've overridden the WndProc method to give me
messages that are sent to it. B is a program that loads, sends a
message and then quits. Let me give you the code to B (bits are missed
out, but I've got the important stuff there):

private const uint WM_USER_SENDTEX T = 0x8001;
[DllImport("user 32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(Int Ptr hWnd, uint Msg, IntPtr
wParam, ref StringBuilder lParam);
public arghandler(stri ng assemblyname,st ring[] args)
{
IntPtr hWnd = FindWindow(null , "ProgramA") ;
if (hWnd!=IntPtr.Z ero)
{
StringBuilder sb = new StringBuilder(" hi there");
IntPtr res =
SendMessage(hWn d,WM_USER_SENDT EXT,IntPtr.Zero ,ref sb);
bool rah = SetForegroundWi ndow(hWnd);
}
this.Close();
}
So there we go. I send a reference to a stringbuilder that contains the
string that I want to send "hi there".

Here's the relevant bit of A's WndProc:

case WM_USER_SENDTEX T:
StringBuilder sb = new StringBuilder() ;
try
{
sb =
(StringBuilder) Marshal.PtrToSt ructure(m.LPara m,typeof(String Builder));
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
MessageBox.Show ("Hey! Got a message here
"+sb.ToString() );
break;
Now, when I run A in debug mode and run B, I can always catch the code
getting to this case. I step it through, and it throws an exception at
the PtrToStructure line:

_message "The specified structure must be blittable or have
layout information." string
Ok, so some reading about tells me that a stringbuilder is not a
blittable structure. So lets try with an int. I send a ref int and try
to read it as an int (using typeof(int) and everything). This time, the
int I read out is the same as the int value of lParam. So it's reading
itself as an int and not the int object I put in in program B (42,
btw). Ok, so lets send an int and not a ref int.

_message "Object reference not set to an instance of an object."
string

WTF? What the hell is null here? certainly not lParam, that's 1274539
or something. Why can't I send even a simple int from one .NET program
to another?

Anyone got any ideas?

P.S. I had one idea but not been able to test this one out - is there a
danger that B is sending a pointer to the memory for A to get hold of,
but then quitting before A can do anything with it and therefore
de-allocating it's memory making the pointer not valid?
Jul 27 '06 #3
"gr****@gmail.c om" wrote:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is
to send a simple string from B to A.
We offer a .NET component FREE for non-commercial use that will enable you
to easily share strings between two programs running on the same PC. It uses
Windows messaging and therefore is very fast and efficient:

http://www.mini-tools.com/goto/comm

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com

Jul 27 '06 #4

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

Similar topics

5
7992
by: Pete Loveall | last post by:
I have a server application that monitors a private local queue for messages. The message sent to it has a label and a response queue defined. It works correctly when the queue is accessed via another program. However, when I attempt to Send a message from an ASP.NET VB page, I get "One or more of the passed properties are invalid." I have tried different combinations of the non-transactional Send() with no success. My Windows\Temp...
4
8063
by: zhimin | last post by:
Hi, I'm writing a program to send large file(100m) through dotnet using TCPListener & TCPClient, I'm sending the file with a ask and response loop: 1. Client send a flag 1 to server indicate it has data send to server. 2. Client send the buffer block size. 3. Client send the actual buffer to the server. 4. Server send a flag 1 to client indicating that the buffer has been successfully receeived. 5. The next loop until all data of the...
6
3254
by: John J. Hughes II | last post by:
I have a service that needs to send e-mail alerts. I have been attempting to use the System.Net.Mail function from .NET but this seems to require the IIS be installed and running. Since some of my customers are not really happy with having the IIS installed, not being used except to send e-mail this is becoming a problem. I have also tried using a little program from code project for sending e-mail which works great on older servers...
6
2385
by: Becker | last post by:
I have a program that I can drag and drop files to. I would also like to add this to the sendto menu so that users could right click a file and choose to "send to" my program. My only problem is I have no idea what event to handle in my program to accept the "send to" action. Any help, suggested reading or examples is greatly appreciated! Thanks, Ben
7
2166
by: GeorgeAtkins | last post by:
I want to create a web-based form or page that consists of a series of formatted questions and answers. The form will resemble an existing paper form. When the form is filled in, I want the user to submit the form via e-mail and have the complete form with answers sent, not just the data. That is, the recipient should be able to open the attachment and see (and print) the complete, formatted form. It seems to me that solutions simply...
8
1533
by: Jimbo | last post by:
Hello I am currently designing an internal ordering system for IT equipment. I am designing it in ASP.NET (vb) using Visual Studio 2003 and using Microsoft SQL Server I have got the system to add the order into the database and assign it a unique ID ("EITReqCode"), and made it e-mail the it purchaser via SMTP automatically with a link to the "Authorisation" page where they
3
4301
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3161
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
1
2719
by: zivon | last post by:
now for the bigger problam :) I know you pepole hate using OE for sending emails, but its user friendly and its needed in this case... I found on this forum, a code that sends email using OE with attachments. it works, but with two problams... first, its directly sends the email, without opening the OE "editing mode" of the email, like when you use the DoCmd.SendObject. this is the smaller problams though... the bigger problam...
0
8689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9178
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
8916
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
7752
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
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
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3058
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
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
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.