Registrieren Passwort vergessen?
Alt 04.02.2008, 22:44   #1
Backslash
Commander
 
Benutzerbild von Backslash
 
Dabei seit: Okt 2006
Ort: /etc/sudoers
Beiträge: 2.301
[C#] 32bit DLL auf Vista 64bit nutzen

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.
__________________
sysprofile | deviantART | Website
C# - Immer wieder lecker
multiStreamRipper - Paralleles Rippen mehrerer Shoutcast-Webradio Streams
Backslash ist offline   Antwort
Anzeige
Alt 05.02.2008, 20:06   #2
TheCadillacMan
Lieutenant
 
Benutzerbild von TheCadillacMan
 
Dabei seit: Jun 2005
Beiträge: 580
AW: [C#] 32bit DLL auf Vista 64bit nutzen

Ich hab mal ein bisschen geforscht und das hier gefunden: Link!

Kernaussage: Du kannst unter x64 keine x86-DLL benutzen. Du musst dein C#-Projekt einfach als 32bit-Anwendung kompilieren. Dann läuft das.
__________________
Mein System
TheCadillacMan ist offline   Antwort
Alt 05.02.2008, 20:11   #3
1668mib
Admiral
 
Benutzerbild von 1668mib
 
Dabei seit: Mär 2006
Beiträge: 8.735
AW: [C#] 32bit DLL auf Vista 64bit nutzen

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)
1668mib ist offline   Antwort
Alt 05.02.2008, 21:14   #4
Backslash
Commander
Ersteller dieses Themas
 
Benutzerbild von Backslash
 
Dabei seit: Okt 2006
Ort: /etc/sudoers
Beiträge: 2.301
AW: [C#] 32bit DLL auf Vista 64bit nutzen

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.
__________________
sysprofile | deviantART | Website
C# - Immer wieder lecker
multiStreamRipper - Paralleles Rippen mehrerer Shoutcast-Webradio Streams
Backslash ist offline   Antwort
Alt 05.02.2008, 21:23   #5
AndrewPoison
Vice Admiral
 
Benutzerbild von AndrewPoison
 
Dabei seit: Jan 2005
Ort: D:\Thü\07318 Saalfeld Typ: Hardware-Exot
Beiträge: 6.280
AW: [C#] 32bit DLL auf Vista 64bit nutzen

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 ^^
__________________
"Heute morgen brach das Internet für 3 Stunden zusammen und stürzte die Nation in eine Produktivitätswelle. Der Ausfall, der zu signifikaten Arbeitsaufnahmen führte, bewirkte das sich die Anzahl von 120 Millionen amerikanischen Angestellen während der Arbeitszeit nicht mehr im Netz rumtrieben." -OnionNews
AndrewPoison ist offline   Antwort
Alt 06.02.2008, 13:48   #6
Backslash
Commander
Ersteller dieses Themas
 
Benutzerbild von Backslash
 
Dabei seit: Okt 2006
Ort: /etc/sudoers
Beiträge: 2.301
AW: [C#] 32bit DLL auf Vista 64bit nutzen

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:
Zitat:
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)
__________________
sysprofile | deviantART | Website
C# - Immer wieder lecker
multiStreamRipper - Paralleles Rippen mehrerer Shoutcast-Webradio Streams
Backslash ist offline   Antwort
Alt 07.02.2008, 17:17   #7
1668mib
Admiral
 
Benutzerbild von 1668mib
 
Dabei seit: Mär 2006
Beiträge: 8.735
AW: [C#] 32bit DLL auf Vista 64bit nutzen

Naja du hättest ja auch einfach das Compilat des x86-Systems nehmen könnne...
1668mib ist offline   Antwort
Alt 07.02.2008, 17:43   #8
Backslash
Commander
Ersteller dieses Themas
 
Benutzerbild von Backslash
 
Dabei seit: Okt 2006
Ort: /etc/sudoers
Beiträge: 2.301
AW: [C#] 32bit DLL auf Vista 64bit nutzen

"Einfach" wohl eher nicht, hab da kein VS installiert, dann ist es wohl einfacher es umzustellen O.o
__________________
sysprofile | deviantART | Website
C# - Immer wieder lecker
multiStreamRipper - Paralleles Rippen mehrerer Shoutcast-Webradio Streams
Backslash ist offline   Antwort
Antwort

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:49 Uhr.