如何从 C++ 调用 VB.NET DLL (也调用函数 - 不仅仅是 DLL 文件)
我想问一下如何从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() 来查找 ERROR
cout << GetLastError() <<结束;
但我发现函数失败时出现错误 127,调用 DLL 文件中出现错误 203
任何人都可以帮助我,
非常感谢
,问候
I want to ask question about how to call VB.NET DLL from C++ program
I have tried many times to call VB.NET DLL file from C++ and it is working fine but the problem is I can't call the function of VB.NET DLL file ( I can only load the VB.NET DLL file)
in VB.NET DLL I have the following code:
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
============================
My C++ Code is:
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);
}
My following steps is:
1) I have Created VB.NET DLL.
2) I have Created a new application visual C++ and selected "win32 console application"
3) I have written the code to call the DLL and the functions (as you can see in the above)
did I miss anything in the steps or code because I can call VB.NET DLL file but I can't call the VB.NET DLL function
as you can see I have written the GETLASTERRIR() to find the ERROR
cout << GetLastError() << endl;
but I found this Error 127 in function when failed and 203 in the call DLL file
can anyone help me
Thank you very much
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您的 vb 程序集需要与“本机”可执行文件完全不同的运行时,因此您需要在两者之间使用某个层。该层可以是COM。
您可以通过其“ComVisible”属性将程序集公开给 COM 子系统。然后,您应该注册该程序集以将其公开给 COM“订阅者”。
只有这样,您才能从 C++ 代码
#import
程序集命名空间。注意:这是 msdn 文章“如何调用托管来自本机 Visual C++ 代码的 DLL"
编辑 - 刚刚尝试了一下...并且似乎工作正常:
C# 代码
项目设置
(需要签名以便能够生成 tlb)
C++ 代码:
Since your vb assembly needs a totally different runtime than the 'native' executable, you will need to use some layer in between. This layer may be COM.
You can expose your assembly to the COM subsystem by it's 'ComVisible' property. Then, you should register the assembly to expose it to COM 'subscribers'.
Only then you can
#import
the assembly namespace from your c++ code.Note: this is a very brief version of an msdn article "How to call a managed DLL from native Visual C++ code"
EDIT-- Just tried it out... and it seems to work allright:
C# code
Project settings
(Signing was needed in order to be able to generate the tlb)
C++ code:
"Class1::example_function1"
可以作为有效标识符的 dll。通常它仅与extern "C"
(或在不带 ++ 的 C 中实现)函数一起使用,这些函数不会被损坏。"Class1::example_function1"
could be valid identifier. Normally it's only used withextern "C"
(or implemented in C without ++) functions, which are not mangled.您无法直接从本机 C++ 访问 .NET 代码,因此需要 C++/CLI。
如果您的程序需要是本机 C++,则可以编写混合模式包装 DLL,为主程序提供本机 C++ 接口,并在实现中使用 C++/CLI 将调用转发到 .NET DLL。
You can't directly access .NET code from native C++, you will need C++/CLI for that.
If your program needs to be native C++, there is the possibility of writing a mixed-mode wrapper DLL which provides a native C++ interface for the main program, and uses C++/CLI in the implementation to forward calls to the .NET DLL.
您需要在 C++/CLI 上为此编写一个包装器。您可能会发现以下链接很有帮助。 http://www.codeproject.com/KB/mcpp/cppcliintro01.aspx
You would need to write a wrapper on C++/CLI for that . You might find the following link helpful. http://www.codeproject.com/KB/mcpp/cppcliintro01.aspx