我的代码使用Java,我需要与用.NET / WPF编写的同一Windows计算机上的另一个程序集成。 .NET / WPF程序正在使用Windows命名管道发送数据,这些管道是根据.NET应用程序中发生的某些事件触发的。因此,我只需要设置一个侦听器以在触发Windows命名管道时读取该数据。注意:我不需要从Java创建命名管道,只需设置一个侦听器即可读取来自Windows .NET应用程序的命名管道。
尝试了该线程Concurrent read/write of named pipe in Java (on windows),但收到此错误:
java.io.FileNotFoundException:\。\ pipe \ pixelcade(系统找不到指定的文件)
在启动生成Windows命名管道的.NET程序后,会发生此错误。但是请注意,命名管道会在某些事件时触发,并且不会持续流式传输。
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();
}
预期结果将是.NET应用程序中命名管道的一些字符串。
需要用C#/ WPF中的代码,只是不知道如何用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);
}
}