BadImageFormatException. This will occur when running in 64 bit mode with the 32 bit Oracle client components installed

258,800

Solution 1

One solution is to install both x86 (32-bit) and x64 Oracle Clients on your machine, then it does not matter on which architecture your application is running.

Here an instruction to install x86 and x64 Oracle client on one machine:

Assumptions: Oracle Home is called OraClient11g_home1, Client Version is 11gR2

  • Optionally remove any installed Oracle client (see How to uninstall / completely remove Oracle 11g (client)? if you face problems)

  • Download and install Oracle x86 Client, for example into C:\Oracle\11.2\Client_x86

  • Download and install Oracle x64 Client into different folder, for example to C:\Oracle\11.2\Client_x64

  • Open command line tool, go to folder %WINDIR%\System32 (typically C:\Windows\System32) and create a symbolic link ora112 to folder C:\Oracle\11.2\Client_x64 (see commands section below)

  • Change to folder %WINDIR%\SysWOW64 (typically C:\Windows\SysWOW64) and create a symbolic link ora112 to folder C:\Oracle\11.2\Client_x86, (see below)

  • Modify the PATH environment variable, replace all entries like C:\Oracle\11.2\Client_x86 and C:\Oracle\11.2\Client_x64 by C:\Windows\System32\ora112, respective their \bin subfolder. Note: C:\Windows\SysWOW64\ora112 must not be in PATH environment.

  • If needed set your ORACLE_HOME environment variable to C:\Windows\System32\ora112

  • Open your Registry Editor. Set Registry value HKLM\Software\ORACLE\KEY_OraClient11g_home1\ORACLE_HOME to C:\Windows\System32\ora112

  • Set Registry value HKLM\Software\Wow6432Node\ORACLE\KEY_OraClient11g_home1\ORACLE_HOME to C:\Windows\System32\ora112 (not C:\Windows\SysWOW64\ora112)

  • You are done! Now you can use x86 and x64 Oracle client seamless together, i.e. an x86 application will load the x86 libraries, an x64 application loads the x64 libraries without any further modification on your system.

  • Probably it is a wise option to set your TNS_ADMIN environment variable (resp. TNS_ADMIN entries in Registry) to a common location, for example TNS_ADMIN=C:\Oracle\Common\network.

Commands to create symbolic links:

cd C:\Windows\System32
mklink /d ora112 C:\Oracle\11.2\Client_x64
cd C:\Windows\SysWOW64
mklink /d ora112 C:\Oracle\11.2\Client_x86

Notes:

Both symbolic links must have the same name, e.g. ora112.

Despite of their names folder C:\Windows\System32 contains the x64 libraries, whereas C:\Windows\SysWOW64 contains the x86 (32-bit) libraries. Don't get confused.

Solution 2

I had the same issue on a Windows 10 PC. I copied the project from my old computer to the new one, both 64 bits, and I installed the Oracle Client 64 bit on the new machine. I got the same error message, but after trying many solutions to no effect, what actually worked for me was this: In your Visual Studio (mine is 2017) go to Tools > Options > Projects and Solutions > Web Projects

On that page, check the option that says: Use the 64 bit version of IIS Express for Websites and Projects

Solution 3

In my situation, the Oracle 11.2 32-bit client was installed on my 64-bit Windows 2008 R2 OS.

My solution: In the Advanced Settings for the Application Pool assigned to my ASP.NET application, I set Enable 32-Bit Applications to True.

Please see below for the standalone .ashx test script that I used to test the ability to connect to Oracle. Before making the Application Pool change, its response was:

[Running as 64-bit] Connection failed.

...and after the Application Pool change:

[Running as 32-bit] Connection succeeded.

TestOracle.ashx – Script to Test an Oracle Connection via System.Data.OracleClient:

To use: Change the user, password and host variables as appropriate.

Note that this script can be used in a standalone fashion without disturbing your ASP.NET web application project file. Just drop it in your application folder.

<%@ WebHandler Language="C#" Class="Handler1" %>
<%@ Assembly Name="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %>

using System;
using System.Data.OracleClient;
using System.Web;

public class Handler1 : IHttpHandler
{
    private static readonly string m_User = "USER";
    private static readonly string m_Password = "PASSWORD";
    private static readonly string m_Host = "HOST";

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string result = TestOracleConnection();
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get { return false; }
    }

    private string TestOracleConnection()
    {
        string result = IntPtr.Size == 8 ?
            "[Running as 64-bit]" : "[Running as 32-bit]";

        try
        {
            string connString = String.Format(
              "Data Source={0};Password={1};User ID={2};",
              m_Host, m_User, m_Password);

            OracleConnection oradb = new OracleConnection();
            oradb.ConnectionString = connString;
            oradb.Open();
            oradb.Close();
            result += " Connection succeeded.";
        }
        catch
        {
            result += " Connection failed.";
        }

        return result;
    }
}

Solution 4

To revise IIS

  1. Select Application Pools.
  2. Clic in ASP .NET V4.0 Classic.
  3. Select Advanced Settings.
  4. In General, option Enable 32-Bit Applications, default is false. Select TRUE.
  5. Refresh and check site.

Comment:

Platform: Windows Server 2008 R2 Enterprise - 64Bit - IIS 7.5

Solution 5

I had the same problem in SSIS 2008. I tried to connect to an Oracle 11g using ODAC 12c 32 bit. Tried to install ODAC 12c 64 bit as well. SSIS was actually able to preview the table but when trying to run the package it was giving this error message. Nothing helped. Switched to VS 2013, now it was running in debug mode but got the same error when the running the package using dtexec /f filename. Then I found this page: http://sqlmag.com/comment/reply/17881.

To make it short it says: (if the page is still there just go to the page and follow the instrucrtions...) 1) Download and install the latest version of odac 64 bit xcopy from oracle site. 2) Download and install the latest version of odac 32 bit xcopy from oracle site. How? open a cmd shell AS AN ADMINSTARTOR and run: c:\64bitODACLocation> install.bat oledb c:\odac\odac64. the first parameter is the component you want to install. The second param is where to install to. install the 32 version as well like this: c:\32bitODACLocation> install.bat oledb c:\odac\odac32. 3) Change the path of the system to include c:\odac\odac32; c:\odac\odac32\bin; c:\odac\odac64;c:\odac\odac64\bin IN THIS ORDER. 4) Restart the machine. 5) make sure you have the same tnsnames.ora in both odac32\admin\network and odac64\admin\network folders (or at least the same entry for your connection). 6) Now open up SSIS in visual studio (I used the free 2013 version with the ssis package) - Use OLEDB and then select the Oracle Provider for OLE DB provider as your connection type. Set the name of the entry in your tnsnames.ora as the "server or file name". Username is your schema name (db name) and password is the password for schema. you are done!

Again, you can find the very detailed solution and much more in the original site.

This was the only thing which worked for me and did not mess up my environment.

Cheers! gcr

Share:
258,800

Related videos on Youtube

Maven
Author by

Maven

Updated on July 09, 2022

Comments

  • Maven
    Maven almost 2 years

    I am getting this error while on of my .Net application are trying to make a connection to oracle database.

    The error says that This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.. But I have made sure many times that the client installed in x64 bit not 32.

    Date Time: 6/8/2014 10:57:55 AM: System.InvalidOperationException: Attempt to load Oracle client libraries threw BadImageFormatException.  This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
           at System.Data.Common.UnsafeNativeMethods.OCILobCopy2(IntPtr svchp, IntPtr errhp, IntPtr dst_locp, IntPtr src_locp, UInt64 amount, UInt64 dst_offset, UInt64 src_offset)
           at System.Data.OracleClient.OCI.DetermineClientVersion()
           --- End of inner exception stack trace ---
           at System.Data.OracleClient.OCI.DetermineClientVersion()
           at System.Data.OracleClient.OracleInternalConnection.OpenOnLocalTransaction(String userName, String password, String serverName, Boolean integratedSecurity, Boolean unicode, Boolean omitOracleConnectionName)
           at System.Data.OracleClient.OracleInternalConnection..ctor(OracleConnectionString connectionOptions)
           at System.Data.OracleClient.OracleConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
           at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
           at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
           at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
           at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
           at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
           at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
           at System.Data.OracleClient.OracleConnection.Open()
           at CustomizedSetupInstaller.Runscripts.InitializeDBObjects(String connectionString, String dbProvider)
    
    • Luke Woodward
      Luke Woodward about 10 years
      You're using System.Data.OracleClient. This namespace has been deprecated and will be removed in a future version of .NET. Perhaps you are using a 32-bit version of that? It is also not part of the Oracle client, so whether you are using a 32-bit or 64-bit Oracle client is irrelevant. Ideally, you should be using Oracle.DataAccess (or Oracle.ManagedDataAccess) instead.
    • Wernfried Domscheit
      Wernfried Domscheit about 10 years
      Minor mistake: System.Data.OracleClient is just the Provider, it also uses an Oracle Client which must match the architecture. Only Oracle.ManagedDataAccess does not require an additional Oracle client installed. Maybe the Oracle Client is x64 but your application is 32-bit, this does not work either. It is possible to install both 32-bit and 64-bit Oracle client on the same machine
  • Bender
    Bender over 9 years
    When running the Oracle Client installer, it asks you for a path for the Oracle Base, and one for the Oracle Home. In your instructions here, would you just tack on "OraClient11g_home1" to each of the paths you specify? So Oracle Base would be "C:\Oracle\11.2\Client_x86" and Software Location (Oracle Home) would be "C:\Oracle\11.2\Client_x86\OraClient11g_home1"?
  • Wernfried Domscheit
    Wernfried Domscheit over 9 years
    OraClient11g_home1 is the "Oracle Home Name" which is used to identify correct key in your registry, see How Home Selector Works. In earlier Oracle versions you had an "Oracle Home Selector" application where you can switch between different Oracle Clients installed on one machine. It is not mandatory to have the Oracle Home Name equal to your folder name where Oracle is installed. Oracle Base "C:\Oracle\11.2\Client_x86" and Software Location "C:\Oracle\11.2\Client_x86\OraClient11g_home1" works fine.
  • Bender
    Bender over 9 years
    Ah, I see... the Instant Client only asks you for one path: the Software Location, i.e. the Oracle Home. So all of your paths above are the Oracle Home. If you choose any of the other three install types (Administrator, Runtime, Custom) it makes you choose a path for both Oracle Base and Oracle Home. If I choose "C:\Oracle\11.2\Client_x86\OraClient11g_home1" for my Oracle Home, then I need to use that path in place of your paths above...
  • Andrew Grothe
    Andrew Grothe over 9 years
    This is very helpful. Is the 64bit xCopy package good enough or is the full 64bit client needed? I can't seem to find a download for it besides the instaclient, but I don't have the 32bit instaclient installed, rather the full client.
  • b_levitt
    b_levitt over 9 years
    what is the point of the symbolic links or modifying the registry? When left alone, the provider will find the unmanaged libraries by the path IT sets in the registry.
  • Wernfried Domscheit
    Wernfried Domscheit over 9 years
    @agrothe, this guideline applies for any type of installation, no matter if "Instant Client", "Full", "Administrator", etc. It can be even mixed, e.g. 64-bit Full Client, 32-bit Instant Client only.
  • Wernfried Domscheit
    Wernfried Domscheit over 9 years
    @b_levitt, please read this to get an understanding: How Windows 64-bit Supports 32-bit Applications. The 64-bit provider searches for Oracle binaries in %WINDIR%\System32\ora112\bin which points to C:\Oracle\11.2\Client_x64\bin whereas a 32-bit provider searches in %WINDIR%\SysWOW64\ora112\bin which points to C:\Oracle\11.2\Client_x86\bin. You cannot put folders directly to PATH, because (depending on order) a 64-bit provider may search first C:\Oracle\11.2\Client_x86\bin and would fail.
  • b_levitt
    b_levitt over 9 years
    @Wernfried, I understand all that. What I think you are missing is that the system path is the LAST thing the oracle provider uses to find it's unmanaged dlls (see Search Order for Unmanaged DLLs in: docs.oracle.com/database/121/ODPNT/InstallODP.htm ). Side by side installation works fine without modifying the path, changing the registry, or creating symbolic links. Is your solution exclusive to the deprecated System.Data.OracleClient?
  • Wernfried Domscheit
    Wernfried Domscheit over 9 years
    @b_levitt, it applies to anything which uses Oracle Client binaries, include System.Data.OracleClient and ODBC drivers. The issue is this: Oracle.DataAccess.dll itself loads further Oracle dll's (e.g. oci.dll or oranls11.dll, etc) and those dll's are searched in folders listed in PATH Environment (plus current directory). So, depending whether you run 64 or 32 bit application, Oracle.DataAccess.dll has to search different folders. This is achieved by the symbolic links.
  • Wernfried Domscheit
    Wernfried Domscheit over 9 years
    @b_levitt, I made some tests on my machine. Using c:\oracle\product\11.2\Client_x64;c:\oracle\product\11.2\Cli‌​ent_x64\bin directly in PATH worked for ODP.NET(x86) but some OLEBD(x86) and ODBC(x86) drivers failed. Without the symbolic links all failed if architecture does not match, i.e. ODP.NET, OLEDB and ODBC - no matter whether I use the Microsoft drivers (which are deprecated apart from ODBC) or the Oracle drivers.
  • b_levitt
    b_levitt over 9 years
    I should clarify my last comment. In the context of a .net app connecting with the oracle provider with somebody that's obviously new to connecting to oracle, I guess I'm wondering why you would go thru all this as opposed to just setting dllpath in the .config file? In the end I'll just post my own answer, but since you have the accepted answer I wanted to show the discussion.
  • Bender
    Bender almost 9 years
    An update here, since VS2015 and Oracle 12cR3 are out... if you want to install both the 32-bit and 64-bit clients, download both ODAC packages (took me a while to find them, but just search for e.g. "oracle 64 bit odac" etc.). Install each of them to an Oracle Base path of "C:\Oracle\12.1.0.2.1\" and then for the Software Location add "\Client_x86" (or x64) onto that path. Everything that b_levitt has said then applies. Both versions are fully configured and 32-bit apps will use the 32-bit driver, and 64-bit apps will use the 64-bit driver. Easy-peasy.
  • Bender
    Bender almost 9 years
    Oh, and as of 2015-09-04 Oracle still hasn't released the Oracle Developer Tools for Visual Studio 2015. They promised to have it released within one month of VS2015 RTM, but it's well past that and still a no-show. :(
  • Martijn Pieters
    Martijn Pieters about 8 years
    Please describe (in your own words) what the instructions told you to do. The page you link to could easily disappear, and then we are all still none the wiser as to how to solve this error.
  • Sreekumar P
    Sreekumar P over 6 years
    This worked for me as well. By default IIS express will be running in 32 bit mode in VS.
  • Caverman
    Caverman about 4 years
    Just throwing this out in case someone comes across this. In my case the app pool's 32bit was set to True. I had to set it to False to get mine to work but it was the app pool.