[C#] 32bit DLL auf Vista 64bit nutzen

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)
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
}
(C++, main.h)
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__
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
Code:
using System;
using System.Runtime.InteropServices;

class Program
{	
	[DllImport("nvista.dll")]
	static extern int GetTemperature();

	static void Main()
	{	
		Console.WriteLine(GetTemperature());
		Console.ReadKey();
	}
}
Und da muss wohl das Problem liegen, irgendwie muss die dll anders imported werden. Weiß jemand wie?

Schonmal danke. :)
 
Ich geh mal schwer davon aus, dass sein C#-Programm auch lediglich als 32-Bit-Programm kompiliert wurde.
(Und wenn nicht, frage ich mich, wieso man hier ein 64-Bit-Prozess benötigt)
 
Wo stellt man um ob 32/64bit compiling? (VCS08Express) Macht das einen Unterschied? Weil es läuft auf Vista 64 wohl als 64bit aber unter Vista 32 gehts auch, muss dort also als 32bit laufen.
 
Na Backslash, programmierst ja wirklich ;)

Mh... könntest alternativ noch 2 DLLs programmieren, eine für 64 und eine 32er. Mein Proggy würde dann beim Aufruf selbst entscheiden, welche er aufruft - abhängig davon, ob es eben unter V32 oder V64 gestartet wurde. Da meine Anwendung allerdings selbst 32-bittig ist, weiss ich nicht in wie weit sie 64er DLLs importieren kann... das Leben ist gemein ^^
 
Also man muss tatsächlich als x86 compilen. Die Einstellung muss man aber erstmal finden...
Die Lösung fand ich zufällig auf einer Flight Simulator Website:
1. Open Tools->Options
2. Check “Show all settings”
3. Check “Project and Solution->Show Advanced Build Configurations”
4. Open Build->Configuration Manager
5. Select the combo box under the “Platform” column for the project you want to target x86
6. Choose “NEW…”
7. Under “New Platform” select x86
8. Click OK, then Close
9. Note that the Toolbar combobox for Platform Configuration now lists both "x86" and "AnyCPU" and has x86 selected
10. Building, running and debugging will now build x86-only binaries. You can switch back to the "AnyCPU" configuration to change this if necessary.
(quelle: http://www.fsinsider.com/developers/Pages/default.aspx)
 
Naja du hättest ja auch einfach das Compilat des x86-Systems nehmen könnne...
 
"Einfach" wohl eher nicht, hab da kein VS installiert, dann ist es wohl einfacher es umzustellen O.o
 
Zurück
Oben