Tarek Saied
Tarek Saied

Reputation: 6616

Cannot load dll file in my project

In my project i cannot load dll file

dll file --> platform target is x86

and my project --> platform target is Anycpu

machine is running a 64 bit OS

and this is the code i use

 pDll = NativeMethods.LoadLibrary(dllname);
// some code here
static class NativeMethods
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);
}

why i cannot load this dll file ?

sorry for my bad English

Upvotes: 0

Views: 2915

Answers (1)

Yahia
Yahia

Reputation: 70369

Assuming that everything else is ok (DLL name etc.):

What you describe is a known problem... for example if your DLL is 32 bit and the OS you are using is 64 Bit then your .NET application will be running in 64 Bit mode...

The easiest solution: IF the DLL is only available in x86 you need to compile for x86 and NOT AnyCPU.

Otherwise you will need a 64 Bit version of that DLL and then you run into a problem known as "side-by-side-assembly"... I think you will find these helpful:

Upvotes: 2

Related Questions