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

Ioannis Koutromanos 21 Reputation points


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:

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.

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.
2,742 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,959 questions
{count} votes

Accepted answer
  1. Minxin Yu 3,861 Reputation points Microsoft Vendor

    Hi, @Ioannis Koutromanos

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

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

    Call the class in VB project:

    Imports System  
    Imports CPPLibrary  
    Module Program  
        Sub Main(args As String())  
            Dim m As New MyManagedCPP_Class()  
            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.

1 additional answer

Sort by: Newest
  1. Ioannis Koutromanos 21 Reputation points

    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?