为避免要求为电子表格的所有用户注册 Dll,我尝试使用后期绑定,以便用户无需添加对 Dll 的引用。
我已经使用 Visual Studio 在 C# 中创建了 Dll,即使我已经包含了“使用 RGiesecke.DllExport;”。并在函数上使用 DllExport 返回包含我需要在 VBA 中访问的函数的对象,我仍然收到错误“运行时错误 '453': Can't Find DLL entry point CreateDotNetObject in C:\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 中添加引用。如果我添加引用(早期绑定),这些函数确实可以工作,但是 DLL 不会传播给使用电子表格的每个人,我需要它在他们运行正常函数时不会崩溃。
编辑:附加信息。我刚刚注意到,除了 DLL,当我在 Visual Studio 中构建解决方案时,我还得到了一个“ Object FileLibrary ”和一个“ Exports Library File ”。当我注册 DLL 时,我应该对 .exp 或 .lib 做什么?
谢谢,
麦克风