64位程序調用32DLL解決方案

     最近作一個.NETCore項目,須要調用之前用VB6寫的老程序,本來想重寫,但因爲其調用了大量32DLL,重寫後還須要編譯爲32位才能運行,因而乾脆把老代碼整個封裝爲32DLL,而後準備在64位程序中調用。(注意Windows系統中,先要把DLL註冊爲COM)ui

   爲了實現64位程序調用32DLL,我嘗試了大量方法,但效果都不是很理想,偶然中發現.NetCore的「管道」,能夠完美地解決這個問題,具體思路以下:blog

一、建立一個.NETFramework32位程序,在其中對封裝的老代碼進行引用(COM中引用),而後將其接口暴露接口

二、建立64位.NETCore程序,在其啓動時,爲第一步建立的程序建立進程,並啓動進程

三、使用「雙工管道」讓64位程序與32程序進行通訊,完美實現64位程序調用32DLLip

   下邊代碼展現一個簡單的管道通訊過程:input

A、64程序代碼string

 static void Main(string[] args)
        {  //建立refpropPipe進程
 Process process = new Process();
            //將refpropPipe.exe放在與refprop64Hv相同路徑下,相對路徑引用
            process.StartInfo.FileName = @"C:\Users\Administrator\source\repos\refpropPipe\refpropPipe\bin\Debug\refpropPipe.exe";
             //process.StartInfo.FileName = "refpropPipe.exe";
            process.Start();
            double value = 0;
            //向refpropPipe發送調用信息,即查詢輸入變量值
            using (NamedPipeClientStream pipeClientStream = new NamedPipeClientStream("request"))
            {
                pipeClientStream.Connect();
                string input = Method + "," + FluidName + "," + InpCode + "," + Units + "," + Prop1 + "," + Prop2;
                using (StreamWriter writer = new StreamWriter(pipeClientStream))
                {
                    writer.WriteAsync(input);
                }
            }
            //接收refpropPipe返回的信息,即查詢結果
            using (NamedPipeClientStream pipeClientStream = new NamedPipeClientStream("respose"))
            {
                pipeClientStream.Connect();
                using (StreamReader reader = new StreamReader(pipeClientStream))
                {
                    string val = reader.ReadToEnd();
                    value = Convert.ToDouble(val);
                }
            }
            process.WaitForExit();
            process.Close();

}

  B、32位程序代碼it

  static void Main(string[] args)
        {
            double respose = 0;
            ///接收refprop64Hv的輸入信息
            using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("request"))
            {
                pipeStream.WaitForConnection();
                using (StreamReader reader = new StreamReader(pipeStream))
                {
                 //此處接收到消息後,對32Dll進行調用
}

                //向refprop64Hv返回結果
  using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("respose"))
                {
                    pipeStream.WaitForConnection();
                    using (StreamWriter writer = new StreamWriter(pipeStream))
                    {
                        string res = respose.ToString();
                        writer.WriteAsync(res);
                    }
                }
相關文章
相關標籤/搜索