为什么我什至不能将返回值从DLL C传递给C#?有帮助吗? [英] Why can't I even pass return value from DLL C to C#? Any Help?

查看:79
本文介绍了为什么我什至不能将返回值从DLL C传递给C#?有帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这个项目对我很有用,现在我使用的是窗口形式而不是控制台应用程序形式,并试图将int值从C中的DLL传递给C#.但是每次我收到的时候都是17岁,我不知道发生了什么.请帮助我,只有当我成功完成此步骤后,我才能继续前进,好可怜!!! :D
非常感谢

从C#调用:

Hi,
This project is useful for me and now I am using window form not console application form and trying to pass the int value from DLL in C to C#. But Everytime I received is 17, I don''t know what happened. Please help me with this, only when i succeed this step , I can go on, so poor!!! :D
Thanks a lot

From C# calling:

namespace GUI
{
    public partial class Form1 : Form
    {
        
        [DllImport("YaMone.dll")]
        public static extern int DisplayHelloFromDLL();
        private void button1_Click(object sender, EventArgs e)
        {

            label10.Text = "This is C# program";
            int fighting = DisplayHelloFromDLL();
            textBox1.Text = fighting.ToString();
        }
}



从C中的DLL:



From DLL in C:

extern "C"
{
  __declspec(dllexport) int DisplayHelloFromDLL()
  {
        printf ("Hello from DLL !\n");
        return 52;
  }
}

推荐答案

它看起来基本上是正确的.尝试在两侧明确指定命名约定,例如C ++中的__cdeclSystem.Runtime.InteropServices.DllImportAttribute中的CallingConvention.Cdecl等于CallingConvention.Cdecl.

我还建议使用System.Runtime.InteropServices.DllImportAttribute.EntryPoint显式命名入口点.这样,即使名称经过修饰或mangled( http://en.wikipedia.org/wiki/Name_mangling [ ^ ]).要查看实际名称,请使用某些二进制转储实用程序,例如DUMPBIN.EXE,该实用程序捆绑在所有版本的Visual Studio中;您可以从Visual Studio命令提示符中使用它.请参阅 http://msdn.microsoft.com/en-us/library /c1h23y6c%28v=vs.71%29.aspx [ ^ ].

另外,我想指出,您在示例DLL代码中使用了控制台,但使用Windows .NET应用程序对其进行了测试,默认情况下未显示该控制台.要显示控制台,除了应用程序类型外,不要在项目中进行任何更改.在项目属性中将其设置为控制台应用程序"-您的应用程序将同时用作控制台和窗口应用程序.

—SA
It looks basically correct. Try to explicitly specify naming convention on both sides, for example __cdecl in C++ and CallingConvention equals to CallingConvention.Cdecl in System.Runtime.InteropServices.DllImportAttribute.

I would also advice to explicitly name entry point by using System.Runtime.InteropServices.DllImportAttribute.EntryPoint. In this way, you don''t have to use extern "C" and bind the entry point even if the name is decorated or mangled (http://en.wikipedia.org/wiki/Name_mangling[^]). To see what the actual names are, use some binary dump utility like DUMPBIN.EXE, which is bundled with all versions of Visual Studio; you can use it from Visual Studio Command Prompt. See http://msdn.microsoft.com/en-us/library/c1h23y6c%28v=vs.71%29.aspx[^].

Also, I want to note that you use console in your sample DLL code but test it with Windows .NET application where the console is not shown by default. To show console, don''t change anything in the project but application type. Set it to "Console Application" in the project Properties — your application will work as console and window application at the same time.

—SA


向dll添加调用约定,例如:
Add calling convention to the dll, like:
extern "C"
{
  __declspec(dllexport) int __stdcall DisplayHelloFromDLL()
  {
        printf ("Hello from DLL !\n");
        return 52;
  }
}



最好的问候
Espen Harlinn



Best regards
Espen Harlinn


这篇关于为什么我什至不能将返回值从DLL C传递给C#?有帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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