简体   繁体   English

如果在调用表单非托管代码(C ++)时托管c#类库dll不在.exe文件夹中,则获取FileNotFound异常

[英]Gets FileNotFound Exception if managed c# class library dll is not in .exe folder when called form unmanaged code (c++)

I have to use the C# class library code in my native code which is written in c++. 我必须在用C ++编写的本机代码中使用C#类库代码。 For this I added one interface written in c++ add the my C# class library project as dependency to it. 为此,我添加了一个用c ++编写的接口,并添加了我的C#类库项目作为对其的依赖。 This works fine till I put the C# dll in .exe folder , but I put this in libs folders (where we keep our dll's), I get file not found exception when tried to call the C# dll function. 直到我将C#dll放在.exe文件夹中,此方法才能正常工作,但是我将其放在libs文件夹(我们保留dll的文件夹)中,尝试调用C#dll函数时出现文件未找到异常。 Is there any way to keep c# dll in libs folder and make it work? 有什么办法可以将c#dll保留在libs文件夹中并使它工作?

Thanks. 谢谢。

Yes, you need to re-add it from the libs folder and it will be fine. 是的,您需要从libs文件夹中重新添加它,这样就可以了。 Or if you are naming the DLL in your code itself, then just provide the full path along with the DLL name. 或者,如果您要在代码本身中命名DLL,则只需提供完整路径以及DLL名称。 Then when you recompile, the .exe will contain the path you gave it in the source code and will look for the DLL there. 然后,当您重新编译时,.exe将包含您在源代码中提供的路径,并在其中查找DLL。

From the MSDN(LoadLibrary) , Remarks section:: MSDN(LoadLibrary)的 “备注”部分::

The first directory searched is the directory containing the image file used to create the calling process (for more information, see the CreateProcess function). 搜索的第一个目录是包含用于创建调用进程的图像文件的目录(有关更多信息,请参见CreateProcess函数)。 Doing this allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable. 这样做可以查找与进程关联的私有动态链接库(DLL)文件,而无需将进程的安装目录添加到PATH环境变量中。 If a relative path is specified, the entire relative path is appended to every token in the DLL search path list. 如果指定了相对路径,则整个相对路径将附加到DLL搜索路径列表中的每个标记。 To load a module from a relative path without searching any other path, use GetFullPathName to get a nonrelative path and call LoadLibrary with the nonrelative path. 要从相对路径加载模块而不搜索其他路径,请使用GetFullPathName获取非相对路径,并使用非相对路径调用LoadLibrary。 For more information on the DLL search order, see Dynamic-Link Library Search Order. 有关DLL搜索顺序的更多信息,请参见动态链接库搜索顺序。

The search path can be altered using the SetDllDirectory function. 可以使用SetDllDirectory函数更改搜索路径。 This solution is recommended instead of using SetCurrentDirectory or hard-coding the full path to the DLL. 建议使用此解决方案,而不要使用SetCurrentDirectory或对DLL的完整路径进行硬编码。

If a path is specified and there is a redirection file for the application, the function searches for the module in the application's directory. 如果指定了路径,并且有应用程序的重定向文件,则该函数在应用程序的目录中搜索模块。 If the module exists in the application's directory, LoadLibrary ignores the specified path and loads the module from the application's directory. 如果模块位于应用程序的目录中,则LoadLibrary会忽略指定的路径,并从应用程序的目录中加载模块。 If the module does not exist in the application's directory, LoadLibrary loads the module from the specified directory. 如果模块不在应用程序的目录中,则LoadLibrary从指定的目录加载模块。 For more information, see Dynamic Link Library Redirection. 有关更多信息,请参见动态链接库重定向。

If you call LoadLibrary with the name of an assembly without a path specification and the assembly is listed in the system compatible manifest, the call is automatically redirected to the side-by-side assembly. 如果使用不带路径说明的程序集名称调用LoadLibrary,并且该程序集在系统兼容清单中列出,则该调用将自动重定向到并行程序集。

This is all you need to know about path search of a dll. 这就是关于dll的路径搜索所需的全部信息。 The easy solution, enter the full path in LoadLibrary call or set your PATH environment variable to contain the alternate path of the dll. 简单的解决方案是,在LoadLibrary调用中输入完整路径,或将PATH环境变量设置为包含dll的备用路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从C#调用非托管C ++库(dll):如何在托管代码和非托管代码之间正确传递数据 - Calling unmanaged C++ library (dll) from C# : How to properly pass data between managed and unmanaged code 在非托管c ++ dll中使用托管c#dll - Using a managed c# dll inside an unmanaged c++ dll 我可以在非托管Delphi EXE中使用托管C#DLL吗? - Can I use managed C# DLL in unmanaged Delphi EXE? 从C#windows服务调用C ++ DLL(非托管代码)(用托管代码编写) - Calling a C++ dll (unmanaged code) from a C# windows service (written in managed code) 从 C#.exe 调用 function 与非托管 C++.Z06416233FE5EC4C59133122E4ABZ - Call a function from C# .exe with unmanaged C++ .dll 将非托管 dll 嵌入托管 C# dll - Embedding unmanaged dll into a managed C# dll 使用pinvoke从C#代码调用时,非托管c ++ dll何时从内存中卸载 - when does unmanaged c++ dll unload from memory when called from C# code using pinvoke 从c#调用的非托管c ++ dll在dll中使用CString时崩溃 - unmanaged c++ dll called from c#, crashes when CString used in dll 将非托管EXE作为资源合并到托管C#代码中 - Incorporate an unmanaged EXE as resource in a managed C# code 调试从非托管C ++调用的托管.NET代码 - Debugging Managed .NET Code Called From Unmanaged C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM