c++.netvisual-studiodlldevops

How to get USB C++ SetupApi.h on a computer for a .NET application Installation to use


The story:

The specific issues

Example code: C++

#include <iostream>
#include <Windows.h>
#include <SetupAPI.h>
#include <cfgmgr32.h >
#include <initguid.h>
#include <usbiodef.h>
#include <usbioctl.h>
#include <combaseapi.h>
#include <winioctl.h>
#include <regex>

int UsbQuery::getUSBCount() {
    //error is thrown from the below code
    HDEVINFO usbInfo = SetupDiGetClassDevs(&GUID_CLASS_USB_DEVICE, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); 

    SP_DEVINFO_DATA deviceData;
    deviceData.cbSize = sizeof(SP_DEVINFO_DATA);

    int i = 0;
    while (SetupDiEnumDeviceInfo(usbDeviceInfoSet, i, &deviceData))
    {
        i++;
    }
    return i;
}

C#

    [DllImport(_dllImportPath, CallingConvention = CallingConvention.Cdecl, EntryPoint = "GetUSBCount", CharSet = CharSet.Auto)]
    public static extern int GetUSBCount();

What has worked

The one way i have gotten this to work is by installing Visual Studio on the PC the software is deployed to. However, this is obviously not the ultimate solution, but has lead me down the to a path of knowing there is a way to fix it. If I install Visual Studio Community with the following package (and nothing else), the software runs without error. enter image description here

This package also installs the following SDK: enter image description here When I uninstall this sdk package, the deployed software fails to find the setupAPI.h. Leading me to guess that something installed with this package includes the link needed to find SetupApi.

What I have tried

The software is not really deployed in numerous locations, and there is already some hacky solutions for other aspects of its installation. So if I can copy a folder somewhere, and setup some hacky link to call it, that works too. The main thing is that I obviously cannot install Visual Studio on every computer. If there is a way to bundle my C++ project DLL to be large and include all the requirements, that is a viable solution. But i have been unsuccessful in figuring out how to compile a C++ project DLL to bundle everything together.
If I can copy the needed *.h, *.lib, or *.dll files as an "Embedded Resource" of the Project, that is acceptable.

Deployment is done via the UWP deployment. We utilize a cloud build script to actually build and store the install files. But when we setup a new PC there are many drivers and custom installs we need, to get everything working. The need for an automated install isnt that important as each deployment already requires decent manual involvement.


Solution

  • As AhmedAEK's comment led to the solution. The main issue that could help anyone else with a similar issue, is to make sure to compile the VC C++ projects in Visual Studio to "Release" mode or else they will not work.