0

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

Tarek Saied
  • 6,482
  • 20
  • 67
  • 111
  • How do you know you can't load the DLL file? – Bob. Dec 18 '12 at 19:15
  • 1
    You sample is not enough - please instead of `dllname` put static string constant so it is clear that there is no problem with value of the parameter itself, also add excpetion information. – Alexei Levenkov Dec 18 '12 at 19:17

1 Answers1

2

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:

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144