using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace WindowsProject
{
    public partial class Form1 : Form
    {
        long hCom;        public class cCommOpendll
        {
            [DllImport("DrfApiV10.dll", EntryPoint = "DrfCommOpen", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern int DrfCommOpen(long hCom,string com_port);
        }
        public class cSetBaudRatedll 
        {
            [DllImport("DrfApiV10.dll", EntryPoint = "DrfSetBaudRate", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern int DrfSetBaudRate(long hCom,int BaudRate,byte NetAddr);        }
        public class cGetFirmwaredll 
        {
            [DllImport("DrfApiV10.dll", EntryPoint = "DrfGetFirmwareVersion", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern int DrfGetFirmwareVersion(long hCom,byte major,byte minor,byte NetAddr);
        }
        public class cParameterdll 
        {
            [DllImport("DrfApiV10.dll", EntryPoint = "GetParameter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern int GetParameter(long hCom,byte addr,byte Value,byte NetAddr);
        }
        public class cCommClosedll 
        {
            [DllImport("DrfApiV10.dll", EntryPoint = "DrfCommClose", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern int DrfCommClose(long hCom);
        }
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            comboBox3.Enabled = false;
            comboBox3.Text = "1";
            for (int j = 0;j <= 240;j++)
            {
                if (j <= 239)
                {
                    comboBox3.Items.Add(j + 1);
                }
                else
                {
                    comboBox3.Items.Add("通用地址");
                }
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            byte temp,temp1;
            int result;
            string strComm;           temp = Convert.ToByte(this.comboBox2.SelectedIndex);
           strComm = comboBox1.Text;           result = cCommOpendll.DrfCommOpen(hCom, strComm);
           if (result == 0)
           {
               label15.Text = " 打开串口" + (this.comboBox2.SelectedIndex + 1) + "成功!";
               temp1 = Convert.ToByte(cSetBaudRatedll.DrfSetBaudRate(hCom, temp, 255));
               if (temp1 == 0)
               {
                   label16.Text = " 设置波特率成功!";
                   if (temp1 == 0 && result == 0)
                   {
                       label17.Text = " 读卡器连接成功!";                       button1.Enabled = false;
                       button2.Enabled = true;
                       button3.Enabled = true;
                       button4.Enabled = true;
                       button5.Enabled = true;
                       button6.Enabled = true;
                   }
               }
               else
               {
                   label16.Text = " 设置波特率不成功!";
                   button2.Enabled = true;                   result = cCommClosedll.DrfCommClose(hCom);
               }
         }
         else
         {
             label15.Text = " 打开串口" + (this.comboBox2.SelectedIndex + 1) + "不成功!";
             button2.Enabled = true;
  
        }
     }        private void button3_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = true;
            groupBox5.Visible = false;
            groupBox8.Visible = false;
        }        private void button4_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = true;
            groupBox5.Visible = true;
            groupBox8.Visible = false;
        }
  }
}

解决方案 »

  1.   

    Hi,
    I have a native dll..the name of the DLL is "DrfApiV10.dll"..The dll has functions like;short __declspec(dllexport) __stdcall DrfCommOpen (HANDLE * hCom, char *com_port);I have been trying to call this function into C#.NET and I'm importing the function like this[DllImport("DrfApiV10.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "DrfCommOpen", CharSet = CharSet.Ansi)]
    public static extern apiStatus RfCommOpen(ref IntPtr hCom, IntPtr com_port);is it correct..???