创建Win32 DLL并从.NET应用程序调用。 [英] Creating a Win32 DLL and calling from a .NET Application.

查看:91
本文介绍了创建Win32 DLL并从.NET应用程序调用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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 signature DisplayMessage(StringBuilder szOper) cab be applied only in C#, because the type System.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆