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)