发新帖

[C#/ASP.NET] C# 调用 C语言封装的dll文件

零下一度 2019-1-4 1282
//引用文件 创建一个接口文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace 项目名称
{
   public static class ReadDll
    { 
        [DllImport("demo.dll")]
        public static extern IntPtr 方法名1();
        
        [DllImport("demo.dll")]
        public static extern void 方法名2(IntPtr hDev);
        
        [DllImport("demo.dll")]
        public static extern int 方法名3(IntPtr hDev, out uint phyno);
        
        [DllImport("demo.dll")]
        public static extern int 方法名4(IntPtr hDev, byte[] cardno);
        
        [DllImport("demo.dll")]
        public static extern int 方法名5(IntPtr hDev, byte[] cardno, byte[] name);
        
        [DllImport("demo.dll")]
        public static extern int 方法名6(IntPtr hDev, int ms, int count); 
    }
}

//调用方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq; 
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 项目名称
{
        public partial class 类名{
        
            public string 方法1(){
            
                IntPtr hDev = ReadDll.方法1()
                
                byte[] no = new byte[18];
  
                IntPtr hDev = ReadDll.方法1();
                
                uint phyno;
                
                ReadDll.方法名3(hDev, out phyno);
                
                byte[] cardno = new byte[18];
                
                int ret = ReadDll.方法名4(hDev, cardno);
                
                string msg = System.Text.Encoding.Default.GetString(cardno);
                
                res = msg.ToString();
                
                res = res.Replace("\0", "");
                
                ReadDll.方法2(hDev);
  
                return res;
            }
        }
}


最新回复 (0)
返回
零下一度
主题数
931
帖子数
0
注册排名
1