如何使用32位.dll在Excel 64位VSTO C#上运行

时间:2020-02-08 17:36:50

标签: c#

我在解决方案中有2个项目:一个是与PLC通信的控制台项目,另一个是Excel加载项。

我的问题:使用.dll(x32位)并在Excel 64位上运行。通过功能区按钮控制按钮来连接或断开与PLC的连接。因此,我在Console Project的引用中添加了Excel加载项项目,以使用Excel加载项公共类。但是,我无法调用VSTO Addin的构造函数

public ThisAddIn(global::Microsoft.Office.Tools.Word.ApplicationFactory factory, global::System.IServiceProvider serviceProvider) : 
                base(factory, serviceProvider, "AddIn", "ThisAddIn") {
            Globals.Factory = factory;
        }

这是我在控制台中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Microsoft.Office.Tools.Ribbon;
using PLC2EXCEL_Addin;
using DATABUILDERAXLibLB;
using Microsoft.Office.Tools.Ribbon;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

namespace PLC2EXCEL
{
    class Program
    {
        public static DBCommManager dBCommManager = new DBCommManager();

        static void Main(string[] args)
        {

            dBCommManager.PLC=DBPlcId.DBPLC_KV3000;
            dBCommManager.Peer = "USB";
            //Microsoft.Office.Tools.Excel.ApplicationFactory _fatory ;
            //_fatory.GetRibbonFactory();

            //ThisAddIn thisAddIn = new ThisAddIn(Microsoft.Office.Tools.Excel.ApplicationFactory,IServerProvider); //i am not sure about this

            PLC2EXCEL_Ribbon pLC2EXCEL_Ribbon = new PLC2EXCEL_Ribbon();
            if (pLC2EXCEL_Ribbon != null)
                pLC2EXCEL_Ribbon.connectPLC_btn.Click += new RibbonControlEventHandler(startPLC2Excel);
            dBCommManager.Connect();
            Console.WriteLine("Connect PLC OK");
            Console.ReadLine();
        }

        public event EventHandler startPLC2Excel1;
        public static void startPLC2Excel(object sender, RibbonControlEventArgs eventArgs)
        {
            //DATABUILDERAXLibLB.DBCommManager dBCommManager = new DBCommManager();
            //dBCommManager.PLC = DBPlcId.DBPLC_KV3000;
            //dBCommManager.Peer = "USB";
            dBCommManager.Connect();
            Console.WriteLine("Connect PLC OK");
            Console.ReadLine();
        }
    }
}

以及Excel加载项功能区中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Tools.Ribbon;

namespace PLC2EXCEL_Addin
{
    public partial class PLC2EXCEL_Ribbon
    {
        private void PLC2EXCEL_Ribbon_Load(object sender, RibbonUIEventArgs e)
        {

        }


        [ComVisible(true)]
        public void connectPLC_btn_Click(object sender, RibbonControlEventArgs e)  //How to catch this event from Console project
        {

            //Catch event 
            MessageBox.Show("Connect to PLC");

        }
        public void abc() { }
    }
}

很抱歉给您带来不便,这是我的第一篇文章。 预先感谢。

1 个答案:

答案 0 :(得分:0)

无法在64位Excel上使用32位.dll,所以我通过创建2个单独的项目解决了我的问题:

  • 一个是C#应用程序,添加了32位.dll并与PLC通信并发送数据 打开Excel文件。
  • 还有带功能区按钮的Excel插件, 单击按钮启动/停止执行C#应用程序
相关问题