我想问关于如何从VB.NET程序调用C++ DLL的问题。
我多次尝试从VB.NET调用C++ DLL文件,它运行良好,但问题是无法调用VB.NET DLL文件的函数(只能加载VB.NET DLL文件)。
in 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动态链接库。
2)我创建了一个新的应用程序visual C++并选择了"win32控制台应用程序“。
3)我编写了调用DLL和函数的代码(如上面所示)。
是否遗漏了步骤或代码中的任何内容,因为我可以调用VB.NET DLL文件,但不能调用VB.NET DLL函数
如您所见,我编写了GETLASTERRIR()来查找错误
cout << GetLastError() << endl;
但是当函数失败时,我发现了这个错误127,在调用DLL文件中发现了203错误。
有人能帮我吗
非常感谢
问候
发布于 2011-05-13 12:39:13
"Class1::example_function1"
可以作为有效标识符的dll。通常,它只与extern "C"
一起使用(或在C中实现,没有++),这些函数不会损坏。。
发布于 2011-05-13 12:40:43
您不能从本机.NET直接访问C++代码,为此需要C++/CLI。
如果您的程序需要是本机C++,则有可能编写一个混合模式包装器DLL,它为主程序提供本机C++接口,并在实现中使用C++/CLI转发对.NET DLL的调用。
发布于 2011-05-13 12:42:42
为此,您需要在C++/CLI上编写一个包装器。您可能会发现下面的链接很有用。http://www.codeproject.com/KB/mcpp/cppcliintro01.aspx
https://stackoverflow.com/questions/5991705
复制相似问题