作者yayarice (夜夜米)
看板C_Sharp
标题[问题] DllImpor错误: 试图载入格式错误的程式
时间Wed Oct 12 15:36:07 2011
我用的是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
1F:→ heartice:Project的build设定把platform改成x86看看 10/13 09:56
2F:→ yayarice:将platform改成x64後变成找不到进入点 10/14 16:38
[DllImport( "testdll.dll", EntryPoint = "?msgbox@@YAXXZ" )]
改成这样就行了
为什麽一定要打c++ mangle的函式全名才行呢?
puts连进入点都不用设啊??
※ 编辑: yayarice 来自: 140.114.54.45 (10/14 16:42)