Why
- 最近在写我自己的项目的时候遇到了这个情况,有个官方提供的组件库只有32位的,但是我当前的程序是64位的,我不想因为一个
dll改了我整个程序的框架,但是不同位数的又没办法直接调用,所以使用了NamedPipeClientStream和NamedPipeServerStream。
How
- 不需要额外安装包。
- 建立
x86的项目来调用32位的dll。
internal class Program
{
[DllImport("硬件检测引擎.dll", CallingConvention = CallingConvention.StdCall)]
public static extern string Hwinfo(string configPath, string dllPath);
static void Main(string[] args)
{
while (true)
{
using (var pipe = new NamedPipeServerStream("myPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
pipe.WaitForConnection();
//await pipe.WaitForConnectionAsync();
var str = Hwinfo("config.ini", "硬件检测引擎.dll");
Console.WriteLine(str);
var bytes = Encoding.UTF8.GetBytes(str);
pipe.Write(bytes, 0, bytes.Length);
pipe.Flush();
}
}
}
}
- 在生成的
x86的文件夹下放上需要的硬件检测引擎.dll。
- 建立
x64项目(winform)
public static class PipeClient
{
public static string Start()
{
using (var pipe = new NamedPipeClientStream(".", "myPipe", PipeDirection.InOut))
{
pipe.Connect(3000);
byte[] buffer = new byte[4096];
int len = pipe.Read(buffer, 0, buffer.Length);
var str = Encoding.UTF8.GetString(buffer, 0, len);
return str;
}
}
}
- 这是最简单的示例,如果需要加入参数的时候可以自己使用
pipe.Write()。
Run
- 先需要跑起来
x86的项目,然后运行x64的项目,调试可以发现是没什么问题。
Tips
- 不知道为啥,我调用的这个输出信息一直显示不全,很尴尬,但是这个思路肯定是没有问题的。
评论区