如何从 C++ 调用 VB.NET DLL (也调用函数 - 不仅仅是 DLL 文件)

发布于 2024-11-07 08:54:01 字数 2741 浏览 0 评论 0原文

我想问一下如何从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绾颜 2024-11-14 08:54:01

由于您的 vb 程序集需要与“本机”可执行文件完全不同的运行时,因此您需要在两者之间使用某个层。该层可以是COM。

您可以通过其“ComVisible”属性将程序集公开给 COM 子系统。然后,您应该注册该程序集以将其公开给 COM“订阅者”。

只有这样,您才能从 C++ 代码#import 程序集命名空间。

注意:这是 msdn 文章“如何调用托管来自本机 Visual C++ 代码的 DLL"

编辑 - 刚刚尝试了一下...并且似乎工作正常:

C# 代码

namespace Adder
{
    public interface IAdder
    {
        double add(double a1, double a2);
    }
    public class Adder : IAdder
    {
        public Adder() { }
        public double add(double a1, double a2) { return a1 + a2; }
    }
}

项目设置

[assembly: ComVisible(true)]
[assembly: AssemblyDelaySign(false)]

(需要签名以便能够生成 tlb)

C++ 代码:

#import <adder.tlb> raw_interfaces_only

  CoInitialize(NULL);
  Adder::IAdderPtr a;
  a.CreateInstance( __uuidof( Adder::Adder ) );
  double d = 0;
  a->add(1.,1., &d); 
  // note: the method will return a HRESULT; 
  // the output is stored in a reference variable.
  CoUninitialize();

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

namespace Adder
{
    public interface IAdder
    {
        double add(double a1, double a2);
    }
    public class Adder : IAdder
    {
        public Adder() { }
        public double add(double a1, double a2) { return a1 + a2; }
    }
}

Project settings

[assembly: ComVisible(true)]
[assembly: AssemblyDelaySign(false)]

(Signing was needed in order to be able to generate the tlb)

C++ code:

#import <adder.tlb> raw_interfaces_only

  CoInitialize(NULL);
  Adder::IAdderPtr a;
  a.CreateInstance( __uuidof( Adder::Adder ) );
  double d = 0;
  a->add(1.,1., &d); 
  // note: the method will return a HRESULT; 
  // the output is stored in a reference variable.
  CoUninitialize();
苍白女子 2024-11-14 08:54:01
  • GetProcAddress 不理解 C++ 名称修饰,更不用说任何其他修饰,因此不存在 "Class1::example_function1" 可以作为有效标识符的 dll。通常它仅与 extern "C" (或在不带 ++ 的 C 中实现)函数一起使用,这些函数不会被损坏。
  • 如果它是在 VB.NET 中实现的,它根本就不是一个 dll。它是一个 .net 程序集,您需要在 CLR(.net 运行时)中运行才能使用它。您可以在 CLR 中运行 C++ 代码,但它必须是“托管 C++”,它使用用于 .net 对象引用和垃圾收集器操作的特殊类型进行了扩展。
  • GetProcAddress does not understand C++ name mangling, not to mention any other mangling, so there is no dll for which "Class1::example_function1" could be valid identifier. Normally it's only used with extern "C" (or implemented in C without ++) functions, which are not mangled.
  • If it's implemented in VB.NET, it's not a dll at all. It's a .net assembly and you need to be running in CLR (.net runtime) to use it. You can run C++ code in CLR, but it has to be the "Managed C++", which is extended with special types for .net object references and operation with the garbage collector.
扎心 2024-11-14 08:54:01

您无法直接从本机 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.

ˇ宁静的妩媚 2024-11-14 08:54:01

您需要在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文