c++中的原型如下:1 打开端口(可指定波特率)
原型: BYTE WINAPI  OpenComPortEx(int port,DWORD baud)
参数: port: 端口号(1-255)
      baud: 波特率(9600,19200,28800,57600,115200)
返回: 成功返回02 关闭端口
原型: BYTE WINAPI  ClosePort(void)
参数: 无
返回: 成功返回03 读取记录
原型: BYTE WINAPI  ReadRec(BYTE *recNo,char *buf)
参数: recNo: 记录数
      buff: 记录内容(每条记录20个字节)
返回: 成功返回0
请问在C#中怎么调用这些函数?
[DllImport(PICK,CharSet=CharSet.Auto)]
public static extern byte OpenComPortEx(int port,??);这些参数的类型分别对应C#中的哪些?如BYTE *recNo

解决方案 »

  1.   

    使用vc++6.0创建dll,然后用vs2010调用。
    1.vc++6.0创建动态链接库工程:win32 Dynamic-Link Library。随便起名,dllDemo,不过注意的是,你生成的dll文件是和你的工程名是一样的而不是你的cpp文件名。创建dllDemo.cpp和dllDemo,h。下面是代码。
    /*
    * dllDemo.h
    */
    extern "C" _declspec(dllexport) int Sum(int a,int b);//加法函数。
    extern "C" _declspec(dllexport) int Max(int a, int b);//取较大值函数
    extern "C" _declspec(dllexport) int Min(int a, int b);//取较小值函数
    ///:~
    /*
    *dllDemo.cpp
    *
    #include "dllDemo.h"
    extern "C" _declspec(dllexport)int Sum(int a, int b)
    {
     return a+b;
    }
    extern "C" _declspec(dllexport)int Max(int a, int b)
    {
     if(a>=b)
      return a;
     else
      return b;
    }extern "C" _declspec(dllexport)int Min(int a, int b)
    {
     if(a>=b)
      return b;
     else
      return a;
    }
    ///:~
    然后编译运行,会在debug目录下找到dllDemo.dll这个动态链接库。
    下一步,在c#中调用。新建,项目,控制台应用程序。(注意是C#,不是C#环境您重新切一下),在Program.cs中贴入以下代码:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;//引入dll文件中的函数    class Program
        {        //引入dll文件中的函数
            [DllImport("dllDemo.dll")]
            private static extern int Sum(int a, int b);
            [DllImport("dllDemo.dll")]
            private static extern int Max(int a, int b);
            [DllImport("dllDemo.dll")]
            private static extern int Min(int a, int b);
            static void Main(string[] args)
            {
                int a = Sum(3, 5);
                Console.WriteLine(a);
                Console.WriteLine(Max(5,10));
                Console.WriteLine(Min(12,25));
                Console.ReadKey();//直接main里边最后加个Console.Read()不就好了。要按键才退出。
            }
        }
    然后把你编译好的dllDemo.dll粘贴到工程的bin下的debug目录下(vs2008可能直接是debug目录,总之弄到debug目录下就好),运行系统。
    OK,会出现一个什么栈不对称的错误,这是怎么回事呢?你把工程属性的框架改成.net framwork 2.0的,就好用啦。这个原因不用我解释了吧,无非就是驴唇不对马嘴啦。
      

  2.   

    1.引入命名空间
    using System.Runtime.InteropServices
    2.写调用的方法
     [DllImport("READCARD.DLL", EntryPoint = "STK_DownLoadList")]
      private static extern int STK_DownLoadList(参数);
    例子:
     /*
            功能:下载资料信息到采集器
            参数:nPort.连接采集器的串口号, cSeparatore.指定文件的分隔符, pszFileName.待下载的文件名, nFieldMaxWidth.各字段的最大长度, pszRe.保留今后扩展使用
            返回:0 成功 -1 连接失败 -2 通讯失败
            备注:保证此串口不被其它程序占用;数据库能连接正常
             * int STK_DownLoadList(int nPort, char cSeparatore, char *pszFileName, int *nFieldMaxWidth, char* pszRe);
            */
            [DllImport("Easytech.dll", EntryPoint = "STK_DownLoadList")]
            public static extern int STK_DownLoadList(int nPort, sbyte cSeparatore,
                string pszFileName, int[] nFieldMaxWidth, string pszRe);
         
            /
      

  3.   

    BYTE WINAPI OpenComPortEx(int port,DWORD baud)
    对应
    byte OpenComPortEx(int port,uint baud)BYTE WINAPI ClosePort(void)
    对应
    byte ClosePort()BYTE WINAPI ReadRec(BYTE *recNo,char *buf)
    对应
    byte ReadRec(ref byte recNo,string buf)
      

  4.   

    有一本书叫做‘.net平台交互’具体的不记得。
    详细记载不同语言之间的交互。
      

  5.   

    学习学习,以前在C#调用过c++的dll,没有遇到类型转换的问题。
      

  6.   

    1,public partial class NativeMethods {
        
        /// Return Type: BYTE->unsigned char
        ///port: int
        ///baud: DWORD->unsigned int
        [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="OpenComPortEx")]
    public static extern  byte OpenComPortEx(int port, uint baud) ;}
    2,public partial class NativeMethods {
        
        /// Return Type: BYTE->unsigned char
        [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="ClosePort")]
    public static extern  byte ClosePort() ;}
    3,public partial class NativeMethods {
        
        /// Return Type: BYTE->unsigned char
        ///recNo: BYTE*
        ///buf: char*
        [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="ReadRec")]
    public static extern  byte ReadRec(ref byte recNo, System.IntPtr buf) ;}
      

  7.   


    long OpenComPortEx(int Port,UInt32 baud); 
    C++ C#
    传入的char* string
    传出的char* StringBuilder(预分配空间)
    short short
    char byte
    DWORD    Int32 or UInt32
    char[n] fixed byte[n]
    结构指针 结构指针
    函数指针 委托 
      

  8.   

      把c++的功能封装成OCX比较方便