Accessing C++ functions/classes (built as C++/CLI) in Visual Basic

Ioannis Koutromanos 21 Reputation points
2022-06-09T20:53:48.323+00:00

Hello,

I have source code in C++ which I want to combine with a front end built with Visual Basic 2019. I imagine that I need to build a CLR project through Visual studio. What I have been unable to find/understand is how to create a Visual Basic project which can call the functions or use objects defined in my C++ code.

I have found this page:

https://learn.microsoft.com/en-us/cpp/dotnet/walkthrough-compiling-a-cpp-program-that-targets-the-clr-in-visual-studio?view=msvc-170

However, the page only explains how to build the C++/CLI code that targets the CRL. It does not explain how to invoke the program of the code with Visual Basic.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,834 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
{count} votes

Accepted answer
  1. Minxin Yu 12,506 Reputation points Microsoft Vendor
    2022-06-10T01:54:51.87+00:00

    Hi, @Ioannis Koutromanos

    You need to create a C++ CLR Class library.
    For example:

        #pragma once  
      
      
    using namespace System;  
    class myUnmanaged  
    {  
    public:  
        int a ;  
        myUnmanaged()  
        {  
            a = 2;  
        }  
    };  
    namespace CPPLibrary{  
        public ref class MyManagedCPP_Class  
        {  
        private:  
            myUnmanaged* myUnmanagedPtr =new myUnmanaged;  
      
        public:  
            MyManagedCPP_Class() { Console::WriteLine("hello C++!"); }  
            Void MyPrint()   
            {  
                Console::WriteLine(myUnmanagedPtr->a);  
            }  
              
        };  
    }  
    

    Call the class in VB project:

    Imports System  
    Imports CPPLibrary  
      
    Module Program  
        Sub Main(args As String())  
            Dim m As New MyManagedCPP_Class()  
            m.MyPrint()  
            Console.WriteLine("Hello World!")  
        End Sub  
    End Module  
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Ioannis Koutromanos 21 Reputation points
    2022-06-13T14:11:55.4+00:00

    Hi Minxin,

    Thank you for your detailed instructions.

    I tried creating a solution combining the two types of projects that I need. The problem is that, when I compile, I get the warning:

    warning MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "C:\Users\John\source\repos\WinFormsCallCppClass\Debug\CPPLibrary.dll", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.

    Then, when I run my VB code (in debug mode) and I try to create an object defined in the C++ class, the execution breaks with the error message:

    System.BadImageFormatException: 'Could not load file or assembly 'CPPLibrary, Version=1.0.8196.28320, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.'

    Do you know what could be the reason for the specific problem, and how to resolve it?


  2. Franz Etzweiler 0 Reputation points
    2023-05-12T12:32:26.36+00:00

    Works perfectly!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.