【发布时间】:2011-05-13 12:19:41
【问题描述】:
我想问一下如何从C++程序调用VB.NET DLL
我曾多次尝试从 C++ 调用 VB.NET DLL 文件,它工作正常,但问题是我无法调用 VB.NET DLL 文件的功能(我只能加载 VB.NET DLL 文件)
在 VB.NET DLL 中我有以下代码:
Public Function example_function1(ByVal i As Integer) As Integer
Return 3
End Function
Public Function example_function2(ByVal i As Integer) As Integer
Return 3
End Function
=============================
我的 C++ 代码是:
typedef int (__stdcall *ptf_test_func_1_type)(int);
typedef int (__stdcall *ptf_test_func_2_type)(int*);
int i =1;
HINSTANCE dll_instance = LoadLibrary("DLLs7.dll");
int main()
{
if(dll_instance !=NULL)
{
printf("The DLLs file has been Loaded \n");
cout << GetLastError() << endl;
ptf_test_func_1_type p_func1=(ptf_test_func_1_type)GetProcAddress(dll_instance,"Class1::example_function1");
ptf_test_func_2_type p_func2=(ptf_test_func_2_type)GetProcAddress(dll_instance,"Class1::example_function2");
// Function No 1 //
if (p_func1 != NULL)
{
cout << "\nThe function number 1 is " << p_func1(i) << endl;
}
else
{
cout << "\nFailed" << endl;
cout << GetLastError() << endl;
}
// Function No 2 //
if (p_func2 != NULL)
{
cout << "\nThe function number 2 is" << p_func2(&i) << endl;
}
else
{
cout << "\nFailed" << endl;
cout << GetLastError() << endl;
}
}
else
{
printf("\nDLLs file Load Error");
cout << GetLastError() << endl;
}
cout << GetLastError() << endl;
return(0);
}
我的以下步骤是:
1) 我已经创建了 VB.NET DLL。
2)我创建了一个新的应用程序visual C++并选择了“win32控制台应用程序”
3) 我已经编写了调用 DLL 和函数的代码(如您在上面看到的)
我是否因为可以调用 VB.NET DLL 文件但无法调用 VB.NET DLL 函数而遗漏了步骤或代码中的任何内容
如您所见,我已经编写了 GETLASTERRIR() 来查找错误
cout
但是我在失败时在函数中发现了这个错误 127,在调用 DLL 文件中发现了 203
谁能帮帮我
非常感谢
问候
【问题讨论】:
-
错误 127 表示“找不到指定的过程”。 VB.NET 无法导出这样的函数。您将需要使用 COM(使用
属性)或用 C++/CLI 语言编写包装类。需要这种胶水来初始化 CLR,以便您的程序可以执行托管代码。
标签: c++ c visual-studio-2008 visual-c++ visual-c++-2008