0

I have a dll generated from a vb project, that i want to use in Cpp project, my question is that's possible, and how? I tried to Load my dll and call my function as i do with Cpp dll but it doesn't word , here is the code i tried :

#ifdef __cplusplus
extern "C" {
#endif

    
    typedef short (CALLBACK* GetMotor1Position)();

#ifdef __cplusplus
}
#endif

int main(int argc, char *argv[])

BOOL freeResult, runTimeLinkSuccess = FALSE;
GetMotor1Position MotorPosPtr = NULL;

    HINSTANCE hGetProcIDDLL = LoadLibrary(_T("C:/Users/Downloads/Vb/Vb-DLL/bin/Debug/Mydll.dll"));
    if (hGetProcIDDLL == NULL) {
        qDebug() << "Library MyLibrary.dll not found";
      
    }
    if (NULL != hGetProcIDDLL) {
        MotorPosPtr = (GetMotor1Position)GetProcAddress(hGetProcIDDLL,
            "GetMotor1Speed");    
    
        if (runTimeLinkSuccess = (NULL != MotorPosPtr))
        {
                short a = GetMotor1Position () ; // he never get access here, bcs MotorPosPtr still NULL
        }
        freeResult = FreeLibrary(hGetProcIDDLL);
    }

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Soukaina
  • 1
  • 3
  • Is it a .NET assembly DLL? Then you could try to create a managed C++/CLI project and import it like any other .NET assembly. – Some programmer dude Mar 28 '23 at 11:32
  • So `MotorPosPtr` was null? Do you know what the export is actually called in the DLL file? it might not be `MotorPosPtr`, it could for example be something like `_MotorPosPtr@0` – user253751 Mar 28 '23 at 12:16
  • @user253751 MotorPosPtr is a pointer to my function GetMotor1Position, in .vb code the function has that name, and in my dll too – Soukaina Mar 28 '23 at 12:39
  • sorry, I meant it could be called `_GetMotor1Position@0` for example. Apparently there are [some tools](https://stackoverflow.com/questions/4438900/how-to-view-dll-functions) that can view exported functions in a DLL file? – user253751 Mar 28 '23 at 12:44
  • VB does not produce a normal native code DLL, so you can't just grab a pointer to a function from it and expect it to work. You'll need to either use managed code in C++ or make your VB support COM and use COM on the C++ side. – Craig Mar 28 '23 at 13:42
  • 1
    There are a lot of existing Q+A about this. The language doesn't matter after the vb.net compiler is done with it. Find them by googling "call managed code from native c++". – Hans Passant Mar 28 '23 at 14:26

0 Answers0