创建Win32 DLL并从.NET应用程序调用。 [英] Creating a Win32 DLL and calling from a .NET Application.
问题描述
我使用Visual Studio 2008 Professional创建了一个Win32 DLL。我已经定义了一些函数,如下所示。
extern C
{
__declspec(dllexport) void DisplayMessageBox()
{
MessageBox(NULL,_T( 此文本已从DLL),_ T( Win 32 DLL中显示),MB_OK);
}
__declspec(dllexport) void DisplayMessage(TCHAR * szMessage)
{
MessageBox(NULL ,szMessage,_T( Win 32 DLL),MB_OK);
}
}
构建DLL,然后我尝试使用来自C夏普客户端。
喜欢这个。
public partial class frmCSClient:表单
{
[DllImport( Win32Dll.dll)] < span class =code-keyword> public static extern void DisplayMessageBox();
[DllImport( Win32Dll.dll)] public static extern void DisplayMessage(StringBuilder szOper);
public frmCSClient()
{
InitializeComponent();
}
private void btnMsgBox_Click( object sender,EventArgs e)
{
DisplayMessageBox();
DisplayMessage( new StringBuilder( ToTo 跨度>));
}
}
当我调用Dll时,我收到一条错误消息(System.BadImageFormatException)
尝试加载格式不正确的程序。 (HRESULT异常:0x8007000B)
试图加载格式不正确的程序。(HRESULT异常:0x8007000B)}
任何猜测我在这做错了什么。任何见解都会有所帮助。
提前致谢。
-prateek
签名DisplayMessage(StringBuilder szOper)
cab仅适用于C#,因为类型系统.Text.StringBuilder
是.NET BCL的一部分。这样的签名不能用在通过P / Invoke使用的方法中,因为没有这样的类和托管端,即使存在这样的结构/类,它也可能不一样。
因此,整个方法都是错误的,无法正常工作。使用默认编组,您只能传递基本类型,结构和一些近原始字符串类型。
-SA
I was creating a Win32 DLL using Visual Studio 2008 Professional. I have defined some functions as shown below.
extern "C"
{
__declspec(dllexport) void DisplayMessageBox()
{
MessageBox ( NULL, _T("This Text has been displayed from a DLL"), _T("Win 32 DLL"), MB_OK);
}
__declspec(dllexport) void DisplayMessage( TCHAR *szMessage )
{
MessageBox ( NULL, szMessage, _T ("Win 32 DLL"), MB_OK);
}
}
Build a DLL, then i tried to use this from a C Sharp Client.
Like this.
public partial class frmCSClient : Form
{
[DllImport("Win32Dll.dll")] public static extern void DisplayMessageBox();
[DllImport("Win32Dll.dll")] public static extern void DisplayMessage(StringBuilder szOper);
public frmCSClient()
{
InitializeComponent();
}
private void btnMsgBox_Click(object sender, EventArgs e)
{
DisplayMessageBox();
DisplayMessage(new StringBuilder("ToTo"));
}
}
When I am Calling Dll i am getting an Error message (System.BadImageFormatException)
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
"An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"}
Any guess What i am doing here wrong. any insight will be helpful.
Thanks in advance.
-prateek
The signatureDisplayMessage(StringBuilder szOper)
cab be applied only in C#, because the typeSystem.Text.StringBuilder
is a part of .NET BCL. Such signature cannot be used in the method used through P/Invoke, because there is no such class and the managed side, and, even if such structure/class exists, it could not be the same.
Therefore, the whole approach is wrong and cannot work. With default marshalling, you can only pass primitive types, structures and a number of near-primitive string types.
—SA
这篇关于创建Win32 DLL并从.NET应用程序调用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!