我有一个非常短的VB5 DLL,我试图从VS2015 C#调用。我将VB5项目编译为ActiveX DLL并将DLL复制到VS可以找到它的库文件夹中。
我收到一个执行异常,VS无法在DLL中找到该函数,我不知道如何修复它。我在StackOverflow中搜索了许多查询,并尝试实现他们的修复,但到目前为止,没有一个有效。它们是为不同版本的VB或Visual Studios编写的,或者我做了回答者推荐的内容,但它并没有解决我的问题。
VB5项目信息:
VB_HW.vbp的内容:
'-------------------------------------------------------------------------------
Public Function VB_Fn(ByVal x As Long) As Long
VB_Fn = 1000 + x
End Function
'-------------------------------------------------------------------------------
C#表格。单击某个表单上的某个按钮时会生成此表单。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace IR_Trap_Phase_I
{
public partial class TempForm : Form
{
// ---------------------------------------------------------------------
public TempForm()
{
InitializeComponent();
}
// ---------------------------------------------------------------------
[DllImport("VB_HW.dll", SetLastError = true)]
static extern long VB_Fn(long x);
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
long returned = 0;
returned = Call_VB_Fn();
MessageBox.Show(" in TempForm: Got " + returned.ToString("n0") + " back from call to VB.", "debug", MessageBoxButtons.OK);
}
public static long Call_VB_Fn()
{
long returned_long = 0;
long outgoing_long = 1015;
MessageBox.Show("In USB_Ops.Call_VB_Fn: about to call VB_Fn." + Environment.NewLine +
" outgoing_long = " + outgoing_long.ToString("n0") + " and returned_long = <" + returned_long + ">." + Environment.NewLine +
" Drum roll please)...",
"debug", MessageBoxButtons.OK);
returned_long = VB_Fn(outgoing_long);
MessageBox.Show("LOOK LOOK LOOK: " + Environment.NewLine +
" outgoing_long = " + outgoing_long.ToString("n0") + " and VB_Fn = <" + returned_long + "> -- SHOULD BE 2015.",
"debug", MessageBoxButtons.OK);
return (outgoing_long);
}
}
}
我在第
行收到执行错误 returned_long = VB_Fn(outgoing_long);
我收到的错误消息是:
&#34;抛出异常:&#39; System.EntryPointNotFoundException&#39;在IR_Trapper_Phase_I_Ass.exe
中其他信息:无法找到名为&#39; VB_Fn&#39;的入口点在DLL&#39; F:\ SBIR \ VS2015 \ IR_Trap_Phase_I \ IR_Trap_Phase_I \ obj \ Debug \ Interop.VBHW.dll&#39;。&#34;
因此VS正确找到了我的Project Reference并找到了DLL文件,但无法在DLL文件中找到VB_Fn函数。
其他线索:
但是,选择&#34; VB_Hello_World&#34;或者&#34; VB_Hello_World_Class&#34;只提供选项&#34;等于&#34;和&#34; ReferenceEquals&#34;。
这往往会支持错误消息(无法找到VB_Fn)。
有人可以就如何正确执行提供建议吗?
后记。您可能想知道为什么我试图在C#中从VS2015调用VB5 DLL。原因是我已经有一个已经编写并运行了几千行VB5项目代码的项目。如果我可以按原样使用DLL中的VB5代码,并避免重写C#中的所有代码,我将成为英雄。
此方案是一种快速,简短的方法,可以确定是否可以从VS2015 C#调用VB5代码。