views:

438

answers:

3

HI I have a application developed in VC++6.0 ,now I want use some new features from .NET and developed a library, to develop this library I have to use the CLR-VC++ now I packaged this in a DLL.Now I need to call the routine of this DLL in my MFC application.

I tried to write a small MFC application to load this DLL, All the time the LoadLibrary() call is failing @err =126, module not found.I check the the dll with dependency walker everthig is fine there. Please Help me in this regard. If possible provide me a sample code or link. Thanks in advance

-Sachin

+1  A: 

Use ClrCreateManagedInstance to create a COM-Callable-Wrapper for the object you want to call. Then use it like any other COM type.

1800 INFORMATION
A: 

I have a native C++ application which uses a managed C++ assembly and loads it with LoadLibrary() without problems. I had to do two things, however, before LoadLibrary() worked:

  • Make sure that the current directory is the one where the managed assembly resides (use chdir() to change directory)
  • In the managed assembly, the first function invoked by native code only defines the handler for AppDomain::CurrentDomain->AssemblyResolve event which explicitly loads assemblies from the folder of the managed application. It then invokes another managed function to do the rest of the initialization.

The reason for the last point is that CLR attempts to load an assembly dependency only if a function uses it. So I had to ensure that types in non-system assemblies are not referenced before the AssemblyResolve handler has been defined.

ref class AssemblyResolver
{
public:
    /// The path where the assemblies are searched
    property String^ Path
    {
        String^ get()
        { return path_; }
    }

    explicit AssemblyResolver(String^ path)
        : path_(path)
    { /* Void */ }

    Assembly^ ResolveHandler(Object^ sender, ResolveEventArgs^ args)    
    {
        // The name passed here contains other information as well
        String^ dll_name = args->Name->Substring(0, args->Name->IndexOf(','));
        String^ path = System::IO::Path::Combine(path_, dll_name+".dll");

        if ( File::Exists(path) )
            return Assembly::LoadFile(path);

        return nullptr;
    }

private:
    String^ path_;
};

extern "C" __declspec(dllexport) void Initialize()
{
    String^ path = "The path where the managed code resides";

    AssemblyResolver^ resolver = gcnew AssemblyResolver(path);
    AppDomain::CurrentDomain->AssemblyResolve += gcnew ResolveEventHandler(
        resolver, 
        &AssemblyResolver::ResolveHandler
    );

    FunctionWhichUsesOtherManagedTypes();
}
Bojan Resnik
A: 

you have to go to property page -> Common properties ->Add New reference and include you CLR Address there .

sadegh