I have been programming in c++ exclusively at my university. This upcoming semester I am learning Visual Basic. Right now I am looking for a way to use my c++ code in Visual Basic, I have read that it is possible by making a shared library. But my question is should I even bother trying to use c++ with Visual Basic or just remake the code I have in c++?

Yes you can make libraries that interface with VB. The major changed is in the way strings are handled. Depending on the version of vb you want to use, here is an article by Microsoft that illustrates how to call DLL written in C by VB. Another article is here. If you use c++ it may b a little more difficult because c++ mangles names, so vb will have to know the mangled name.

If you want to use char* in the parameter list of the dll function then in VB you must declare String data type. VB lets you use data without declaring them and when that happens the are of type VARIANT (link) in C/C++.

Edited 3 Years Ago by Ancient Dragon

Then again where to want your future to go? If you expect to do a lot of cross platform programming then keeping your knowledge of C++ will be more important. If you expect to do strictly Windows, then relearning how to do things in VB will be more important. If you have the option you might want to look at C# it combines a lot of the C structure and syntax with Windows programming. And your libraries will more compatible with VB.

Edited 3 Years Ago by tinstaafl

Here is a very very simple example. AFAIK VB.NET can not call c++ functions or c++ classes because the c++ compiler will mangle the names. You need the *.def file to prevent the compiler from mangling C function names. Using __delspec(dllexport) isn't enough because VB will still do some name mangling. e.g. test function becomes _text@4. VB.NET hates the @4 in the name.

; vb.net program

Module Module1
    Private Declare Function test Lib "name and path of dll on my computer" (ByVal b As Integer) As Integer

    Sub Main()
        Dim b As Integer = 10
        Dim c As Integer
        c = test(b)
        Console.Write(c)
    End Sub

End Module

Below is the DLL

#include <windows.h>

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

    long _stdcall test(long a) 
    {
        return a * a;
    }

And this is the *.def file for the DLL projecgt

LIBRARY "name of the DLL goes here"
EXPORTS
   test

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.