BadImgeFormatException when trying to start an application after reinstalling Windows 8.1
I hope this question will not be viewed as a duplicate, as I know there are many similar questions on stackoverflow. I need to know. I've already read most of them. So bear with me ...
I am writing a C # application that relies on the OpenCV libraries to work. OpenCV libraries are written in C ++ code and to use them in C #, I wrote 2 libraries: a static library containing the methods that I want to get from OpenCV; CLR DLL that acts as a bridge between the static lib and the C # project.
What bothers me is that everything works fine until I reinstalled Windows 8.1 due to a virus. Before reinstalling, the application is compiled and works as expected. After reinstalling, the same project throws "BadImageFormatException" when trying to debug:
namespace G19_GUI
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new GUI.GUI()); //this is the line producing the exception
}
}
}
Additional information: Could not load file or assembly 'clrLib, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I know that the most common reason for this exception is when the application is trying to load a 32 bit library into a 64 bit compiled application, or vice versa. Thus, I have confirmed that the OpenCV libraries I am trying to use are intended to be used in a 32-bit application, and this is indeed the case. I tried to switch to 64-bit DLLs, which caused 39 errors like "LINK2028: Unresolved token".
Be aware that the project itself has not changed between the two Windows installations. I used the same property sheet and the same OpenCV libraries. Everything was saved on an external drive, which was disabled at the time when I stupidly double clicked the virus executable and made a backup to DropBox, the only thing I had to do after reinstalling Windows was resetting the environment and environment variables of OpenCV which I was using using a .bat file, the same .bat file that was used to set the same variables when you first installed Windows Therefore, I highly doubt the possibility of a virus infection with the project files. However, I double checked everything and found nothing wrong.
I started to document the exception online. As a result, I decided to try every possible combination of config builds and target platforms on which I dared to be patient to try all three projects I had in my solution.
Indeed, trying to build and compile for a 64-bit machine threw the exception, but the other DLL my C # project depended on was intended to be 32-bit, and when trying to load that DLL, the same exception occurred (as expected). Unfortunately the DLL does not come with a 32 bit version or source code to try and build my 64 bit version of the said DLL.
Thus, I am forced to create my application both for target platforms (any processor, mixed platform, and for any) or only for 32-bit. Which of course throws a startup exception.
I know that all my DLLs are built for 32-bit use. For my life, I can't figure out what the problem is and why this problem didn't exist before I reinstalled os.
Below is the code of my cllLib dll:
#include "Stdafx.h"
#include "clrLib.h"
#include <vector>
namespace clrLib
{
public ref class mapper
{
private:
static openCVProc * myProc;
public:
static mapper(void)
{
myProc = new openCVProc();
}
char * mapStringToChar(String ^ path)
{
return (char*)(void*)Marshal::StringToHGlobalAnsi(path);
}
array<int>^ getRGBfromHSV(int h, int s, int v)
{
array<int>^ values = gcnew array<int>(3);
std::vector<int> myVals = myProc->hsv2rgb(h, s, v);
values[0] = myVals[0];
values[1] = myVals[1];
values[2] = myVals[2];
return values;
}
array<int>^ getHSV(String^ src)
{
array<int>^ vals = gcnew array<int>(3);
vals[0] = (myProc->getHSV(mapper::mapStringToChar(src)))[0];
vals[1] = (myProc->getHSV(mapper::mapStringToChar(src)))[1];
vals[2] = (myProc->getHSV(mapper::mapStringToChar(src)))[2];
return vals;
}
void openCam()
{
myProc->startCam();
}
void closeCam()
{
myProc->stopCam();
}
int^ getBrightness()
{
int^ b;
b = myProc->getB();
return b;
}
bool^ camState()
{
return myProc->camState();
}
array<Byte>^ mapper::getFrameData()
{
cv::Mat f = myProc->getFrame();
int width = f.cols;
int height = f.rows;
array<Byte>^ bitmapData = gcnew array<Byte>(width*height * 3);
int c = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
bitmapData[c] = f.at<cv::Vec3b>(i, j)[0];
bitmapData[c + 1] = f.at<cv::Vec3b>(i, j)[1];
bitmapData[c + 2] = f.at<cv::Vec3b>(i, j)[2];
c += 3;
}
}
return bitmapData;
}
Size^ mapper::getFrameSize()
{
std::vector<int> size = myProc->getFrameSize();
Size^ frameSize = gcnew Size(size[0], size[1]);
return frameSize;
}
};
}
The other 2 libraries are a bit long, but I'll include them if necessary.
Thanks for taking the time to read my post.
source to share
No one has answered this question yet
Check out similar questions: