C ++ / CLI:使用LoadLibrary + GetProcAddress与exe [英] C++/CLI: Use LoadLibrary + GetProcAddress with an 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屋!