-1

我想在 VBA 中使用 C# 回调函数。但是当我调用 C# 函数时,它显示“找不到入口点”

我该如何解决?谢谢

PS:我检查了 Make assembly COM-Visible and Register for COM inter Environment:MS Office Excel 2007, MS Visual Studio 2008

VBA代码:

Declare Function Result Lib"C:\Users\admin\Desktop\myprog\New_DLL_Test\New_DLL_Test\bin\Debug\New_DLL_Test.dll" Alias "Msg" (ByVal p As Long) As Integer

Function Disp()
   MsgBox x
End Function

Sub AddResult(ByVal p As Long)
    Dim x As Long   
    x = Result(p)
    Debug.Print x
End Sub

Sub testnow_Click()
    Call AddResult(AddressOf Disp)
End Sub

C#代码:

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


namespace New_DLL_Test
{
    [Guid("f1286974-0f1a-466c-8389-dd1ab7e3eed2"), InterfaceType(ComInterfaceType.InterfaceIsDual)]
    interface Msginterface
    {
        int Msg;
    }
    public unsafe class MsgProcess
    {
        public int Msg(int* p)
        {
           return add(p,1);
        }
        public int add(int* p, int j)
        {
            return j+1;
        }
    }

}
4

1 回答 1

0

VBA 声明函数允许您访问从 DLL 导出的 stdcall 函数。COM 可见类和接口是完全不同的东西。虽然这两种方法可以一起工作,但充当非托管导出的函数可以返回一个 com 可见对象。或者每种方法都可以独立使用。

从 VBA 调用静态 C# 函数

如果您想以这种方式编写 VBA 代码,并像这样访问 DLL,则需要从 C# 项目创建一个非托管导出。我相信有两种方法可以做到这一点。一种是在托管 C++ 中手动创建一个导出函数的包装器。另一种是使用像UnmanagedExports这样的编译时构建任务和库。

使用 VBA 中的 COM 可见 C# 对象

要通过 COM 互操作使用 C# 类,您需要使用 regasm.exe 注册 C# 程序集,然后使用 CreateObject 函数在 VBA 中创建对象。类和接口都需要 COM 可见。你可以在这里找到一个非常基础的教程

于 2013-08-20T03:35:24.180 回答