How to make C++ project for visual studio 2019

Hello,

I am trying to make a C++ project for Visual Studio 2019 to open and monitor CAN message from USB to CAN Compact V2.

I did check SDK and C++ design guide but this simple code gives error below.

"Error LNK2019 unresolved external symbol VciGetDeviceManager referenced in function “long __cdecl SelectDevice(int)” (?SelectDevice@@YAJH@Z) usbcan_cpp
"

HRESULT SelectDevice(BOOL fUserSelect)
{
    HRESULT hResult = 0; // error code

    if (fUserSelect == FALSE)
    {
        IVciDeviceManager* pDevMgr = 0;    // device manager
        IVciEnumDevice* pEnum = 0;    // enumerator handle
        VCIDEVICEINFO       sInfo;          // device info

        hResult = VciGetDeviceManager(&pDevMgr);
        if (hResult == VCI_OK)
        {
            hResult = pDevMgr->EnumDevices(&pEnum);
        }

        //
        // retrieve information about the first
        // device within the device list
        //
        if (hResult == VCI_OK)
        {
            hResult = pEnum->Next(1, &sInfo, NULL);

            //
            // close the device list (no longer needed)
            //
            pEnum->Release();
            pEnum = NULL;
        }

        //
        // open the first device via device manager and get the bal object
        //
        if (hResult == VCI_OK)
        {
            IVciDevice* pDevice;
            hResult = pDevMgr->OpenDevice(sInfo.VciObjectId, &pDevice);

            if (hResult == VCI_OK)
            {
                hResult = pDevice->OpenComponent(CLSID_VCIBAL, IID_IBalObject, (void**)&pBalObject);

                pDevice->Release();
            }
        }

        //
        // always select controller 0
        //
        lBusCtlNo = 0;

        //
        // close device manager
        //
        if (pDevMgr)
        {
            pDevMgr->Release();
            pDevMgr = NULL;
        }
    }
    else
    {
        //
        // open a device selected by the user
        //
        //hResult = SocketSelectDlg(NULL, VCI_BUS_CAN, &pBalObject, &lSocketNo, &lBusCtlNo);
    }

    //DisplayError(NULL, hResult);
    return hResult;
}

I tried to import everything in the sample sdk and copied above piece of code.
Any solutions or other example code that I can try?

I finally managed to build and run the code by modifying the property of solution.
Now I am trying to pack this whole socket open and receive thread inside a class but it is not easy task.
Is there any such example?

For your first question about the error. Did you include VCIAPI.lib?

MicrosoftTeams-image

The LIB has the function “VciGetDeviceManager”.

“c:\Program Files\HMS\Ixxat VCI\sdk\vci\lib\x32\release\vciapi.lib”

There are sample code here:
VCIConsoleSample.zip (33.1 KB)

For your second question, I’m not sure I understand exactly. Do you want a complete C++ example with ready to use classes or do you need only classes for opening and closing the socket, and receiving data?

Hi @kyle_HMS
Thanks for your comment and the sample code.
I actually working with the sample sdk and found it needs some improvements for application development.
Below repository is my own wrapper class for the C API which utilizes callback and C++11 standard thread.

I’m not a C++ expert and mostly done by searching from stackoverflow or google so there could be some bugs and errors in it.

Kyuhyong

Thanks for sharing that @Kyuhyong_You!
The examples are only to show the usage. How to use it it depends on the programming style, program architecture and what to do, but I’ll share this with the developers for them to review and maybe they can make some improvements.

1 Like