2014-03-04 67 views
2

我試圖從C#DLL與非託管導出包(https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports)導出函數。我不想使用COM。如何在VBA中使用來自C#的非託管導出?

可惜的是,我得到一個錯誤:

Can't find DLL entry point [email protected] in DllName

我的C#類看起來是這樣的:

public class TestClass 
{ 
    [DllExport] 
    public static int TestDLL(int x) 
    { 
     return x + 2; 
    } 
} 

我這樣稱呼它:

Private Declare Function TestDLL Lib "DllName" Alias "[email protected]" (ByVal x As Long) As Long 

Public Sub TestFunc() 
    MsgBox CStr(TestDLL(2)) 
End Sub 

爲什麼不這行得通...?

+3

聽起來像是你根本沒有正確地使用工具,如不使用項目模板。別名不是必需的,不妨將其刪除。通過在DLL上運行dumpbin.exe/exports來仔細檢查。 –

回答

2

感謝漢斯帕桑特評論,我想在我的DLL,這給了輸出使用DUMPBIN.EXE /出口:

Dump of file DllName.dll

File Type: DLL

Section contains the following exports for \DllName.dll

00000000 characteristics 
5315D2DA time date stamp Tue Mar 04 14:19:22 2014 
    0.00 version 
     1 ordinal base 
     1 number of functions 
     1 number of names 

ordinal hint RVA  name 

     1 0 000027DE TestDLL 

Summary

2000 .reloc 
    2000 .rsrc 
    2000 .sdata 
    2000 .text 

正如你所看到的,函數名是TestDLL。我糾正申報VBA來:

Private Declare Function TestDLL Lib "DllName" (ByVal x As Long) As Long 

現在工作得很好:)

相關問題