سؤال

C# application loading C++ dll:

Got this exception:

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

Exception happens at

CartToPol(fileName, _imageWidth, _imageHeight, _bytePerPixle);

in following C# code (about at the middle, scroll down a little you can see the line)

C# code:

using System;
// many usings here omitted .......   

namespace CarPoolUI
{
    public partial class MainWindow : Window
    {
        private int _imageWidth;
        private int _imageHeight;
        private int _bytePerPixle;
        private IntPtr _imageData;

        public MainWindow()
        {
            InitializeComponent();                  
        }

        [DllImport("D:\\Projects\\CarPool\\x64\\Debug\\CarPool.dll", EntryPoint = "CartToPol")]
        public static extern long CartToPol(string fileName, int imgWidth, int imgHeight, int bytePerPixel);


        public void ProcessData(bool doColor, string fileName)
        {
            try
            {
               string fileName = "D:\\myData\\data.dat";
            _imageWidth = 512;
            _imageHeight = 512;
            _bytePerPixle = 2;
                 **// this is where exceptions happen!!**
                CartToPol(fileName, _imageWidth, _imageHeight, _bytePerPixle);

            }
            catch(Exception ex)
            {
               string err = ex.ToString();
            }

        }

    }
}

C++ (CPP) code:

// CarPool.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "CarPool.h"

using namespace std;

namespace CarPool
{

    long CarPool::CartToPol(string fileName, long imgWidth, long imgHeight, long bytePerPixel)
    {
        long ret = 0;

       return ret;
    }

    long PolToCart(string fileName, long imgWidth, long imgHeight, long bytePerPixel)
    {
        long ret = 0;

       return ret;

    }    
}

C++ (.h) code

#include <string>


namespace CarPool
{
    // This class is exported from the CarPool.dll
    class CarPool
    {
    public: 
        // cartesian to polar
        long CartToPol(std::string fileName, long imgWidth, long imgHeight, long bytePerPixel);

  //polar to cartesian
        long PolToCart(std::string fileName, long imgWidth, long imgHeight, long bytePerPixel);


        //polar to cartesian
        static int* LoadData(std::string fileName, long imgWidth, long imgHeight, long bytePerPixel);
    };

}

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

هل كانت مفيدة؟

المحلول

An attempt was made to load a program with an incorrect format error normally means you are trying to load a 32 bit DLL from 64 bit code or vise-vera.

Your C++ project appears to be built as 64 bit code (since your path to the DLL includes a X64 in it). That woule likely mean that your C# application is running in 32 bit mode. That is likely as well since as of Visual Studio 2010 client application projects (console, WinForms, WPF) default to a 32 bit target.

Check the project settings of your C# application by double-clicking on the Properties item in the Solution Explorer. Then choose the "Build" section and ensure that the platform target is "x64". If it is "x86" that would mean that your C# code was running in 32 but mode.

Setting Enable 32-Bit Applications setting in IIS is incorrect for two reasons. First your wanted your C# code to run in 64 bit mode, and enableing that setting would force the code to run in 32 bit mode. Secondly that setting only affects applications running in IIS (read ASP.Net) not client applications.

نصائح أخرى

Given this line:

[DllImport("D:\\Projects\\CarPool\\x64\\Debug\\CarPool.dll", EntryPoint = "CartToPol")]

It appears that you are building a 64-bit DLL for p/invoke within your .net program.

My psychic powers suggest your .NET app is being compiled for 32-bit. Either compile both the DLL and App as 64-bit, or compile both as 32-bit.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top