如何在c#中调用来自c native api的方法和事件 [英] how to call method and event from c native api in c#

查看:197
本文介绍了如何在c#中调用来自c native api的方法和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用用c(c native api)编写的方法和事件。

并且想要使用ABC和(或ABC.lib作为静态链接)与C和C ++接口。



I want to call method and event which written in c(c native api).
and want to use of ABC.dll (or ABC.lib for static link) with the C and the C++ interfaces.

C functions are described in ABC_Functions.h ;
C/C++ ABC error codes are descibed in ABC_Errors.h ;
C++ enumerations and constants are described in ABC_Types.h ;
C++ classes and methods are described in ABC_Classes.h ;
any of them can be used by including ABC.h in the project that needs ABC capabilities.





我的代码是



My code is

[DllImport("ABC.dll",CallingConvention = CallingConvention.StdCall)]
    static extern void  ABC();
    [DllImport(" ABC.dll", CallingConvention = CallingConvention.StdCall)]
    static extern IntPtr GetDisplay();
    [DllImport(" ABC.dll",CallingConvention = CallingConvention.StdCall)]
    static extern IntPtr SetDisplay(IntPtr displayhandle);
    [DllImport(" ABC.dll",  CallingConvention = CallingConvention.StdCall)]
    static extern Int32 GetNumberOfDevices();







点击按钮






On button click

private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               SetDisplay(this.Display.Handle);
               GetDisplay();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }



msg是

无法在DLL中找到入口点SetDisplay ABC.dll


msg is
"Unable to find an entry point SetDisplay" in DLL ABC.dll

推荐答案

关于调用函数:问题很简单:ABC.DLL中没有这样的方法。在编写P / Invoke代码之前,检查导出的内容和名称(名称可以 mangled http://en.wikipedia.org/wiki/Name_mangling [ ^ ])。



您可以使用一些二进制转储实用程序,例如dumpbin.exe,您可以从Visual Studio命令行调用它: br />
http://support.microsoft.com/kb/177429 [ ^ ],

http://msdn.microsoft.com/en-us/library/c1h23y6c.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/20y 05bd5.aspx [ ^ ]。



关于召集事件:在这一部分,这个问题毫无意义。 1)没有召集事件这样的概念。 2)在本机API中,没有事件,就像你在.NET中一样。即使在.NET中:事件也不会被调用,它们被调用(非常非常不同的概念);此外,除了声明类型之外,你不能从任何地方调用它们。



-SA
About calling a function: the problem is very simple: there is no such method in "ABC.DLL". Before writing your P/Invoke code, check up what is exported and under what name (a name can be mangled: http://en.wikipedia.org/wiki/Name_mangling[^]).

You can use some binary dump utility, such as dumpbin.exe, which you can call from the Visual Studio Command Line:
http://support.microsoft.com/kb/177429[^],
http://msdn.microsoft.com/en-us/library/c1h23y6c.aspx[^],
http://msdn.microsoft.com/en-us/library/20y05bd5.aspx[^].

About "calling an event": in this part, the question makes no sense at all. 1) There is no such notion as "call an event". 2) In native API, there are no "events", nothing like you have in .NET. Even in .NET: events are not called, they are invoked (very, very different concept); besides, you cannot invoke them from anywhere except their declaring types.

—SA


这篇关于如何在c#中调用来自c native api的方法和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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