![](/img/trans.png)
[英]Using UnmanagedExports Package [DllExport] to call C# DLL in VBA Triggers "Can't Find DLL Entry Point" Error
[英]C# DLL using DllExport: No entry point when called in VBA
為避免要求為電子表格的所有用戶注冊 Dll,我嘗試使用后期綁定,以便用戶無需添加對 Dll 的引用。
我已經使用 Visual Studio 在 C# 中創建了 Dll,即使我已經包含了“使用 RGiesecke.DllExport;” 並在函數上使用 DllExport 返回一個包含我需要在 VBA 中訪問的函數的對象,我仍然收到錯誤“運行時錯誤‘453’:無法在 C:\\temp\\MyFunctions 中找到 DLL 入口點 CreateDotNetObject。 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 做什么?
謝謝,
麥克風
我正在構建解決方案,其中類庫屬性中的平台目標設置為“任何 PC”,這顯然不允許導出。 當我將它切換到“x86”時,它完全可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.