VBA找不到C#dll入口点 [英] VBA can't find C# dll entry point

查看:211
本文介绍了VBA找不到C#dll入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#中创建一个dll,当调用单个函数runSimulation()时运行模拟。这个dll应该从VBA调用,因为在Excel中给出了某些参数值作为输入。这是我使用的代码。



C#:

  ; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Diagnostics;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用RGiesecke.DllExport;

命名空间模拟
{
public static class Simulation
{
[ComVisible(true)]
[DllExport(runSimulation,CallingConvention = CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.SysInt)]
public static int runSimulation()
{
//做模拟

返回0;
}
}
}

上面的代码编译为一个类库,x64作为平台目标,并返回Simulation.dll。



VBA:

 code>公共声明函数runSimulation LibSimulation.dll()As Long 

Sub Run_DLL()
ChDir(ActiveWorkbook.Path)
Dim returnValue As Long
returnValue = runSimulation()
End Sub

运行Visual Basic代码返回


运行时错误453:'找不到DLL入口点runSimulation
Simulation.dll'


当尝试调用runSimulation()。



作为参考:我试过运行

  [DllExport(runSimulation,CallingConvention = CallingConvention.StdCall)] 

而是也不行。我还尝试使用 https://www.linkedin.com/grp/post中提供的建议/ 40949-258782989 ,但它给了我一样的错误。

解决方案

我设法调用'runSimulation'函数在VBA代码中,通过C#中的接口暴露Simulation类:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Runtime.InteropServices;

命名空间NSSimulation
{
public interface ISimulation
{
int runSimulation();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Simulation:ISimulation
{
[ ComVisible(true)]
public int runSimulation()
{
return 0;
}
}
}

然后重新编译项目并从Visual Studio命令提示符导出您的重建库与'tlbexp.exe':

  tlbexp.exe SimulationLib.dll / out :SimulationLib.tlb/ win64 

然后在Excel中打开Visual Basic编辑器,并添加一个引用'SimulationLib.tlb'通过工具 - >参考 - >浏览。验证

 公共x作为新的SimulationLib.Simulation 

子测试()
MsgBox x.runSimulation
End Sub


I'm creating a dll in C# which runs a simulation when a single function runSimulation() is called. This dll should be called from VBA, as certain parameter values are given as input in Excel. This is the code I use.

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using RGiesecke.DllExport;

namespace Simulation
{
    public static class Simulation
    {
        [ComVisible(true)]
        [DllExport("runSimulation", CallingConvention = CallingConvention.Cdecl)]
        [return: MarshalAs(UnmanagedType.SysInt)]
        public static int runSimulation()
        {
            // Do simulation

            return 0;
        }
    }
}

The above code is compiled as a Class Library with x64 as Platform Target and returns Simulation.dll.

VBA:

Public Declare Function runSimulation Lib "Simulation.dll" () As Long

Sub Run_DLL()
ChDir (ActiveWorkbook.Path)
Dim returnValue As Long
returnValue = runSimulation()
End Sub

Running the Visual Basic code returns

run-time error 453: 'Can't find DLL entry point runSimulation in Simulation.dll'

when it tries to call runSimulation().

As a reference: I've tried running with

[DllExport("runSimulation", CallingConvention = CallingConvention.StdCall)]

instead, but this also doesn't work. I additionally tried using the advice given in https://www.linkedin.com/grp/post/40949-258782989 but it gives me the same error.

解决方案

I managed to call the 'runSimulation' function in VBA code, by exposing the Simulation class through an interface in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace NSSimulation
{
    public interface ISimulation
    {
        int runSimulation();
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class Simulation : ISimulation
    {
        [ComVisible(true)]
        public int runSimulation()
        {
            return 0;
        }
    }
}

Then re-compile your project and export your rebuilt library with 'tlbexp.exe' from the Visual Studio command prompt:

tlbexp.exe SimulationLib.dll /out:"SimulationLib.tlb" /win64

Then open the Visual Basic editor in Excel and add a reference to 'SimulationLib.tlb' through "Tools->References->Browse". To verify

Public x As New SimulationLib.Simulation

Sub test()
   MsgBox x.runSimulation
End Sub

这篇关于VBA找不到C#dll入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆