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

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_SENDTEXT = 0x8001;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, ref StringBuilder lParam);
public arghandler(string assemblyname,string[] args)
{
IntPtr hWnd = FindWindow(null, "ProgramA");
if (hWnd!=IntPtr.Zero)
{
StringBuilder sb = new StringBuilder("hi there");
IntPtr res =
SendMessage(hWnd,WM_USER_SENDTEXT,IntPtr.Zero,ref sb);
bool rah = SetForegroundWindow(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_SENDTEXT:
StringBuilder sb = new StringBuilder();
try
{
sb =
(StringBuilder)Marshal.PtrToStructure(m.LParam,typ eof(StringBuilder));
}
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 2647
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.com 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_SENDTEXT = 0x8001;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, ref StringBuilder lParam);
public arghandler(string assemblyname,string[] args)
{
IntPtr hWnd = FindWindow(null, "ProgramA");
if (hWnd!=IntPtr.Zero)
{
StringBuilder sb = new StringBuilder("hi there");
IntPtr res =
SendMessage(hWnd,WM_USER_SENDTEXT,IntPtr.Zero,ref sb);
bool rah = SetForegroundWindow(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_SENDTEXT:
StringBuilder sb = new StringBuilder();
try
{
sb =
(StringBuilder)Marshal.PtrToStructure(m.LParam,typ eof(StringBuilder));
}
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.com 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_SENDTEXT = 0x8001;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, ref StringBuilder lParam);
public arghandler(string assemblyname,string[] args)
{
IntPtr hWnd = FindWindow(null, "ProgramA");
if (hWnd!=IntPtr.Zero)
{
StringBuilder sb = new StringBuilder("hi there");
IntPtr res =
SendMessage(hWnd,WM_USER_SENDTEXT,IntPtr.Zero,ref sb);
bool rah = SetForegroundWindow(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_SENDTEXT:
StringBuilder sb = new StringBuilder();
try
{
sb =
(StringBuilder)Marshal.PtrToStructure(m.LParam,typ eof(StringBuilder));
}
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.com" 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
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...
4
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...
6
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...
6
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...
7
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...
8
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...
3
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...
0
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...
1
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...
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: 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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.