Click to See Complete Forum and Search --> : LoadLibrary and NET dlls


DeepButi
April 21st, 2004, 03:30 AM
A non-NET dll can be used with LoadLibrary and GetProcAddress:

typedef int (*MYPROC)(int);
int _tmain(void)
{
HMODULE hmod;
MYPROC ProcAdd;
int i;
CString txt;
hmod=LoadLibrary("MyC++DLL");
if (hmod != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hmod, "MyFunc");
if (NULL != ProcAdd)
{
i=(ProcAdd) (1);
txt.Format("VC++ MyFunc returns %d",i);
Console::WriteLine(txt);
}
FreeLibrary(hmod);
}
return 0;
}


A NET dll can be used as a class

#using "MyNETDLL.dll"
using namespace MyNETDLL;
int _tmain(void)
{
int i;
CString txt;
NetClass *NetObject;
NetObject = new NetClass;
i=NetObject->MyFunc(1);
txt.Format("NET MyFunc returns %d",i);
Console::WriteLine(txt);
return 0;
}


Using a NET dll as shown requires knowledge of the dll name and function at compile time, whereas LoadLibrary/GetProcAdress allows you to decide dll and func name at run time.

So, ... is there any way to build a NET dll that allows LoadLibrary/GetProcAddress to be used?

Following some suggestions I gave it a strong name, registered it for COM interop and registered it in the Global Assembly Cache, but nothing works.

Since NET dlls rely on CRT and MSIL, don't have external entry points so GetProcAdress can not find the adress at run time, but to guarantee compatibility with "old" programming some method should exist ... or perhaps it doesn't :rolleyes:

Any ideas?

Thanks

RobAnd
April 28th, 2004, 02:37 AM
If you are interested in dynamically loading .NET Assemblies (developed in any language of your choice) and creating classes/invoking methods then I strongly encourage you to read the .NET Framework documentation regarding Reflection. The .NET Framework provides good support for loading and executing managed code dynamically.

You will specifically be interested in the following classes:
System.Reflection.Assembly (see the Load and LoadFile methods)
System.Reflection.MethodInfo (see the Invoke method)
System.Type (nice for invoking static methods).

As a note, LoadLibrary/GetProcAddress are for unmanaged code only. Unless you export unmanaged "flat" functions, you can't use those APIs.

Hope that helps!

- Robert

DeepButi
April 28th, 2004, 11:04 AM
RobAnd,
just what I was looking for.

LoadFrom works fine (although it expects a complete path and not a relative one as stated in the doc).

I'm trying to invoke the methods ... :rolleyes: ... still not working but in the right direction ;).

Thanks a lot