[問題] DllImpor錯誤: 試圖載入格式錯誤的程式
我用的是Visual Studio 2008
我先開了一個c++的Win32的dll空專案"testdll":
// testdll.h
__declspec( dllexport ) void msgbox( void );
// testdll.cpp
#include <windows.h>
__declspec( dllexport ) void msgbox( void ){
MessageBox( NULL, L"In DLL", L'message box", MB_OK );
}
然後開了一個C#的專案"ConsoleApplication1"
並將testdll.dll丟到"ConsoleApplication1\bin\Debug":
// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport( "testdll.dll" )]
public static extern void msgbox();
[DllImport( "msvcrt.dll" )]
public static extern int puts( string c );
static void Main( string[] args )
{
puts( "In puts\n" );
msgbox();
}
}
}
預期結果:
主控台印出: In puts
並跳出對話框
實際結果:
主控台什麼都沒印出
也沒跳出對話框
反而跳出例外狀況的對話框:
類型"System.BadImageFormatException"的未處理例外狀況發生於
ConsoleApplication1.exe
其他資訊:試圖載入格式錯誤的程式 (例外狀況發生於 HRESULT:0x8007000B)
dll的寫法應該沒問題 我用c++程式呼叫是正確的
c#的import也是我照msdn寫的
為什麼沒辦法順利呼叫呢?......
請各位大大替小的解惑
謝謝~!!
--
劍是凶器,劍術是殺人之術
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.54.45
→
10/13 09:56, , 1F
10/13 09:56, 1F
→
10/14 16:38, , 2F
10/14 16:38, 2F
[DllImport( "testdll.dll", EntryPoint = "?msgbox@@YAXXZ" )]
改成這樣就行了
為什麼一定要打c++ mangle的函式全名才行呢?
puts連進入點都不用設啊??
※ 編輯: yayarice 來自: 140.114.54.45 (10/14 16:42)
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章