很多时候为了做集成我们别无选择的要用C#来调用C/C++的API,如果你对C#和C/C++数据类型对应关系不是很了解,那么这就是一件很头疼的事情了。

下面来看看具体怎么使用

void* OpenDevice(int nMyAddress, HWND hWnd)
void CloseDevice(void* pDevice)
void SetMyAddress(void* pDevice, int nAddress) 
BOOL ConnectPort(void* pDevice, LPCTSTR lpCommDef)
BOOL IsConnect(void* pDevice)
void SetAddLog(void* pDevice, BOOL bAddLog)
void SetCommKey(void* pDevice, LPCTSTR lpCommKey)
void SetInnerCode (void* pDevice, BOOL bBig5)
BOOL DisConnectPort(void* pDevice)
void SetWaitTime(void* pDevice, DWORD dwWaitTime)
void* StartICDMCommand(void* pDevice, int nAddress, int nCommand, 
void* pParameters = NULL, int nSizeOfParameter = 0)
int GetSizeOfData(void* pCommand)
BOOL GetData(void* pCommand, void* pDataBuffer, int nSize)
int GetCmdResult(void* pCommand)
void EndICDMCommand(void* pCommand)
void RecvEdition(LPTSTR lpEdition)

上面是api接口原函数

如果想要在C#里面使用就要重新定义以为他是非托管类行

[DllImport("DLL4k.dll", ExactSpelling = false, EntryPoint = "OpenDevice")]
public static extern IntPtr OpenDevice(int nMyAddress, IntPtr hwnd);
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "CloseDevice")]
public static extern void CloseDevice(IntPtr hwnd);
[DllImport("DLL4k.dll", SetLastError = true)]
public static extern void SetMyAddress(IntPtr hwnd, int nMyAddress);
[DllImport("DLL4k.dll", ExactSpelling = false, EntryPoint = "ConnectPort")]
public static extern bool ConnectPort(IntPtr pDevice, StringBuilder port);  
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "IsConnect")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool IsConnect(IntPtr pDevice);
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "SetAddLog")]
public static extern void SetAddLog(IntPtr pDevice, bool isaddlog);
[DllImport("DLL4k.dll", SetLastError = true)]
public static extern void SetCommKey(IntPtr pDevice, string str);
[DllImport("DLL4k.dll", SetLastError = true)]
public static extern void SetInnerCode(IntPtr pDevice, bool isbog5);
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "DisConnectPort")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool DisConnectPort(IntPtr pDevice);
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "SetWaitTime")]
public static extern void SetWaitTime(IntPtr pDevice, int dwWaitTime);
[DllImport("DLL4k.dll", ExactSpelling = false, EntryPoint = "StartICDMCommand")]
public static extern IntPtr StartICDMCommand(IntPtr pDevice, int nAddress, int nCommand, IntPtr pParameters, int nSizeOfParameter = 0);
[DllImport("DLL4k.dll", EntryPoint = "GetSizeOfData", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetSizeOfData(IntPtr pParameters);
[DllImport("DLL4k.dll", EntryPoint = "GetData", SetLastError = true)]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool GetData(IntPtr pCommand, IntPtr pDataBuffer, int nSize);
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "GetCmdResult")]
public static extern int GetCmdResult(IntPtr pCommand);
[DllImport("DLL4k.dll", SetLastError = true, EntryPoint = "EndICDMCommand")]
public static extern void EndICDMCommand(IntPtr pDevice);
[DllImport("DLL4k.dll", SetLastError = true)]
public static extern void RecvEdition(string str);

以上就是c#调用函数一定要是静态的

下面来看看一个列子

public IntPtr hwnd;
StringBuilder sb = new StringBuilder();
public static IntPtr showhwnd = System.Diagnostics.Process.GetCurrentProcess().Handle;
public static IntPtr api = OpenDevice(0, showhwnd);
public IntPtr kk = new IntPtr();
public static int rcount = 0;

以上要是全局变量

最后是下载数据的调用例子

sb.Clear();
sb.AppendFormat("{0}:baud={1},parity=N,data=8,stop=1", cbPort.Text, cbRate.Text);
int count = 0;
if (!IsConnect(api))
{
    bool islink = ConnectPort(api, sb);
    DMSTATUS dmm = new DMSTATUS();
    int sizenewb = Marshal.SizeOf(dmm);
    IntPtr buffernew = Marshal.AllocCoTaskMem(sizenewb);
    int sizenewa = 0;
    try
    {
    //将托管对象拷贝到非托管内存
    SetWaitTime(api, 3000);
    Marshal.StructureToPtr(dmm, buffernew, true);
    SetAddLog(api, true);
        kk = StartICDMCommand(api, 0, (int)CMDWORD.nRecvDMStatus, buffernew, sizenewb);
        rcount = GetCmdResult(kk);
        if (rcount != 0)
        {
            sizenewa = GetSizeOfData(kk);
            bool isok = GetData(kk, buffernew, sizenewa);
            DMSTATUS mm = (DMSTATUS)Marshal.PtrToStructure(buffernew, typeof(DMSTATUS));
            count = mm.nRecordCount;
            EndICDMCommand(kk);
            StringBuilder sbt = new StringBuilder();
            int totalcount = 0;
            if (count % 100 == 0)
            {
            totalcount = count / 100;
            }
            else
            {
               totalcount = (count / 100) + 1;
            }
            for (int i = 0; i < totalcount; i++)
            {
               bool link = ConnectPort(api, sb);
               if (link)
               {
                  ATTGUARDRECORD[] Record = new ATTGUARDRECORD[100];
                  int sizerecord = Marshal.SizeOf(Record[0]);
                  IntPtr bufferrecord = Marshal.AllocCoTaskMem(sizerecord * 100);
                  kk = StartICDMCommand(api, 0, (int)CMDWORD.nRecvDelRecord, bufferrecord, sizerecord);
                  rcount = GetCmdResult(kk);
                  if (rcount > 0)
                  {
                     int sizecount = GetSizeOfData(kk);
                     GetData(kk, bufferrecord, sizecount);
                     for (int j = 0; j < 100; j++)
                     {
                     Record[j] = (ATTGUARDRECORD)Marshal.PtrToStructure((IntPtr)(bufferrecord + sizerecord * j), typeof(ATTGUARDRECORD));
                     if (Record[j].nYear > 0)
                     {
                       sbt.AppendLine(Record[j].strPersonID + "-" +Record[j].nYear.ToString() + "-" + Record[j].nMonth.ToString() + "-" + Record[j].nDay.ToString() + "-"+ Record[j].nHour.ToString() + ":" + Record[j].nMinute.ToString()+ ":" + Record[j].nSecond.ToString()+ "-" +Record[j].bOnDuty.ToString() + "-" + Record[j].bBC.ToString() + "-" + Record[j].nDMAdr);
                                }
                                else
                                {
                                    continue;
                                }
                        }
                        IntPtr del = new IntPtr();
                        StartICDMCommand(api, 0, (int)CMDWORD.nRecvDelRecordRlt, del);
                        }
                    }
                    EndICDMCommand(kk);
            }
            txtcontent.Text = sbt.ToString();
            }
        }
        catch
        {

}

}

这样就结束了

转载于:https://www.cnblogs.com/keepsilence/archive/2011/09/20/2182373.html

关于C#调用API的理解(汇多考勤机HD4K)相关推荐

  1. 本地方法(JNI)——使用调用API

    [0]README 1) 本文文字描述+source code 均转自 core java volume 2 , 旨在理解 本地方法(JNI)--使用调用API 的基础知识 : 2) for sour ...

  2. oauth2和jwt_使用具有OAuth2的Web应用程序和JWT的使用来调用API – WSO2 API Manager

    oauth2和jwt 在这篇文章中,我将分享我在WSO2 API管理器(API-M)中针对行业中非常常见且有用的场景的经验和理解. 简要地说明以下流程. 公开了一个API,供应用程序开发人员在API管 ...

  3. 使用带有OAuth2的Web应用程序和使用JWT来调用API – WSO2 API Manager

    在这篇文章中,我将分享我在WSO2 API管理器(API-M)方面的经验和理解,以解决该行业中非常普遍和有用的情况. 简要地说明以下流程. 公开了一个API,供应用程序开发人员在API Manager ...

  4. Apsara Clouder专项技能认证:实现调用API接口

    一.API 简介 1.API 的概念 API(Application Programming Interface应用程序编程接口)是一些预定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访 ...

  5. MySQL数据库进阶(API的理解、Python对MySQL的接口)

    文章目录 一.API的理解 二.Python对MySQL的API 本篇文章主要是关于Python对MySQL的接口API的内容,简单来说就是在Python中调用MySQL语句来执行 一.API的理解 ...

  6. 【100个 Unity踩坑小知识点】| Unity调用API ,动态获取Android权限,附带所有Android权限表格

    Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 . 包括游戏开发.美术.建筑.汽车设计.影视在内的所有创作者,借助 Unity 将创意 ...

  7. 调用API接口 获取和解析 京东按关键字搜索商品数据

    这篇文章主要介绍了如何调用API接口 获取和解析京东按关键字搜索商品数据,帮助大家更好的理解和使用API接口. 点击测试 请求示例 <?php// 请求示例 url 默认请求参数已经URL编码处 ...

  8. 阿里Apsara Clouder专项技能认证:实现调用API接口——笔记

    课时1:API简介 API的概念 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访 ...

  9. java扫描接口_一种扫描接口并生成可调用API接口文档的方法与流程

    本发明属于JavaWeb开发技术领域,涉及一种API接口文档的生成方法,尤其是一种扫描接口并生成可调用API接口文档的方法. 背景技术: API(Application Programming Int ...

最新文章

  1. Easy Problem 7 求反数字字符串
  2. [UE4蓝图教程]蓝图入门之蓝图通信机制入门
  3. python 上传文件夹,python – 使用Flask上传文件夹/文件
  4. (分组交换时延)谢希仁 第七版第一章1-10题详细解答
  5. Ubuntu安装amule和编译安装amule-dlp
  6. pandas 数据索引与选取
  7. python从txt拿取数据_python requests + xpath 获取分页详情页数据存入到txt文件中
  8. 易华录发布蓝光存储新品
  9. Ethernet、VLAN、QinQ
  10. undefined reference to `create_module''
  11. 坐标字符NYOJ 298 点的变换 (矩阵快速幂)
  12. matlab中mov是什么意思,PLC编程中MOV是什么意思
  13. 使用命令结束Linux系统
  14. Wonderful Coloring - 2
  15. 以前的windows安装文件可以删除吗_Windows系统中“C盘”可以删除的文件—让你最大限度提C盘空间...
  16. 剑指 Offer 42. 连续子数组的最大和java
  17. SIM7600CE TCP AT指令
  18. w ndows10支持微信吗,windows10系统怎样安装微信客户端
  19. ROS学习笔记08、机器人导航仿真(slam、map_server、amcl、move_base与导航消息介绍)
  20. 周杰伦2015魔天伦2世界巡回演唱会各站曲目歌单(完结)

热门文章

  1. Java 实现单例模式的 9 种方法
  2. Hystrix文档-实现原理
  3. spring boot应用启动原理分析
  4. Android --- 微信支付时出现错误:错误的签名,验签失败,return_code=FAIL
  5. Android --- textColorHint与textColor的用法介绍(包懂)
  6. php禁止国外访问,wordpress 禁止国外ip访问
  7. 2026年,数据中心电力市场将达到150亿美元
  8. 1000亿,行业巨头纷纷押宝的数据中心
  9. 某银行省级数据中心IT运维服务体系建设完整思路
  10. 【解决方案】MTU现场能源发电机,点亮孤岛的文明之光