Unable to Call Simple Custom Dll Import from C# Application - c#

Update
I have now run through the tutorial 3 times with the same results (Visual Studio/Windows is unable to recognise the DLL as a valid file). This must be an issue with my environment, as mike below ran through the tutorial and it worked fine.
I did notice this in the C++ project's solution explorer on my last run:
Does anyone know if this is okay? There seems to be a lot of Red stop-signs which to me would suggest something bad...
Following this tutorial, I have ended up with the following:
C#
using System.Runtime.InteropServices;
namespace TestImport
{
class Program
{
[DllImport("TestLib.dll")]
public static extern void DisplayHelloFromDLL();
static void Main(string[] args)
{
DisplayHelloFromDLL();
}
}
}
C++
In .cpp file Source Files
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf("Hello from DLL !\n");
}
}
When debugging in Visual Studio:
An unhandled exception of type 'System.DllNotFoundException' occurred in TestImport.exe
Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
When running the built EXE (from he same folder as the Dll):
An attempt was made to load a program with an incorrect format.
Have I done anything wrong in the above code?
I did do all of this in VS 2015, where the tutorial calls for VS 2010, howeve rI couldn't find a more up-to-date tutorial.
When I try to add the project as a reference by Project -> Add Reference ... I am seeing this error:
A reference to 'Path\To\MyLib.dll' could not be added. Please make sure
that the file is accessible, and that it is a valid assembly or COM
component.
Which makes me think that the issue is either with the source code or the DLLs configuration.
I have also tried building both files and putting them in the same folder before running the Executable, but without any improved results (same error as when running the EXE above).
Solution Explorer Screenshot:

Make sure the name of your C++ project and DllImport parameter are the same. Also, if the dll file is not located in the same directory as your C# project, make sure to add the dll into your Debug/Release folder.
Also! Make sure that you are running in the same 32/64 bit mode when compiling.. if you compiled the dll using 32 bit and try to use it in a 64 bit C# program, it won't work.

OK, now build this application, and then copy the previously built DLL into the Debug/Release directory of the current application. The DLL should be in the same directory as your main application.
Did you do the bolded text? Your C# app won't know where to look for the DLL and it looks in the build folder, by default.

An unhandled exception of type 'System.DllNotFoundException'
It happened to me, because of the imported c++ dll, its own dependency dlls were missing.
After copying them into Debug/Release folder everything worked.

Related

How to fix Error HRESULT 0x8007007E on windows 10 x64 [duplicate]

I have a dll library with unmanaged C++ API code I need to use in my .NET 4.0 application. But every method I try to load my dll I get an error:
Unable to load DLL 'MyOwn.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
I have read and tried several solutions I have found on the internet. Nothing works..
I have tried using following methods:
[DllImport("MyOwn.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs((UnmanagedType.I4))]
public static extern Int32 MyProIni(string DBname, string DBuser_pass,
string WorkDirectory, ref StringBuilder ErrorMessage);
When I tried following this article and when I run this example (from the downloaded code) it runs without a problem (the dll used is in the bin/debug folder)
I have copied my dll (along with all the files the it depends on into my bin folder).
I also tried this approach but got the same error:
[DllImportAttribute(MyOwnLibDllPath, EntryPoint="TMproIni")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int MyproIni(string DBname, string DBuser_pass,
string WorkDirectory, ref StringBuilder ErrorMessage);
Any suggestions?
From what I remember on Windows the search order for a dll is:
Current Directory
System folder, C:\windows\system32 or c:\windows\SysWOW64 (for 32-bit process on 64-bit box).
Reading from the Path environment variable
In addition I'd check the dependencies of the DLL, the dependency walker provided with Visual Studio can help you out here, it can also be downloaded for free: http://www.dependencywalker.com
You can use the dumpbin tool to find out the required DLL dependencies:
dumpbin /DEPENDENTS my.dll
This will tell you which DLLs your DLL needs to load. Particularly look out for MSVCR*.dll. I have seen your error code occur when the correct Visual C++ Redistributable is not installed.
You can get the "Visual C++ Redistributable Packages for Visual Studio 2013" from the Microsoft website. It installs c:\windows\system32\MSVCR120.dll
In the file name, 120 = 12.0 = Visual Studio 2013.
Be careful that you have the right Visual Studio version (10.0 = VS 10, 11 = VS 2012, 12.0 = VS 2013...) right architecture (x64 or x86) for your DLL's target platform, and also you need to be careful around debug builds. The debug build of a DLL depends on MSVCR120d.dll which is a debug version of the library, which is installed with Visual Studio but not by the Redistributable Package.
The DLL has to be in the bin folder.
In Visual Studio, I add the dll to my project NOT in References, but "Add existing file". Then set the "Copy to Output Directory" Property for the dll to "Copy if newer".
This is a 'kludge' but you could at least use it to sanity-test:
Try hard-coding the path to the DLL in your code
[DllImport(#"C:\\mycompany\\MyDLL.dll")]
Having said that; in my case running dumpbin /DEPENDENTS as suggested by #anthony-hayward, and copying over 32-bit versions of the DLLs listed there into my working directory solved this problem for me.
The message is just a bit misleading, becuase it isn't "my" dll that can't be loaded - it's the dependencies
Try to enter the full-path of the dll.
If it doesn't work, try to copy the dll into the system32 folder.
"Unable to load DLL 'xxx.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" means the file CAN be found BUT it's not able to load it. Try to copy the DLL file to the root folder of your application, some DLL libraries need to be available in the root folder of the application in order for it to work. Or check if there are any other depending DLL files required by it.
"Cannot find DLL 'xxx.dll': ..." means the file CANNOT be found. Try to check the path. For example, [DllImport(#"\Libraries\Folder\xxx.dll")]
Ensure that all dependencies of your own dll are present near the dll, or in System32.
Turn on the fusion logging, see this question for lots of advice on how to do that. Debugging mixed-mode apps loading problems can be a right royal pain. The fusion logging can be a big help.
Make sure you set the Build Platform Target to x86 or x64 so that it is compatible with your DLL - which might be compiled for a 32 bit platform.
There is one very funny thing (and has a technical relevance) which might waste your hours so thought of sharing it here -
I created a console application project ConsoleApplication1 and a class library project ClassLibrary1.
All the code which was making the p/invoke was present in ClassLibrary1.dll. So before debugging the application from visual studio I simply copied the C++ unmanaged assembly (myUnmanagedFunctions.dll) into the \bin\debug\ directory of ClassLibrary1 project so that it can be loaded at run-time by the CLR.
I kept getting the
Unable to load DLL
error for hours. Later on I realized that all such unmanaged assemblies which are to be loaded need to be copied into the \bin\debug directory of the start-up project ConsoleApplication1 which is usually a win form, console or web application.
So please be cautious the Current Directory in the accepted answer actually means Current Directory of main executable from where you application process is starting. Looks like an obvious thing but might not be so at times.
Lesson Learnt - Always place the unamanaged dlls in the same directory as the start-up executable to ensure that it can be found.
I had the same problem when I deployed my application to test PC. The problem was development PC had msvcp110d.dll and msvcr110d.dll but not the test PC.
I added "Visual Studio C++ 11.0 DebugCRT (x86)" merge module in InstalledSheild and it worked. Hope this will be helpful for someone else.
In my case one unmanaged dll was depending on another which was missing. In that case the error will point to the existing dll instead of the missing one which can be really confusing.
That is exactly what had happen in my case. Hope this helps someone else.
If the DLL and the .NET projects are in the same solution and you want to compile and run both every time, you can right click the properties of the .NET project, Build events, then add something like the following to Post-build event command line:
copy $(SolutionDir)Debug\MyOwn.dll .
It's basically a DOS line, and you can tweak based on where your DLL is being built to.
I think your unmanaged library needs a manifest.
Here is how to add it to your binary. and here is why.
In summary, several Redistributable library versions can be installed in your box but only one of them should satisfy your App, and it might not be the default, so you need to tell the system the version your library needs, that's why the manifest.
Setup: 32-bit Windows 7
Context: Installed a PCI-GPIB driver that I was unable to communicate through due to the aforementioned issue.
Short Answer: Reinstall the driver.
Long Answer:
I also used Dependency Walker, which identified several missing dependency modules. Immediately, I thought that it must have been a botched driver installation. I didn't want to check and restore each missing file.
The fact that I was unable to find the uninstaller under Programs and Features of the Control Panel is another indicator of bad installation. I had to manually delete a couple of *.dll in \system32 and registry keys to allow for driver re-installation.
Issue fixed.
The unexpected part was that not all dependency modules were resolved. Nevertheless, the *.dll of interest can now be referenced.
I have come across the same problem, In my case I had two 32 bit pcs.
One with .NET4.5 installed and other one was fresh PC.
my 32-bit cpp dll(Release mode build) was working fine with .NET installed PC but Not with fresh PC where I got the below error
Unable to load DLL 'PrinterSettings.dll': The specified module could not be
found. (Exception from HRESULT: 0x8007007E)
finally,
I just built my project in Debug mode configuration and this time my
cpp dll was working fine.
Also faced the same problem when using unmanaged c/c++ dll file in c# environment.
1.Checked the compatibility of dll with 32bit or 64bit CPU.
2.Checked the correct paths of DLL .bin folder, system32/sysWOW64 , or given path.
3.Checked if PDB(Programme Database) files are missing.This video gives you ans best
undestand about pdb files.
When running 32-bit C/C++ binary code in 64bit system, could arise this because of platform incompatibility. You can change it from Build>Configuration manager.
I faced the same problem when import C++ Dll in .Net Framework +4, I unchecked Project->Properties->Build->Prefer 32-bit and it solved for me.
It has nothing to do with dependencies if you checked all dependencies and you know you got them all, it has nothing to do with the file being in the wrong directory either or incorrect ARGUMENTS passed to dll, the DLL Fails to load using LoadLibrary itself.. you could check the address returned from LoadLibrary is always 0x0000000 (not loaded).
I couldn't figure this error out either it worked fine on Windows 7, but on Windows 10 it doesn't work. I fixed the problem though it had nothing to do with missing dependencies or Runtime redistributable packs.
The problem was I had to pack the DLL with upx and it started working again.
Something with the file being unpacked and compiled on old Windows XP operating system created a bad PE Header or Bad file format or something, but packing it with UPX did the trick works fine now and the DLL got 3x smaller haha.
I got this error for one C++ project in our solution, and only on our buildmaster's machine. The rest of us could build it with no problem.
In our case it was because that particular project had <WindowsTargetPlatformVersion> in the .vcxproj file set to "10.0" vs. "10.0.18362.0" as in all our other C++ projects.
Not specifying the entire SDK version number seems to have allowed MSBuild to choose the newest(?) SDK and associated build tools.
Our buildmaster likely had the remnants of a newer SDK on his machine, and MSBuild was trying to use it (and thus RC.exe was not found).
In any case, bringing up the project's property page and changing Configuration Properties > General > Windows SDK Version to "10.0.18362.0" (or whichever specific version of the SDK you have installed) for all of the project's configurations/platforms did the trick.

Import and use C++ DLL functions in C# application

I am developing an application that communicates with some hardware using the C++ DLL provided by the hardware provider. I also have a header file for this DLL, but no source file. In my C# application, I'm trying to import and call the DLL's functions like this:
public class MyClass
{
[DllImport("CaGe.dll")]
public static extern long someLibraryFunction();
public void callLibFunction()
{
Console.WriteLine("function call: " + someLibraryFunction());
}
}
The library.dll file is located in the same folder as the MyClass.cs file and also the rest of the project files. When I start the debugging session however, I first receive a dialog window from system saying: CAGE InitDll: Erro on load library "CdbClientInit" and after I click OK on that, an unhalted exception on the line where I call the library function occours.
The exception says: System.DllNotFoundException: Exception from HRESULT: 0x8007045A
I found on the internet that this may be caused by a missing DLL that the CaGe.dll needs, so I tried to put the DLL through the Dependency Walker, but the results are a little bit confusing for me. Under the CAGE.DLL there are WS32_32.DLL, VERSION.DLL, KERNEL.DLL and USER32.DLL tabs, I susspect that the libraries I need are somwhere under the USER32.DLL tab, but I don't recognize any of them (if a missing DLL is what is causing this problem).
Any advice would be much appretiated :)
More help to use C++ function in C# please check below URL
http://www.codeproject.com/Questions/107152/Using-a-C-dll-in-a-C-application

Using DllImport to load unmanaged dll into managed application

In my project I have an unmanaged native C++ dll and a C# application. I am trying to import a function from the unmanaged dll using DllImport but I keep getting a DllNotFoundException.
Here is my code that calls the DLL.
using System.Runtime.InteropServices;
namespace TestApp
{
public delegate void UpdateDelegate(string s);
class Program
{
[DllImport("CGPUnmanagedLibrary.dll")]
internal static extern int parse_raw_gsod_file(
[MarshalAs(UnmanagedType.LPStr)]
string filePath,
int minTemp,
UpdateDelegate callBack);
static void Main(string[] args)
{
UpdateDelegate myCallBack = new UpdateDelegate(Program.Report);
string path = #"C:\Creative Solutions\Projects\Clyde's Garden Planner\Frost Data Database\GSOD Data\GSOD_RAW_DATA\1992\gsod_1992.txt";
int result = parse_raw_gsod_file(path, 32, myCallBack);
Console.Write("Parse completed with exit code: " + result.ToString());
Console.ReadLine();
} // end main function
public static void Report(string msg)
{
Console.Write("Message is ");
Console.WriteLine(msg);
}
} // End class
} // end namespace
I tried copying the DLL to the app output directory but it still can't find it. I also tried adding the DLL project as a reference but I get a popup window saying it can't be added. How do you properly link an unmanged DLL to a managed application?
Update - Here is the full error:
Unable to load DLL 'CGPUnmanagedLibrary': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Update 2 - I know for sure that the DLL is in the same directory as the .exe trying to load it. This makes me think there is a dependency in the DLL that isn't getting loaded. I'm only using basic C++ libraries in the DLL (string, math, iostream, etc). Any ideas what could not be loading and why?
Update 3 - Tested with Dependency Walker
Loading my unmanaged C++ DLL in dependency walker showed no errors. I also tried to open my executable in dependency walker and it showed errors loading two DLLs: GPSVC.DLL and IESHIMS.DLL - doesn't make any sense because I am only using standard c++ libraries in my code. I think it may have something to do with the fact that I have a managed C++/CLI DLL trying to load the unmanaged DLL as well (I was trying to implement some C++/CLI wrappers). Anyway, I have since started a new VS solution and moved on. See my answer.
In all likelihood the problem isn't the DLL you're trying to load but one of its (chained) dependencies. Run depends.exe or a similar utility on the DLL to see if all the dependencies can be found. The misleading message "The specified module could not be found" has become a classic annoyance (if not FAQ material!): it leads you to think that your DLL is not being found when almost all of the time it's one of its dependencies that's not being found.
To test, the *.dll needs to be in the same directory as the .exe that is trying to load it. Don't trust Visual Studio to do it for you at this point. Physically copy the file to C:******\Debug\x86\bin\ or whichever configuration you are running under. If in doubt, copy it to all of your bin folders. After you figure out the path, then you start finding ways to automate the project build to copy the file correctly. If that doesn't do it, put it in system32--it will certainly find it there. However, if after doing these things, you still can't find it. There is probably a dependency to your unmanaged dll that is also missing.
First I want to thank everyone for their help. Unfortunately I never did solve this issue (see edits in my main question). The answer turned out to be starting a totally new Visual Studio solution and creating two new projects: C# app and C++ dll. I did away with the need for wrappers as I am now just marshaling two main functions.
Thanks again.
The anwer of user arayq2 made much sense and I was quickly able to solve my problem also.
The dll that couldn't be loaded (DllNotFoundException) in my case is depending upon another dll. This dll (that is not part of my project) was actually compiled with newer versions of certain .h and .lib files. Older versions of these .h and .lib files (with the same filename) were part of the project that compiled the dll that couldn't be loaded.
After I updated my dll project with the newer versions of these .h and .lib files and recompiling my dll project, my problem was solved.
Thank you arayq2!

Unable to load DLL (Module could not be found HRESULT: 0x8007007E)

I have a dll library with unmanaged C++ API code I need to use in my .NET 4.0 application. But every method I try to load my dll I get an error:
Unable to load DLL 'MyOwn.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
I have read and tried several solutions I have found on the internet. Nothing works..
I have tried using following methods:
[DllImport("MyOwn.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs((UnmanagedType.I4))]
public static extern Int32 MyProIni(string DBname, string DBuser_pass,
string WorkDirectory, ref StringBuilder ErrorMessage);
When I tried following this article and when I run this example (from the downloaded code) it runs without a problem (the dll used is in the bin/debug folder)
I have copied my dll (along with all the files the it depends on into my bin folder).
I also tried this approach but got the same error:
[DllImportAttribute(MyOwnLibDllPath, EntryPoint="TMproIni")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern int MyproIni(string DBname, string DBuser_pass,
string WorkDirectory, ref StringBuilder ErrorMessage);
Any suggestions?
From what I remember on Windows the search order for a dll is:
Current Directory
System folder, C:\windows\system32 or c:\windows\SysWOW64 (for 32-bit process on 64-bit box).
Reading from the Path environment variable
In addition I'd check the dependencies of the DLL, the dependency walker provided with Visual Studio can help you out here, it can also be downloaded for free: http://www.dependencywalker.com
You can use the dumpbin tool to find out the required DLL dependencies:
dumpbin /DEPENDENTS my.dll
This will tell you which DLLs your DLL needs to load. Particularly look out for MSVCR*.dll. I have seen your error code occur when the correct Visual C++ Redistributable is not installed.
You can get the "Visual C++ Redistributable Packages for Visual Studio 2013" from the Microsoft website. It installs c:\windows\system32\MSVCR120.dll
In the file name, 120 = 12.0 = Visual Studio 2013.
Be careful that you have the right Visual Studio version (10.0 = VS 10, 11 = VS 2012, 12.0 = VS 2013...) right architecture (x64 or x86) for your DLL's target platform, and also you need to be careful around debug builds. The debug build of a DLL depends on MSVCR120d.dll which is a debug version of the library, which is installed with Visual Studio but not by the Redistributable Package.
The DLL has to be in the bin folder.
In Visual Studio, I add the dll to my project NOT in References, but "Add existing file". Then set the "Copy to Output Directory" Property for the dll to "Copy if newer".
This is a 'kludge' but you could at least use it to sanity-test:
Try hard-coding the path to the DLL in your code
[DllImport(#"C:\\mycompany\\MyDLL.dll")]
Having said that; in my case running dumpbin /DEPENDENTS as suggested by #anthony-hayward, and copying over 32-bit versions of the DLLs listed there into my working directory solved this problem for me.
The message is just a bit misleading, becuase it isn't "my" dll that can't be loaded - it's the dependencies
Try to enter the full-path of the dll.
If it doesn't work, try to copy the dll into the system32 folder.
"Unable to load DLL 'xxx.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)" means the file CAN be found BUT it's not able to load it. Try to copy the DLL file to the root folder of your application, some DLL libraries need to be available in the root folder of the application in order for it to work. Or check if there are any other depending DLL files required by it.
"Cannot find DLL 'xxx.dll': ..." means the file CANNOT be found. Try to check the path. For example, [DllImport(#"\Libraries\Folder\xxx.dll")]
Ensure that all dependencies of your own dll are present near the dll, or in System32.
Turn on the fusion logging, see this question for lots of advice on how to do that. Debugging mixed-mode apps loading problems can be a right royal pain. The fusion logging can be a big help.
Make sure you set the Build Platform Target to x86 or x64 so that it is compatible with your DLL - which might be compiled for a 32 bit platform.
There is one very funny thing (and has a technical relevance) which might waste your hours so thought of sharing it here -
I created a console application project ConsoleApplication1 and a class library project ClassLibrary1.
All the code which was making the p/invoke was present in ClassLibrary1.dll. So before debugging the application from visual studio I simply copied the C++ unmanaged assembly (myUnmanagedFunctions.dll) into the \bin\debug\ directory of ClassLibrary1 project so that it can be loaded at run-time by the CLR.
I kept getting the
Unable to load DLL
error for hours. Later on I realized that all such unmanaged assemblies which are to be loaded need to be copied into the \bin\debug directory of the start-up project ConsoleApplication1 which is usually a win form, console or web application.
So please be cautious the Current Directory in the accepted answer actually means Current Directory of main executable from where you application process is starting. Looks like an obvious thing but might not be so at times.
Lesson Learnt - Always place the unamanaged dlls in the same directory as the start-up executable to ensure that it can be found.
I had the same problem when I deployed my application to test PC. The problem was development PC had msvcp110d.dll and msvcr110d.dll but not the test PC.
I added "Visual Studio C++ 11.0 DebugCRT (x86)" merge module in InstalledSheild and it worked. Hope this will be helpful for someone else.
In my case one unmanaged dll was depending on another which was missing. In that case the error will point to the existing dll instead of the missing one which can be really confusing.
That is exactly what had happen in my case. Hope this helps someone else.
If the DLL and the .NET projects are in the same solution and you want to compile and run both every time, you can right click the properties of the .NET project, Build events, then add something like the following to Post-build event command line:
copy $(SolutionDir)Debug\MyOwn.dll .
It's basically a DOS line, and you can tweak based on where your DLL is being built to.
I think your unmanaged library needs a manifest.
Here is how to add it to your binary. and here is why.
In summary, several Redistributable library versions can be installed in your box but only one of them should satisfy your App, and it might not be the default, so you need to tell the system the version your library needs, that's why the manifest.
Setup: 32-bit Windows 7
Context: Installed a PCI-GPIB driver that I was unable to communicate through due to the aforementioned issue.
Short Answer: Reinstall the driver.
Long Answer:
I also used Dependency Walker, which identified several missing dependency modules. Immediately, I thought that it must have been a botched driver installation. I didn't want to check and restore each missing file.
The fact that I was unable to find the uninstaller under Programs and Features of the Control Panel is another indicator of bad installation. I had to manually delete a couple of *.dll in \system32 and registry keys to allow for driver re-installation.
Issue fixed.
The unexpected part was that not all dependency modules were resolved. Nevertheless, the *.dll of interest can now be referenced.
I have come across the same problem, In my case I had two 32 bit pcs.
One with .NET4.5 installed and other one was fresh PC.
my 32-bit cpp dll(Release mode build) was working fine with .NET installed PC but Not with fresh PC where I got the below error
Unable to load DLL 'PrinterSettings.dll': The specified module could not be
found. (Exception from HRESULT: 0x8007007E)
finally,
I just built my project in Debug mode configuration and this time my
cpp dll was working fine.
Also faced the same problem when using unmanaged c/c++ dll file in c# environment.
1.Checked the compatibility of dll with 32bit or 64bit CPU.
2.Checked the correct paths of DLL .bin folder, system32/sysWOW64 , or given path.
3.Checked if PDB(Programme Database) files are missing.This video gives you ans best
undestand about pdb files.
When running 32-bit C/C++ binary code in 64bit system, could arise this because of platform incompatibility. You can change it from Build>Configuration manager.
I faced the same problem when import C++ Dll in .Net Framework +4, I unchecked Project->Properties->Build->Prefer 32-bit and it solved for me.
It has nothing to do with dependencies if you checked all dependencies and you know you got them all, it has nothing to do with the file being in the wrong directory either or incorrect ARGUMENTS passed to dll, the DLL Fails to load using LoadLibrary itself.. you could check the address returned from LoadLibrary is always 0x0000000 (not loaded).
I couldn't figure this error out either it worked fine on Windows 7, but on Windows 10 it doesn't work. I fixed the problem though it had nothing to do with missing dependencies or Runtime redistributable packs.
The problem was I had to pack the DLL with upx and it started working again.
Something with the file being unpacked and compiled on old Windows XP operating system created a bad PE Header or Bad file format or something, but packing it with UPX did the trick works fine now and the DLL got 3x smaller haha.
I got this error for one C++ project in our solution, and only on our buildmaster's machine. The rest of us could build it with no problem.
In our case it was because that particular project had <WindowsTargetPlatformVersion> in the .vcxproj file set to "10.0" vs. "10.0.18362.0" as in all our other C++ projects.
Not specifying the entire SDK version number seems to have allowed MSBuild to choose the newest(?) SDK and associated build tools.
Our buildmaster likely had the remnants of a newer SDK on his machine, and MSBuild was trying to use it (and thus RC.exe was not found).
In any case, bringing up the project's property page and changing Configuration Properties > General > Windows SDK Version to "10.0.18362.0" (or whichever specific version of the SDK you have installed) for all of the project's configurations/platforms did the trick.

Unable to load DLL ' mydll.dll': The specified module could not be found

On my laptop, where I am developing the WPF application, everything works fine, debug and launch the .exe app.
My app uses a native DLL, for resolve the reference problem I add the DLL in bin/debug (release) folder. I access it using DllImport like this:
[DllImport("xptodll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int LDA_About();
The problem is when I try to run the .exe app on another PC, when I need to access the DLL it crashes. I make a handle to log any unhandled exceptions and the following error appears:
Unable to load DLL ' xptodll.dll': The specified module could not be
found. Exception from HRESULT: 0x8007007E)
The bin/debug directory has xptodll.dll, and app files: .exe, .application, .exe.config, .exe.manifest, .pdb.
Maybe this is important, the xptodll.dll interacts with hardware, but why wouldn´t it have the same behaviour on both machines?
There is probably some further dependency that is failing. My guess is that xptodll.dll itself has dependencies on other libraries that are missing on the failing machine. The documentation of xptodll.dll should explain what dependencies are needed. If the documentation does not make it obvious what is missing, you can diagnose the problem yourself using Dependency Walker.
Another issue might be (beside all this "put the DLL in the correct location") that if the DLL was created with Visual Studio, eg. Visual Studio 2012, also the VCRedistributable for 64 bit must be installed (vcredist_x64.exe), which is profided by Visual Studio.

Categories

Resources