[英]From a C#/.NET assembly, how do I load a C++/CLR DLL which loads an unmanaged DLL?
I have a Visual Studio 2017 solution with the following projects: 我有一个包含以下项目的Visual Studio 2017解决方案:
CppClrQuestion -- Solution
CUnamagedLib -- C++ Unmanaged Windows DLL
CUnmanagedLib.cpp
CUnmanagedLib.h
ClrManagedLib -- C++/CLR DLL Project
ClrManagedLib.cpp
ClrManagedLib.h
CSharpDotNetConsoleApp -- C#/.NET
Program.cs
CSharpDotNetConsoleApp
has a project reference to ClrManagedLib
and ClrManagedLib
has a project reference to CUnamagedLib
. CSharpDotNetConsoleApp
的项目引用为ClrManagedLib
而ClrManagedLib
的项目引用为CUnamagedLib
。
Everything compiles OK. 一切都可以编译。 However, when I run the C# application I get this exception:
但是,当我运行C#应用程序时,出现以下异常:
System.IO.FileNotFoundException: 'Could not load file or assembly 'CppClrLib2.dll' or one of its dependencies. The specified module could not be found.'
If I change ClrManagedLib
so that it doesn't call anything in CUnamagedLib
, it runs OK. 如果我更改
ClrManagedLib
以便它在CUnamagedLib
中不调用任何CUnamagedLib
,它将运行正常。
How do I set up a solution where I have a C# app which references a managed C++/CLR lib which then references an unmanaged C++ lib? 如何在我的C#应用程序引用托管C ++ / CLR库,然后引用非托管C ++库的情况下设置解决方案?
For reference, here are the source file contents: 供参考,以下是源文件内容:
#include "stdafx.h"
__declspec(dllexport) int GetNumUnmanaged(void) {
return 5;
}
#pragma once
__declspec(dllimport) int GetNumUnmanaged(void);
#include "stdafx.h"
#include "..\CUnamangedLib\CUnmanagedLib.h"
#include "ClrManagedLib.h"
int ClrManagedLib::Class1::GetNum() {
return GetNumUnmanaged();
}
#pragma once
using namespace System;
namespace ClrManagedLib {
public ref class Class1
{
public:
int GetNum();
};
}
namespace CSharpDotNetConsoleApp {
class Program {
static void Main(string[] args) {
var managedObj = new ClrManagedLib.Class1();
}
}
}
Windows will identify the required unmanaged DLL, but it only looks in a few places for it. Windows将识别所需的非托管DLL,但仅在几个地方查找它。 Make sure you have a copy there.
确保在那里有副本。 In the old days, everyone piled everything in the Windows\\System32 subfolder.
在过去,每个人都将所有内容堆放在Windows \\ System32子文件夹中。 Don't do that.
不要那样做 Details: https://msdn.microsoft.com/en-ca/library/7d83bc18.aspx More details: https://docs.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-search-order
详细信息: https : //msdn.microsoft.com/en-ca/library/7d83bc18.aspx更多详细信息: https : //docs.microsoft.com/zh-cn/windows/desktop/dlls/dynamic-link-library-搜索顺序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.