Search code examples
c#c++winformstarget-platform

Cannot run Winforms application with C++ library on Windows 7 and 8.1


I have a WinForms application which has a C++ library called DeviceAccess. The C# libraries are targeting the .NET Framework 4.5.1 (x86), and my C++ library is targeting Windows SDK Version 8.1 and Platform Toolset Visual Studio 2017 (v141) and Win32. I am building the application on my Windows 10 machine and trying to run through virtual machines of Windows 8.1 and Windows 7.

enter image description here

I've already set those two defines inside targetver.h, which looks like:

#pragma once

#define WINVER 0x0601
#define _WIN32_WINNT 0x0601 

#include <SDKDDKVer.h>

But, still when I open the application through my virtual machine of Windows 8.1 or Windows 7, I get the following error:

Could not load file or assembly '...DeviceAccess.dll' or one of its dependencies. The specified module could no be found.

I have the .dll at the same folder as the application.

Someone have any idea why I cannot load the C++ library, please?

EDIT: I run the Process Monitor and filtered the missing DLL, as shown below:

enter image description here

I feel like the missing VCRUNTIME140.dll might be the issue?

EDIT2:

From Dependecy Walker I got those error when loading that DLL:

enter image description here


Solution

  • So I fix my own problem. I had a WiX setup installer with the following commands:

    <DirectoryRef Id="TARGETDIR">
        <Merge Id="VCRedist" SourceFile="..\MergeModules\Microsoft_VC140_CRT_x86.msm" DiskId="1" Language="0"/>
        <Merge Id="VCRedist2" SourceFile="..\MergeModules\Microsoft_VC140_CXXAMP_x86.msm" DiskId="1" Language="0"/>
    </DirectoryRef>
    
    <Feature Id="VCRedist" Title="Visual C++ 14.0 Runtime" AllowAdvertise="no" Display="hidden" Level="1">
        <MergeRef Id="VCRedist"/>
        <MergeRef Id="VCRedist2"/>
    </Feature>
    

    Which deploys the msvcp140.dll and vcruntime140.dll. However I was still missing the api dlls of the Universal CRT. So I use this approach:

    Updated September 11, 2015: App-local deployment of the Universal CRT is supported. To obtain the binaries for app-local deployment, install the Windows Software Development Kit (SDK) for Windows 10. The binaries will be installed to C:\Program Files (x86)\Windows Kits\10\Redist\ucrt. You will need to copy all of the DLLs with your app (note that the set of DLLs are necessary is different on different versions of Windows, so you must include all of the DLLs in order for your program to run on all supported versions of Windows).

    Thus, after I copied all the binaries files from x86 to my application folder, I was able to run it on the Windows 8.1 VM.

    EDIT: For those who are curious about how to deploy the Universal CRT dlls in the WiX setup installer, I've followed this.