qt5 顯示呼叫CAN卡第三方庫並呼叫函式

2021-08-09 16:54:17 字數 3821 閱讀 6912

qt呼叫第三方庫一般有兩種方式,顯示和隱式。這裡主要介紹顯示呼叫can卡第三方庫(因為隱式呼叫can失敗了,太懶,不想弄了

一、隱式呼叫建立工程

將xx.h和xx.lib放入當前工程目錄

xx.dll檔案放入debug和release資料夾中(編譯後自動生成)

工程下開啟.pro檔案,右鍵選擇

「新增庫」

選擇「外部庫」、「動態庫」

工程新增標頭檔案xx.h

標頭檔案包含「xx.h"

二、顯式呼叫

1、建立工程

2、can.lib資料夾裡有幾個檔案如下。kerneldlls中有很多xx.dll檔案,如下。將使用的can型別對應的.dll檔案複製(我使用的是usbcan,所以是usbcan.dll),放到工作目錄下,同時複製controlcan.h到工作目錄下。

3、包含controlcan.h到工程中

4、使用qlibrary載入動態庫

qlibrary lib("usbcan.dll");

if(true==lib.load())

qdebug()<

5、宣告與usbcan.dll裡的函式功能相對應的函式,在標頭檔案中新增:

public:

typedef dword(__stdcall vci_opendevice)(dword,dword,dword);

typedef dword(__stdcall vci_resetcan)(dword devicetype,dword deviceind,dword canind);

typedef dword(__stdcall vci_closedevice)(dword devicetype,dword deviceind);

typedef dword(__stdcall vci_initcan)(dword devicetype, dword deviceind, dword canind, pvci_init_config pinitconfig);

typedef dword(__stdcall vci_startcan)(dword devicetype,dword deviceind,dword canind);

typedef ulong(__stdcall vci_transmit)(dword devicetype,dword deviceind,dword canind,pvci_can_obj psend,ulong len);

typedef ulong(__stdcall vci_receive)(dword devicetype,dword deviceind,dword canind,pvci_can_obj preceive,ulong len,int waittime/*=-1*/);

typedef ulong(__stdcall vci_getreceivenum)(dword devicetype,dword deviceind,dword canind);

typedef dword(__stdcall vci_clearbuffer)(dword devicetype,dword deviceind,dword canind);

typedef dword(__stdcall vci_readerrinfo)(dword devicetype,dword deviceind,dword canind,pvci_err_info perrinfo);

typedef dword(__stdcall vci_readcanstatus)(dword devicetype,dword deviceind,dword canind,pvci_can_status pcanstatus);

typedef dword(__stdcall vci_setreference)(dword devicetype,dword deviceind,dword canind,dword reftype,pvoid pdata);

typedef dword(__stdcall vci_readboardinfo)(dword devicetype,dword deviceind,pvci_board_info pinfo);

6、宣告函式物件,如

vci_opendevice *popendevice;

vci_resetcan *presetcan;

vci_closedevice *pclosedevice;

vci_initcan *pinitcan;

vci_startcan *pstartcan;

vci_transmit *ptransmitcan;

vci_receive *preceive;

vci_getreceivenum *pgetreceivenum;

vci_clearbuffer *pclearbuffer;

vci_readerrinfo *preaderrinfocan;

vci_readcanstatus *preadcanstatus;

vci_setreference *psetreference;

vci_readboardinfo *preadboardinfo;

7、將usbcan.dll中的函式功能分解到自定義對應的函式中,原始檔中:

popendevice = (vci_opendevice *)lib.resolve("vci_opendevice");

pclosedevice = (vci_closedevice *)lib.resolve("vci_closedevice");

pinitcan = (vci_initcan *)lib.resolve("vci_initcan");

pstartcan = (vci_startcan *)lib.resolve("vci_startcan");

ptransmitcan = (vci_transmit *)lib.resolve("vci_transmit");

preceive = (vci_receive *)lib.resolve("vci_receive");

pgetreceivenum = (vci_getreceivenum *)lib.resolve("vci_getreceivenum");

pclearbuffer = (vci_clearbuffer *)lib.resolve("vci_clearbuffer");

preaderrinfocan = (vci_readerrinfo*)lib.resolve("vci_readerrinfo");

presetcan=(vci_resetcan *)lib.resolve("vci_resetcan");

psetreference=(vci_setreference *)lib.resolve("vci_setreference");

preadboardinfo=(vci_readboardinfo *)lib.resolve("vci_readboardinfo");

preadcanstatus=(vci_readcanstatus *)lib.resolve("vci_readcanstatus");

Qt 呼叫第三方庫

一 製作動態庫c 動態庫 c 生成dll,專案名稱mydll 件 ifndef mdll h define mdll h ifdef mylibdll define mylibdll extern c declspec dllimport else define mylibdll extern c ...

用qt呼叫第三方庫

宣告 事先我已經自己動手寫了乙個簡單的dll檔案 mydll.dll c版介面的。並且用我前兩篇有關dll文章裡面的方法,從dll中匯出了導入庫 lib 檔案,dll中有兩個函式,原型如下 void helloworld 函式內部呼叫win32 api,功能是彈出乙個helloworld提示框 in...

用qt呼叫第三方庫

宣告 事先我已經自己動手寫了乙個簡單的dll檔案 mydll.dll c版介面的。並且用我前兩篇有關dll文章裡面的方法,從dll中匯出了導入庫 lib 檔案,dll中有兩個函式,原型如下 void helloworld 函式內部呼叫win32 api,功能是彈出乙個helloworld提示框 in...