none
C# Windows application to conditionally run a native DLL RRS feed

  • Question

  • Hi Team,

    I want my C# Windows application to conditionally run a native method, conditionally choosing to run either the x86 or the x64 version of the dll(Native dll in c++). Whenever I try to load the 64 bit dll I get the below error:

    BadImageFormatException "An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

    But it works fine in case of x86(i.e 32 Bit dll's) in case of selecting any cpu. My Machine is x64(i.e 64 bit).

    Code is as follows:

    var x86 = "vddk5_x86";
    var x64 = "vddk6_x64";
    var vddkPath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, x86);
    //var vddkPath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, x64);
    Environment.SetEnvironmentVariable("PATH", vddkPath);
                
    VixError vixError = VixDiskLib.VixDiskLib_InitEx(VixDiskLib.VIXDISKLIB_VERSION_MAJOR, VixDiskLib.VIXDISKLIB_VERSION_MINOR,
                                          null,
                                         null,//(a, b) => _stringBuilder.AppendLine(string.Format("Warning VixDiskLib_InitEx:  {0}", a)),
                                         null,// (a, b) => _stringBuilder.AppendLine(string.Format("Panic VixDiskLib_InitEx:  {0}", a)),
                                         null, null);

    When i specify the target either x64 or x86 it is giving Exception "Unable to load DLL 'vixDiskLib.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)".

    Friday, October 17, 2014 12:20 PM

Answers

  • The bitness of all DLLs loaded into a process must match the bitness of the process itself. By default, Visual Studio will mark assemblies that are targeting the AnyCPU architecture as being "32 bit preferred" - which means that by default your executable will be running in a 32 bit process and you will only be able to load 32 bit DLLs into that process.

    If you'd like to load a 64 bit DLL into your process, you can instead use the x64 architecture specifically, or uncheck the "prefer 32 bit" check box in the build settings which will cause the process to run as 64 bit on 64 bit operating systems by default.

    Friday, October 17, 2014 6:04 PM

All replies

  • The bitness of all DLLs loaded into a process must match the bitness of the process itself. By default, Visual Studio will mark assemblies that are targeting the AnyCPU architecture as being "32 bit preferred" - which means that by default your executable will be running in a 32 bit process and you will only be able to load 32 bit DLLs into that process.

    If you'd like to load a 64 bit DLL into your process, you can instead use the x64 architecture specifically, or uncheck the "prefer 32 bit" check box in the build settings which will cause the process to run as 64 bit on 64 bit operating systems by default.

    Friday, October 17, 2014 6:04 PM
  • Hi,

    Above issue is solved thanks for help,now 64 bit DLL's also loads correctly but now I have come across one more issue in following code

    var ulStatus = VixDiskLib.VixDiskLib_Connect(GetConnectParams(), out vixConnHandle);

    where GetConnectParams() is as follows

    private static VixDiskLibConnectParams GetConnectParams()
            {
                var connectParams = new VixDiskLibConnectParams { serverName = "****" };
                connectParams.creds.uid.userName = "****";
                connectParams.creds.uid.password = "****";
                connectParams.credType = VixDiskLibCredType.VIXDISKLIB_CRED_UID;
                connectParams.vmxSpec = "moref=vm-324275";
                connectParams.port = 443;

                return connectParams;
            } 

    Call to VixDiskLib_Connect() method gives following error:

     Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    Monday, October 20, 2014 11:28 AM