为了避免要求为电子表格的所有用户注册 Dll,我尝试使用后期绑定(bind),这样用户就不需要添加对 Dll 的引用。
我已经使用 Visual Studio 在 C# 中创建了 Dll,尽管我已经包含了“using RGiesecke.DllExport;”并在函数上使用 DllExport 返回包含我需要在 VBA 中访问的函数的对象,我仍然收到错误“运行时错误‘453’:无法在 C 中找到 DLL 入口点 CreateDotNetObject :\temp\MyFunctions.dll."
DLL代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Data;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Client;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
using System.Collections.ObjectModel;
using System.Threading;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework;
using Microsoft.TeamFoundation.Common;
using Microsoft.VisualBasic;
using System.Diagnostics;
using RGiesecke.DllExport;
namespace MyFunctions
{
public interface IMyFunctions
{
string GetWorkItemLinkList(string WIIDs);
}
[CLSCompliant(true), ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class MyFunctions : IMyFunctions
{
TfsConfigurationServer server;
WorkItemStore store;
private void TFSconnect()
{
//Code to connect
}
[CLSCompliant(true), ComVisible(true), Description("GetWorkItemLink func")]
public string GetWorkItemLink(int WIID)
{
TFSconnect();
//Code to build return string "message"
return message;
}
[CLSCompliant(true), ComVisible(true), Description("GetWorkItemLinkList func")]
public string GetWorkItemLinkList(string WIIDs)
{
TFSconnect();
//Code to build return string "returnStr"
return returnStr;
}
}
static class UnmanagedExports
{
[DllExport]
[return: MarshalAs(UnmanagedType.IDispatch)]
static Object CreateDotNetObject()
{
return new MyFunctions();
}
}
}
在VBA中的声明如下:
Private Declare Function CreateDotNetObject Lib "c:\temp\MyFunctions.dll" () As Object
但是当我尝试实例化一个对象时,我得到了上面提到的错误。
Sub test()
Dim o As Object
Set o = CreateDotNetObject()
End Sub
这是我第一次尝试在 Excel 中使用自定义 dll 函数而不在 VBA 中添加引用。如果我添加一个引用(早期绑定(bind)),这些函数确实可以工作,但 DLL 不会传播给使用电子表格的每个人,我需要它在运行正常函数时不会崩溃。
编辑:附加信息。我只是注意到,除了 DLL 之外,当我在 Visual Studio 中构建解决方案时,我还会得到一个“Object FileLibrary”和一个“Exports Library File”。当我注册 DLL 时,我应该用 .exp 或 .lib 做些什么吗?
谢谢,
迈克
最佳答案
我在构建解决方案时将类库属性中的平台目标设置为“任何 PC”,这显然不允许导出。当我将它切换到“x86”时,它完全可以工作。
关于C# DLL 使用 DllExport : No entry point when called in VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23415566/