Backslash
Captain
- Registriert
- Okt. 2006
- Beiträge
- 3.242
Hi,
Ich habe mit C++ eine kleine DLL erstellt, die ich mit C# nutzen will.
Jetzt das Problem: Wenn ich das ganze auf Vista 64bit mache bekomme ich immer eine System.BadImageFormatException. Unter Vista 32bit und Win XP keine Probleme.
Mein Code:
(C++, main.cpp)
(C++, main.h)
daraus wird dann die nvista.dll. Das ganze ist fast vollständig von Code::Blocks generiert und funktioniert wie gesagt auf WinXP sowie Vista 32bit.
Abschließend noch der C#-Code
Und da muss wohl das Problem liegen, irgendwie muss die dll anders imported werden. Weiß jemand wie?
Schonmal danke.
Ich habe mit C++ eine kleine DLL erstellt, die ich mit C# nutzen will.
Jetzt das Problem: Wenn ich das ganze auf Vista 64bit mache bekomme ich immer eine System.BadImageFormatException. Unter Vista 32bit und Win XP keine Probleme.
Mein Code:
(C++, main.cpp)
Code:
#include "main.h"
// Temp Emulator Function
int DLL_EXPORT GetTemperature()
{
return 77;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
Code:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
int DLL_EXPORT GetTemperature();
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
Abschließend noch der C#-Code
Code:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("nvista.dll")]
static extern int GetTemperature();
static void Main()
{
Console.WriteLine(GetTemperature());
Console.ReadKey();
}
}
Schonmal danke.