1

My code is in Java and I need to integrate with another program on the same Windows machine that is written in .NET/WPF. The .NET/WPF program is sending data using Windows Named Pipes that are triggered based on certain events that happen in the .NET application. So I just need to setup a listener to read that data from the Windows Named Pipe when it's triggered. Note: I don't need to create the Named Pipe from Java, only to setup a listener to read the Named Pipe coming from the Windows .NET application.

Tried this thread Concurrent read/write of named pipe in Java (on windows) but get this error:

java.io.FileNotFoundException: \.\pipe\pixelcade (The system cannot find the file specified)

This error happens after I've started the .NET program which is generating the Windows Named Pipe. But note that the named pipe is fired upon certain events and not streaming continuously.

try {
// Connect to the pipe
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\pixelcade", 
"rw");
String echoText = "Hello word\n";
// write to pipe
pipe.write ( echoText.getBytes() );
// read response
String echoResponse = pipe.readLine();
System.out.println("Response: " + echoResponse );
pipe.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

The expected result would be a few strings from the named pipes from the .NET application.

This code in C#/WPF is what is needed, just don't know how to code the equivalent in Java.

// 1. Create a Pipe event handler
_pipeServer = new PipeServer();
_pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);

// 2. Then listen for a message
private void Listen()
{
try
{
if (userPrefs.NamedPipeID == string.Empty)
_pipeServer.Listen(_marqueeConfigfileName); // _marqueeConfigfileName is 
the pipename (shake hand between 2 apps; client and server)
else
_pipeServer.Listen(userPrefs.NamedPipeID); // _marqueeConfigfileName is the 
pipename (shake hand between 2 apps; client and server)
}
catch (Exception ex)
{
ApplicationLog.EventWriteLog(ex.Message);
}
}
// 3. Handler, my custom logic goes here
private void PipesMessageHandler(string message)
{
try
{
string[] args = CommandLineToArgs(message);
int paramLengh = args.Length;
if (!CheckAccess())
{
 Dispatcher.Invoke(() => PipesMessageHandler(message));
}
else
{
if (paramLengh > 2) _game = args[2];
if (paramLengh > 3) _gameSystem = args[3];
if (paramLengh > 4) _freetext = args[4];
if (paramLengh > 5) _event = args[5];

ApplicationLog.EventWriteLog("Pipe message received Wheel: ["+_gameSystem 
+"] item: ["+_game+"]");
if (applySettingsThread != null) applySettingsThread.Abort();
applySettingsThread = this.Dispatcher.BeginInvoke(DispatcherPriority.Send, 
new Action(ApplySettings));

}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
Al Linke
  • 51
  • 7
  • https://stackoverflow.com/questions/634564/how-to-open-a-windows-named-pipe-from-java/2605884#2605884 – niteshbisht Jul 05 '19 at 05:08
  • thanks, I did see that post. In my case, I just need to create a listener for an existing named pipe from Windows as opposed to creating a new named pipe from Java. There was some code in that post to connect to an existing pipe from Java but didn't work in my case. – Al Linke Jul 05 '19 at 14:29
  • Did you add JNA and dlls – niteshbisht Jul 05 '19 at 14:31
  • did not yet but will try, thanks for your help. I may have missed it but didn't see any code for a listener, just the code to create a named pipe from java which I don't need to do. If you have any sample code to create just the listener, that would be really helpful. On the dll's , not sure which ones? – Al Linke Jul 05 '19 at 16:40

0 Answers0