首页 > 编程语言 >CLR/C++程序找不到DLL最有效方法?

CLR/C++程序找不到DLL最有效方法?

时间:2024-09-01 17:26:28浏览次数:6  
标签:std const name subdirs C++ dll path DLL CLR

 使用C++17及以上,支持 filesystem 处理 不需要程序集强签名 , 也不用注册全局程序集缓存,非常有效。

#include <iostream>
#include <filesystem>
using namespace std::filesystem;
namespace fs = std::filesystem;
//-----------------------------------------------------------------
 /// <summary>
 /// 获取子目录
 /// </summary>
 /// <param name="dir"></param>
 /// <param name="subdirs"></param>
 void GetPaths( const fs::path& dir , std::vector<fs::path>& subdirs ) {
     subdirs.push_back(dir);
     for (const auto& entry : fs::directory_iterator(dir)) {
         const auto& path = entry.path();
         if (fs::is_directory(path)) {
             //subdirs.push_back(path);
             GetPaths( path , subdirs ); // 递归获取子目录中的子目录
         }
     }
 }
 //-----------------------------------------------------------------
 int _tmain( int argc, _TCHAR* argv[] )
{
    AppDomain^ currentDomain = AppDomain::CurrentDomain;
    currentDomain->AssemblyResolve += gcnew ResolveEventHandler(MyResolveEventHandler);
}
//-----------------------------------------------------------------
static Assembly^ MyResolveEventHandler(Object^ sender, ResolveEventArgs^ args)
{
    String^ dll_name;
    AssemblyName^ myAssemblyName = gcnew AssemblyName(args->Name);
    Console::WriteLine("Resolving..." + myAssemblyName->Name);
    dll_name = myAssemblyName->Name;
    //-个dll文件会产生2次事件,比如abc.dll第一次是abc,需要自己添加dll扩展名,第二次会需要abc.resources,此时返回空即可
    if (dll_name->Contains(".resources")) return nullptr;
    dll_name += ".dll";
    string dll_name_str = marshal_as<std::string>(dll_name->ToString());   
    std::vector<fs::path> subdirs;
    GetPaths("J:\\Library\\C#", subdirs);
    GetPaths("J:\\Driver\\C#" , subdirs);
    std::string filePath;
    for ( const auto& subdir : subdirs ) {
        std::cout << subdir << std::endl;
        filePath = subdir.string();
        filePath += "\\";
        filePath += dll_name_str;if (fs::exists(filePath)) {
            std::cout << "文件存在:" << filePath  << std::endl;
            return Assembly::LoadFrom( marshal_as<String^>( filePath ) );
        }
    }
    return nullptr;
}

 

标签:std,const,name,subdirs,C++,dll,path,DLL,CLR
From: https://www.cnblogs.com/jk-autotech/p/18391491

相关文章

  • Steamui.dll守护指南:Steam客户端遭遇迷失时的自助恢复之旅
    Steamui.dll丢失应该如何处理?很多朋友还不是特别的清楚,解决Steamui.dll丢失的问题,可以按照以下步骤操作:1.重新安装Steam客户端:最直接的方法是卸载当前的Steam客户端,然后从官方网站重新下载安装包进行安装。这会自动替换所有丢失或损坏的文件,包括Steamui.dll。2.文件恢复:若......
  • OpenGL32.dll错误:无法找到入口点的速效修复秘籍及故障全面排查策略
    遇到OpenGL32.dll错误,通常意味着系统中的OpenGL库文件可能损坏或不兼容,这里提供一些解决方案来帮助您修复这个问题:1.更新图形驱动程序:•访问您的显卡制造商官网(例如NVIDIA、AMD或Intel),下载并安装最新的图形驱动程序。正确的驱动程序能够确保OpenGL组件正常工作。2.系统文件......
  • wxmsw311u_qa_vc_custom.dll加载故障:快速修复应用程序核心问题的实战手册
    wxmsw311u_qa_vc_custom.dll加载故障应该如何解决?要想解决wxmsw311u_qa_vc_custom.dll加载故障的问题,可以按照以下步骤尝试修复:1.重新安装相关软件:此dll文件属于某些Windows应用的组件,首先尝试卸载并重新安装可能导致问题的软件。这通常能自动替换损坏或丢失的dll文件。2.......
  • Modern C++——不准确“类型声明”引发的非必要性能损耗
    大纲案例代码地址C++是一种强类型语言。我们在编码时就需要明确指出每个变量的类型,进而让编译器可以正确的编译。看似C++编译器比其他弱类型语言的编译器要死板,实则它也做了很多“隐藏”的操作。它会在尝试针对一些非预期类型进行相应转换,以符合预期,比如《C++拾趣——......
  • c++ I/O
    1.flush刷新缓存,endl刷新缓存并换行cout<<"Hello"<<fulsh;cout<<"Wait<<endl;2.hex,oct,dec输出16进制,8进制,10进制cout<<hexcout<<octcout<<dec3.使用width调节宽度cout.width(12);//width函数只影响下一个要显示的item4.使用fill填充字符。C++默认......
  • C++ 标准输入输出 -- <iostream>
    <iostream>库是C++标准库中用于输入输出操作的头文件。<iostream>定义了几个常用的流类和操作符,允许程序与标准输入输出设备(如键盘和屏幕)进行交互。以下是<iostream>库的详细使用说明,包括其主要类和常见用法示例。主要类std::istream:用于输入操作的抽象基类。std::ostre......
  • DLL 动态注入---ImgWalk动态库,这个DLL用来检测被注入的进程中当前载入的各个模块名称-
    DLL动态注入—ImgWalk动态库,这个DLL用来检测被注入的进程中当前载入的各个模块名称—exe程序DLL动态注入—ImgWalk动态库,这个DLL用来检测被注入的进程中当前载入的各个模块名称—exe程序文章目录DLL动态注入---ImgWalk动态库,这个DLL用来检测被注入的进程中当前载入......
  • C++奇迹之旅:深度解析list的模拟实现
    文章目录......
  • C++:std::thread 和 pthread
            在C++中,线程的实现主要有两种方式:使用C++11标准库中的std::thread和POSIX线程库(pthread)。这两种方式各有优缺点,适用于不同的场景。以下是对这两种方式的详细比较和示例代码。std::thread示例代码#include<iostream>#include<thread>#include<chrono>......
  • C++:std::this_thread::sleep_for 和 sleep
            在C++中,std::this_thread::sleep_for和sleep函数都可以用来使当前线程暂停执行一段时间,但它们有一些重要的区别。以下是对这两种方法的详细比较:std::this_thread::sleep_for定义:std::this_thread::sleep_for是C++11标准库中的一个函数,用于使当前线程暂停执......