472,989 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Send message to .Net process

I am searching for a solution of sending messages from not .Net process into
a .Net process written in C#.
I have gone already thru sockets solution and have porblems with it. I am
not a C# coder dont kill :)
I am thinking about something like this:
A remote non .Net process has a window with a name known to .Net program.
Can .Net program somehow process that window using window's title and
extracting a text from it?
What other solutions might be?

Thanks
Aug 15 '08 #1
5 9644
You are looking for IPC:

http://msdn.microsoft.com/en-us/libr...74(VS.85).aspx

Most likely Named Pipes will do what you want.
Aug 15 '08 #2
On Fri, 15 Aug 2008 13:35:25 -0700, Markgoldin <ma*************@yahoo.com>
wrote:
I am searching for a solution of sending messages from not .Net process
into
a .Net process written in C#.
I have gone already thru sockets solution and have porblems with it. I am
not a C# coder dont kill :)
I am thinking about something like this:
A remote non .Net process has a window with a name known to .Net program.
Can .Net program somehow process that window using window's title and
extracting a text from it?
Using unmanaged code, there are ways to identify windows for other
processes and manipulate them. But that would involve information flowing
_from_ the .NET process to the non-.NET process, which is the opposite of
what you've asked.

Using sockets would in fact be a reasonable solution, as would named pipes
as James suggests (my understanding is that .NET 3.5 includes support for
named pipes now). But you're not specific about why you "have problems
with it". It's impossible to provide any advice along those lines without
details about what problems you specifically had.

Pete
Aug 15 '08 #3
On Aug 15, 11:35*pm, "Markgoldin" <markgoldin_2...@yahoo.comwrote:
I am searching for a solution of sending messages from not .Net process into
a .Net process written in C#.
I have gone already thru sockets solution and have porblems with it. I am
not a C# coder dont kill :)
I am thinking about something like this:
A remote non .Net process has a window with a name known to .Net program.
Can .Net program somehow process that window using window's title and
extracting a text from it?
What other solutions might be?

Thanks
If you want to understand the hole communication between process and
thread read the book of Jeffrey Richter
You have the import one of this functions depends on your need
PostMessage Or SendMessage

Bast Regards
Ohav Baumgarten
Aug 15 '08 #4
Here is my complete project:
main class:

using System;
using System.Net.Sockets;
using Lightstreamer.DotNet.Server;
using System.Net;
using System.Threading;
public class DataAdapterLauncher
{
public static TcpListener tcplJM4;
public static TcpListener tcplJM5;
public static TcpListener tcplJM6;
public static TcpListener tcplMD1;
public static TcpListener tcplMD2;
public static TcpListener tcplJM1;
public static TcpListener tcplJM2;
public static TcpListener tcplG2;
public static TcpListener tcpl1;
public static void Main(string[] args)
{
string host = "localhost";
bool go = true;
int reqrepPort = 6661;
int notifPort = 6662;
try
{
DataProviderServer server = new DataProviderServer();
server.Adapter = new SocketToLightStreamer();

MetadataProviderServer serverMeta = new
MetadataProviderServer();
serverMeta.Adapter = new MessageAwareLiteralBasedProvider();

TcpClient reqrepSocket = new TcpClient(host, reqrepPort);
server.RequestStream = reqrepSocket.GetStream();
server.ReplyStream = reqrepSocket.GetStream();

TcpClient notifSocket = new TcpClient(host, notifPort);
server.NotifyStream = notifSocket.GetStream();

server.Start();
System.Console.WriteLine("Remote Adapter connected to
Lightstreamer Server.");
System.Console.WriteLine("Ready to publish data...");

//start listeners that send data to the front-end. One for each
scanning area
IPEndPoint ipeJM4 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63335);//listen on all local addresses
tcplJM4 = new TcpListener(ipeJM4);
tcplJM4.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM5 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63336);//listen on all local addresses
tcplJM5 = new TcpListener(ipeJM5);
tcplJM5.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM6 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63337);//listen on all local addresses
tcplJM6 = new TcpListener(ipeJM6);
tcplJM6.Start(); // block application until data and connection
is requested
IPEndPoint ipeMD1 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63339);//listen on all local addresses
tcplMD1 = new TcpListener(ipeMD1);
tcplMD1.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM1 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63338);//listen on all local addresses
tcplJM1 = new TcpListener(ipeJM1);
tcplJM1.Start(); // block application until data and connection
is requested
IPEndPoint ipeJM2 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63340);//listen on all local addresses
tcplJM2 = new TcpListener(ipeJM2);
tcplJM2.Start(); // block application until data and connection
is requested
IPEndPoint ipeG2 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63342);//listen on all local addresses
tcplG2 = new TcpListener(ipeG2);
tcplG2.Start(); // block application until data and connection
is requested
IPEndPoint ipeMD2 = new IPEndPoint(IPAddress.Parse("0.0.0.0"),
63341);//listen on all local addresses
tcplMD2 = new TcpListener(ipeMD2);
tcplMD2.Start(); // block application until data and connection
is requested
catch (Exception e)
{
System.Console.WriteLine("Could not connect to Lightstreamer
Server.");
System.Console.WriteLine("Make sure Lightstreamer Server is
started before this Adapter.");
System.Console.WriteLine(e);
}
}
}

and another class:

using System;
using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;

using Lightstreamer.Interfaces.Data;

public class SocketToLightStreamer : IDataProvider
{
private IItemEventListener _listener;
private volatile bool goJM4;
private volatile bool goJM5;
private volatile bool goJM6;
private volatile bool goMD1;
private volatile bool goJM1;
private volatile bool goJM2;
private volatile bool goMD2;
private volatile bool goG2;

public void Init(IDictionary parameters, string configFile)
{
}

public bool IsSnapshotAvailable(string itemName)
{
return false;
}

public void SetListener(IItemEventListener eventListener)
{
_listener = eventListener;
}

public void Subscribe(string itemName)
{
if (itemName.Equals("floorupdate"))
{
new Thread(delegate() { Run(goJM4,
DataAdapterLauncher.tcplJM4); }).Start();
new Thread(delegate() { Run(goJM5,
DataAdapterLauncher.tcplJM5); }).Start();
new Thread(delegate() { Run(goJM6,
DataAdapterLauncher.tcplJM6); }).Start();
new Thread(delegate() { Run(goJM1,
DataAdapterLauncher.tcplJM1); }).Start();
new Thread(delegate() { Run(goJM2,
DataAdapterLauncher.tcplJM2); }).Start();
new Thread(delegate() { Run(goMD1,
DataAdapterLauncher.tcplMD1); }).Start();
new Thread(delegate() { Run(goMD2,
DataAdapterLauncher.tcplMD2); }).Start();
new Thread(delegate() { Run(goG2,
DataAdapterLauncher.tcplG2); }).Start();
}
}

public void Unsubscribe(string itemName)
{
if (itemName.Equals("floorupdate"))
{
goJM4 = false;
goJM5 = false;
goJM6 = false;
goMD1 = false;
goMD2 = false;
goJM1 = false;
goJM2 = false;
goG2 = false;
}
}

public void Run(Boolean go, TcpListener tcpl)
{
go = true;
// Buffer for reading data
Byte[] bytes = new Byte[1024];
String data;
Encoding ascii = Encoding.ASCII;
TcpClient tcpc = null;
while (go)
{
if (tcpc == null)
tcpc = tcpl.AcceptTcpClient(); //accept connection
data = "";
NetworkStream stream = tcpc.GetStream();
// Loop to receive all the data sent by the client.
int i = 0;
i = stream.Read(bytes, 0, bytes.Length);
while (i 0 && !data.EndsWith("\r"))
{
// Translate data bytes to a ASCII string.
data += ascii.GetString(bytes, 0, i);
i = stream.Read(bytes, 0, bytes.Length);
}
// Process the data sent by the client.
if (data != "")
{
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}
}
}
}
}

Three problems with.Net side:
1. Sometimes it crashes
2. when a client is disconnected CPU on a box that run this console
application goes to 100%.
I am trying to handle that with this:
....
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}

3. I am missing some readings from sockets. I think that is due to this:
.....
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}

I am thinking to move that into a separate thread. Do you think it will
help?

Just want to remind I am not a c# coder. So dont kill if you see something
funny :)
Aug 19 '08 #5
On Tue, 19 Aug 2008 10:46:04 -0700, Markgoldin <ma*************@yahoo.com>
wrote:
Here is my complete project:
Surely whatever problems exist in your code, they could be demonstrated
with just one TcpListener, rather than nine. Or, in other words, the code
you posted isn't a "concise-but-complete" code sample, which is the usual
standard for posting code when you want someone else to find problems in
it.

So, keeping in mind that I haven't put a lot of effort into examining each
and every line of code you posted...
[...]
Three problems with.Net side:
1. Sometimes it crashes
That's too vague a problem description for anyone to help. What does
"crash" mean? Does the OS actually terminate the process? Is there an
error or exception? If so, what is it?
2. when a client is disconnected CPU on a box that run this console
application goes to 100%.
Ahh...this seems familiar. I'm guessing this is the network code you had
questions about previously?

As far as the #2 question goes, you have some irregularities in your
SocketToLightStreamer.Run() method, but the one that is causing the CPU
consumption is that after the stream is closed (Read() returns 0), you
should set your TcpClient variable back to null.

It's not a great way to control flow within the loop, but that should at
least fix that issue, without having to resort to:
I am trying to handle that with this:
...
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}
As before, I still fail to see the point in having a variable (named "go")
to control the loop. Especially as you set it to "true" as the first
thing in the Run() method and the only other place it ever gets set to
"false" is shortly before a "return" statement, what's the point?

Also, stopping and starting the TcpListener is pointless, and could in
fact cause issues because of the way that the OS deals with TCP listening
sockets (you may find that the port you're trying to use is unavailable
for a short time after having closed the listening socket, which is what
TcpListener does implicitly).

But the main question is: are you sure that a closed connection will have
written the text "close" to the stream before it is closed? Assuming that
it does, then I don't see why the above wouldn't work, even if it is a bit
clumsy. So it seems that the most likely explanation for your high CPU
utilization issue is that the stream doesn't have the text "close" in it
before it's actually closed.
3. I am missing some readings from sockets. I think that is due to this:
....
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}

I am thinking to move that into a separate thread. Do you think it will
help?
Honestly, the last thing I think that would help is _more_ threads. :)

But regardless, the code above doesn't appear to me to have anything to do
with the socket i/o at all. As near as I can tell, it's just how you're
passing the most-recently-received data to your IItemEventListener
implementation. So I don't think any modification of that section of
code, whether putting it in a new thread or something else, would help
whatever the "missing some readings from sockets" problem is.

All that said, there's really no real diagnostics that anyone can do with
respect to your code without a concise-but-complete code sample. For
network code, that means code for both ends of the TCP connection, along
with some predefined data demonstrating how the network connection is used.
Just want to remind I am not a c# coder. So dont kill if you see
something
funny :)
Just to be clear: we may harass, laugh at, or otherwise bother someone who
posts funny code (though normally we just try to help), but there's a
strict no-kill policy around here. As far as I know, no one has ever
actually lost their life because of something they posted here. :)

Pete
Aug 19 '08 #6

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

Similar topics

3
by: Kurda Yon | last post by:
Sorry for a very stupid question. Can you tell me what does the following html source does if I press button "Send"? If I read this code by Netscaspe from my local file system and then press...
3
by: Ed Sutton | last post by:
How can I show the email client UI so the user can send the attachment to the desired recipient. I get an exception if I leave the MailMessage.To property blank. Is there any way to do this? I...
5
by: Go Perl | last post by:
Hello Here is an interesting question which I could not solve it. I have an old dos app for which source code is lost. I just have the exe file. So I have created a front-end for that. I...
3
by: SenthilVel | last post by:
how to send message using net send in c#??
14
by: supz | last post by:
Hi, I use the standard code given below to send an email from an ASP.NET web form. The code executes fine but no Email is sent. All emails get queued in the Inetpub mail queue. I'm using my...
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...
3
by: =?Utf-8?B?SHVnaA==?= | last post by:
Hi There, I use follow code to send email inside VB.NET 2005. It does not work well. Error message of "Failure sending email" would occue. However, email was sent out sometimes. I am confused...
6
by: ShieldsJared | last post by:
Hello all, I've been working on an application for a while now and have now come to a standstill. I have an application that I intend to have stored on a file server (developed in C#), which...
2
by: Mesan | last post by:
Hello everyone, Thanks to many useful posts in this newsgroup and others, I've been able to come very close to something I've been wanting to do for a very long time. I've figured out how to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.