locked
LoadLibrary Fails with error 193 on 64 bit Windows 7 RRS feed

  • Question

  • I had a program developed in C# VS2010 and VS2005. The program still works just fine on all Window OSs from Win 2000->XP->Windows 7. All 32 bit versions. The problem came when this program runs on 64 bit Windows7. The program fails during the call to LoadLibrary(). Used P/Invoke method to use this function. The LoadLibrary fails with any dll and not only the one I want. My project settings are "built on x86" and the Target Platform as "Any CPU". Infact I tried a small program(thinking that My original program is buggy), a windows forms application with two buttons. One Button will use LoadLibrary() and one for FreeLibrary(). Same issue on 64 bit. The LoadLibrary fails with error code 193. used same P/Invoke method for making  use of the native kernel32.dll in the System32 folder. Can anyone point me if I need to do anything additonal for the program to work. I am using the C# VS2010 for development and still has to use .Net 2.0. Any suggestion is greatly appreciated.

    Thanks.

    Here is my code:

    using

    using

    using

    using

    using

    using

    using

    using

    using 

    using

    using

    namespace

    {

     

    {

    public Form1() public class UnManagedCodeB

    {

    InitializeComponent();

    }

     

    {

    #region

    Kernel32 DLL Import Functions

     

    arSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)] 

     

    public static extern IntPtr LoadLibrary(string lpFilename);

    [

     

    [

     

    [

     

    [

     

    [

     

    #endregion

    }

     

     

    {

     

     

    m_DllHandle =

     

    {

     

    {error_code = 

     M

     

    }

    }

     

    {

    error_code = U

     

     

    }

    }

     

    {

     

    }

    nManagedCodeB.GetLastError(); MessageBox.Show(error_code.ToString(), "Dll Handle Return code"); Application.Exit(); private void button3_Click(object sender, EventArgs e) UnManagedCodeB.FreeLibrary(m_DllHandle);

     }

    catch (Exception x) MessageBox.Show(x.Message, "DLL Loading problem(LoadUSBLibrary())!");
    if (m_DllHandle == IntPtr.Zero) UnManagedCodeB.GetLastError(); essageBox.Show(error_code.ToString(), "Dll load failed(LoadLibrary()"); throw new ApplicationException("Error loading " + filename.ToString());
    private IntPtr m_DllHandle; private void button1_Click(object sender, EventArgs e) int error_code ; string filename = "C:\\Windows\\SysWOW64\\UAUSBIP.dll"; UnManagedCodeB.LoadLibrary( filename); try
    public static extern int GetLastError();
    DllImport("C:\\Windows\\System32\\kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)] public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); DllImport("C:\\Windows\\System32\\kernel32.dll")] public static extern bool FreeLibrary(IntPtr hModule); DllImport("C:\\Windows\\System32\\kernel32.dll")] public static extern bool SetDllDirectory(string path); DllImport("C:\\Windows\\System32\\kernel32.dll", SetLastError = true)] public static extern void SetLastError(int errorCode); DllImport("C:\\Windows\\System32\\kernel32.dll")]
    [
    DllImport("C:\\Windows\\System32\\kernel32.dll", Ch
    CS_64bit_TestApp public partial class Form1 : Form
    System.Windows.Forms;
    System.Runtime.InteropServices;
    System.Threading;
    System.IO.Ports;
    System.IO;
    System.Text;
    System.Drawing;
    System.Data;
    System.ComponentModel;
    System.Collections.Generic;
    System;
    Monday, August 23, 2010 6:54 PM

Answers

  • Any CPU will give you a 64 bit process on win64, you cannot load a 32bits dll into a 64 bits process. If you have no 64 bits versions of your dlls you can change your soltion platform from 'any cpu' to 'x86' to force a 32 bits process which has no issues loading 32 bits dlls.
    • Marked as answer by kbhanu Tuesday, August 24, 2010 2:06 PM
    Monday, August 23, 2010 8:54 PM

All replies

  • Any CPU will give you a 64 bit process on win64, you cannot load a 32bits dll into a 64 bits process. If you have no 64 bits versions of your dlls you can change your soltion platform from 'any cpu' to 'x86' to force a 32 bits process which has no issues loading 32 bits dlls.
    • Marked as answer by kbhanu Tuesday, August 24, 2010 2:06 PM
    Monday, August 23, 2010 8:54 PM
  • Thanks Ray. The suggestion helped.
    Tuesday, August 24, 2010 2:07 PM