-1

I am currently using BTS BioEngineering SDK (real-time EMG data acquisition device) for a lab project. According to its documentation, libraries (.dll) from this SDK were written in C# and can be used in C++ project by registering them with regasm.exe from .NET framework. Basic working sample projects were included in both languages.

What I would like to do: Create C++ project from scratch using these libraries in Visual Studio. Eventually, I would like to create a GUI with Qt to display data acquisition in real-time.

What works:

  • .dll files are registered successfully with Assembly Registration Tool (Regasm.exe). Registration is done in Prebuild event in Visual Studio configurations. .tlh and .tli filed are generated.
  • It build successfully without any error or warning
  • Types and objects are recognized in the IDE (eg. Definition of IPortCOMPtr is shown in Visual Studio as C++ typedef _com_ptr_t<_com_IIID<bts_biodaq_core::IPortCOM, &__uuidof(bts_biodaq_core::IPortCOM)>> bts_biodaq_core:: IPortCOMPtr)

Problem: I get an _com_error when running the program (Exit Code 3, Abort() is called) and I am not sure to understand why...

Here the code that causes the program to crash:

#include "stdafx.h"
#include <process.h>

#import "mscorlib.tlb" auto_rename
#import "bts.biodaq.drivers.tlb"
#import "bts.biodaq.core.tlb"

using namespace std;
using namespace mscorlib;
using namespace bts_biodaq_core;
using namespace bts_biodaq_drivers;

int _tmain(int argc, _TCHAR* argv[])
{
    USES_CONVERSION;
    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    IPortCOMPtr ptrCOMPort(__uuidof(PortCOM)); // _com_error here

    return 0;
}

Batch script (.cmd) execute in prebuild event that I used:

set REGASM_FOLDER="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\"
set IMPORT_NAME_1=bts.biodaq.drivers
set IMPORT_NAME_2=bts.biodaq.core

echo Libraries to register
echo %IMPORT_NAME_1%
echo %IMPORT_NAME_2%
echo --------

echo Registering BioDaq libraries %IMPORT_NAME_1%.dll and %IMPORT_NAME_2%.dll
%REGASM_FOLDER%RegAsm "%DEPENDENCIES_FOLDER%\%IMPORT_NAME_1%.dll" /tlb: "%DEPENDENCIES_FOLDER%\%IMPORT_NAME_1%.tlb"
%REGASM_FOLDER%RegAsm "%DEPENDENCIES_FOLDER%\%IMPORT_NAME_2%.dll" /tlb: "%DEPENDENCIES_FOLDER%\%IMPORT_NAME_2%.tlb"

echo Copying BioDaq type libraries %IMPORT_NAME_1%.tlb and %IMPORT_NAME_2%.tlb
call :FCOPY "%DEPENDENCIES_FOLDER%\mscorlib.tlb" "%SOURCE_FOLDER%"
call :FCOPY "%DEPENDENCIES_FOLDER%\%IMPORT_NAME_1%.tlb" "%SOURCE_FOLDER%"
call :FCOPY "%DEPENDENCIES_FOLDER%\%IMPORT_NAME_2%.tlb" "%SOURCE_FOLDER%"

goto END

Error message on execution:

Debug Error
Program: C:\Users\myname\source\repos\Test\Debug\Test.exe
abort() has been called

I'm totally new to C#, COM interface and Typed Librabry (.tlb) so the answer might be obvious for some of you. Any help would be greatly appreciate! I can add more codes or information if needed.

Thanks! :)

pwangb
  • 1
  • How do you know it is a `_com_error`? It's not immediately obvious from the error message you included in the question. Are there some additional messages somewhere? If it is a `_com_error`, how about catching it and printing its error message? – dratenik May 28 '21 at 15:04
  • Hi! When I add a breakpoint, it throws : `Unhandled exception at 0x7641A6E2 in Test.exe: Microsoft C++ exception: _com_error at memory location 0x0053FB7C.`. If I use a `try {...} catch (_com_error& e) { printf("%s", e.Description()) }`, (or e.ErrorMessage()) nothing appears in the console – pwangb May 28 '21 at 16:01
  • com error's Description is not always filled. What should always be filled is Error (HRESULT): https://docs.microsoft.com/en-us/cpp/cpp/com-error-error what is its value? – Simon Mourier May 28 '21 at 16:21
  • @SimonMourier If I use `std::string message = std::system_category().message(e.Error()); std::cout << message;`, it prints "Class not registered". However, it should have been registered during prebuild events. The build output I get is : *Types registered successfully 1> Assembly exported to 'C:\Users\myname\source\repos\Test\Dependencies\bts.biodaq.drivers.tlb', and the type library was registered successfully 1> Microsoft .NET Framework Assembly Registration Utility version 4.8.4084.0 1> for Microsoft .NET Framework version 4.8.4084.0* – pwangb May 28 '21 at 17:27
  • class not registered is the classical 0x80040154 error (google on it, you're not alone). it means... well.. the class with CLSID __uuidof(PortCOM) is not registered. It can be an x86 vs x64 issue. Your client is running as x86 and your .dll is registered as x64 or the reverse. Or use ProcMon from sysinternals and filter by registry to track what may be failing. – Simon Mourier May 28 '21 at 17:50
  • @SimonMourier It was indeed running in x86 but was registered for x64. Thank you for your insight! – pwangb May 28 '21 at 19:01
  • @pwangb - you should answer yourself – Simon Mourier May 29 '21 at 05:27

0 Answers0