C ++ / CLI:使用LoadLibrary + GetProcAddress与exe [英] C++/CLI: Use LoadLibrary + GetProcAddress with an exe

查看:867
本文介绍了C ++ / CLI:使用LoadLibrary + GetProcAddress与exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有一些插件机制,其中我加载dll使用LoadLibrary和GetProcAddress创建一个具体的对象,并返回一个公共接口。这工作正常,直到我决定其中一个dll应该是一个exe。



LoadLibrary的文档说,它可以用于exe的,所以我给了它一枪。 exe得到加载没有错误,GetProcAddress。但是当我试图调用我的具体对象的构造函数,我得到一个访问冲突。



我想这将发生,因为加载一个exe不加载其使用的所有dll。所以我试图加载他们使用LoadLibrary,但我有同样的错误。













$ b p> 接口* MCFactory :: LoadInstanceFromAssembly(String ^ concreteAssemblyName,String ^ param){
string fullPathToAssembly =;
fullPathToAssembly + = FileSystem :: GetPathToProgramDirectory();
fullPathToAssembly + =\\+ marshal_as< string>(concreteAssemblyName);

MODULE hDLL = AssemblyLoader :: GetInstance()。LoadAssembly(fullPathToAssembly);

接口* pObject = NULL;
if(hDLL!= NULL){
t_pCreateInstanceFunction pCreateInstanceFunction =
(t_pCreateInstanceFunction):: GetProcAddress(hDLL,CREATE_INSTANCE_FUNCTION_NAME.c_str

if(pCreateInstanceFunction!= NULL){
//是,这个程序集公开了我们需要的函数
//调用函数创建对象
pObject = (* pCreateInstanceFunction)(marshal_as< string>(param));
}
}
return pObject;
}

(AssemblyLoader :: GetInstance()。LoadAssembly只是一个包装器:: LoadLibrary)

解决方案

可以。



http://www.codeproject.com/Articles/1045674/Load-EXE -as-DLL-Mission-Possible



这个想法是修补IAT,然后调用CRT。当然,EXE必须可重定位,默认情况下(ASLR)。


Up until now, I had some sort of plugin mechanism in which I loaded dlls using LoadLibrary and GetProcAddress to create a concrete object and return a common interface. This worked fine until I decided that one of the dlls should be an exe.

LoadLibrary's documentation says that it can be used for exe's as well, so I gave it a shot. The exe gets loaded without errors, as GetProcAddress. But when I try to call my concrete object's constructor, I get an access violation.

I thought this would happen because loading an exe does not load all the dlls it uses. So I tried loading them using LoadLibrary, but I got the same error. Any advice on this?

Here's my code (mixed C++/CLI):

Interface* MCFactory::LoadInstanceFromAssembly( String ^ concreteAssemblyName, String ^ param ){
    string fullPathToAssembly = "";
    fullPathToAssembly += FileSystem::GetPathToProgramDirectory();
    fullPathToAssembly += "\\" + marshal_as<string>(concreteAssemblyName);

    MODULE hDLL = AssemblyLoader::GetInstance().LoadAssembly( fullPathToAssembly ); 

    Interface* pObject = NULL;
    if (hDLL != NULL){
        t_pCreateInstanceFunction pCreateInstanceFunction =
            (t_pCreateInstanceFunction) ::GetProcAddress (hDLL, CREATE_INSTANCE_FUNCTION_NAME.c_str());

        if ( pCreateInstanceFunction != NULL ){
            //Yes, this assembly exposes the function we need
            //Invoke the function to create the object
            pObject = (*pCreateInstanceFunction)( marshal_as<string>(param) );              
        }
    }           
    return pObject;
}

(AssemblyLoader::GetInstance().LoadAssembly is just a wrapper for ::LoadLibrary)

解决方案

It is possible.

http://www.codeproject.com/Articles/1045674/Load-EXE-as-DLL-Mission-Possible

The idea is to patch the IAT, then call the CRT. Of course, the EXE must be relocatable, and by default (ASLR) it is.

这篇关于C ++ / CLI:使用LoadLibrary + GetProcAddress与exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆