简体   繁体   English

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

[英]How to use .dll 32bit run on Excel 64bit VSTO C#

I have 2 projects in a solution :One is Console Project where communicate with PLC and another is Excel add-in project.我在一个解决方案中有 2 个项目:一个是与 PLC 通信的控制台项目,另一个是 Excel 插件项目。

My Problem: using .dll (x32bit) and run on Excel 64bit.我的问题:使用 .dll (x32bit) 并在 Excel 64 位上运行。 Control button by Ribbon button to connect or disconnect to PLC.通过 Ribbon 按钮控制按钮连接或断开与 PLC 的连接。 So, I added Excel add-in project in Console Project's references to use Excel add-in public class.因此,我在控制台项目的引用中添加了 Excel 加载项项目以使用 Excel 加载项公共类。 But, i cannot call constructor of VSTO Addin但是,我无法调用 VSTO Addin 的构造函数

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

Here my code in Console:这是我在控制台中的代码:

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();
        }
    }
}

And code in Ribbon of Excel Addin:和 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() { }
    }
}

Sorry any inconvenience, this is my first post.抱歉给您带来不便,这是我的第一篇文章。 Thank in advance.预先感谢。

Impossible to use .dll 32bit on 64bit Excel,So I have solved my problem by create 2 separate projects:无法在 64 位 Excel 上使用 .dll 32 位,所以我通过创建 2 个单独的项目解决了我的问题:

  • One is C# app adding .dll 32bit and communicate to PLC and send data to opened excel file.一种是 C# 应用程序添加 .dll 32 位并与 PLC 通信并将数据发送到打开的 excel 文件。
  • Anther is Excel Addin with ribbon buttons, launch/stop executive C# app when click button Anther 是带有功能区按钮的 Excel 插件,单击按钮时启动/停止执行 C# 应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM