none
关于c#调用非net的DLL RRS feed

  • 问题

  • C#一个非nET的DLL 原型函数 int Readkey(char* sett,char* rdata,int cmd) ,在C#里要怎么定义 sett为传入值,rdata是存储返回输出的,整个函数成功/失败返回数字值,其中两个char* 在C#下要怎么定义?

    sett的值为 "1,9600,N,8,1"

    2011年10月27日 6:56

答案

  • Hi 飘雪,

    这里你需要使用P/Invoke平台调用: http://msdn.microsoft.com/zh-cn/library/aa288468(v=vs.71).aspx.
     1. [DllImport("DLLTest.dll")]将你的C++ DLL加载进来.
         这里字符串中是dll的路径,如果dll和C#的executable不在同一路径下, 参考这篇去指定路径:
         http://www.cnblogs.com/virusswb/archive/2008/05/30/1210520.html.
     2. public static extern int Readkey(string sett, ref string rdata, int cmd);将你的方法加载进来.
         这里C#中用string来代替C++中的char*, C#的int(System.Int32)代替C++的int.
    然后就可以在你的代码中使用这个方法了.
    -------------------------------------------------
    This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.
    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    祝你快乐每一天,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月28日 6:52
    版主
  • char* 的用 string,如果是传出,C++ 有带 [OUT] 的,使用 out;没有的可以尝试 ref。

    [DllImport("YourDll")]
    public static extern ReadKey(string sett, ref string rdata, int cmd);


    Mark Zhou
    2011年10月28日 8:47

全部回复

  • 学习一下 DllImport 属性
    2011年10月27日 11:03
  • Hi 飘雪,

    这里你需要使用P/Invoke平台调用: http://msdn.microsoft.com/zh-cn/library/aa288468(v=vs.71).aspx.
     1. [DllImport("DLLTest.dll")]将你的C++ DLL加载进来.
         这里字符串中是dll的路径,如果dll和C#的executable不在同一路径下, 参考这篇去指定路径:
         http://www.cnblogs.com/virusswb/archive/2008/05/30/1210520.html.
     2. public static extern int Readkey(string sett, ref string rdata, int cmd);将你的方法加载进来.
         这里C#中用string来代替C++中的char*, C#的int(System.Int32)代替C++的int.
    然后就可以在你的代码中使用这个方法了.
    -------------------------------------------------
    This response contains links reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you.
    Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.
    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    祝你快乐每一天,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月28日 6:52
    版主
  • char* 的用 string,如果是传出,C++ 有带 [OUT] 的,使用 out;没有的可以尝试 ref。

    [DllImport("YourDll")]
    public static extern ReadKey(string sett, ref string rdata, int cmd);


    Mark Zhou
    2011年10月28日 8:47
  • 是的, 没有注意"rdata是存储返回输出的"这句, 已经修改了回复.
    周末愉快,


    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月28日 8:52
    版主