侧边栏壁纸
  • 累计撰写 95 篇文章
  • 累计创建 11 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

CSharp64位程序调用32位Dll

祈安千
2026-05-20 / 0 评论 / 0 点赞 / 0 阅读 / 0 字

Why

  • 最近在写我自己的项目的时候遇到了这个情况,有个官方提供的组件库只有32位的,但是我当前的程序是64位的,我不想因为一个dll改了我整个程序的框架,但是不同位数的又没办法直接调用,所以使用了NamedPipeClientStreamNamedPipeServerStream

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

  • 不知道为啥,我调用的这个输出信息一直显示不全,很尴尬,但是这个思路肯定是没有问题的。
0

评论区