Dev c++ C語言實現(xiàn)第一個 dll 動態(tài)鏈接庫 創(chuàng)建與調(diào)用-CSDN博客
?在寫dll 插件中發(fā)現(xiàn)的函數(shù)指針用途和 typedef 的定義指針的用法-CSDN博客
兩步之后,嘗試加入結構體實現(xiàn)整體數(shù)據(jù)使用。
注意結構體 Ak
是相同的
代碼如下
DLL文件有兩個,dll.dll是上面提到的鏈接里的
dllv2.dll是這個代碼里的
dllv2.cpp文章來源:http://www.zghlxwxcb.cn/news/detail-848602.html
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <iostream>
using namespace std;
void mainDll(){
ak.count++;
ak.number=1;
ak.x=0;
ak.y=0;
cout<<"atking"<<endl;
for(int i=0;i<10;i++){
ak.x++;
}
cout<<"count: "<<ak.count<<endl;
}
// dll.h
#ifndef _DLL_H_
#define _DLL_H_
typedef struct Ak{
int x; // 位置坐標
int y;
int number; // 類型
int time; // 時長上限
int count; // 計時
};
Ak ak;
extern "C"
{
void mainDll();
}
#endif
?主程序代碼如下文章來源地址http://www.zghlxwxcb.cn/news/detail-848602.html
#include <windows.h>
#include <iostream>
typedef struct Ak
{
int x; // 位置坐標
int y;
int number; // 類型
int time; // 時長上限
int count; // 計時
};
using namespace std;
int main()
{
HINSTANCE hDLL = LoadLibrary("dll.dll"); // 填文件名
HINSTANCE hDLLv2 = LoadLibrary("dllv2.dll");
typedef void (*func)(double a, double b, double c[], double* aplusb); // 填調(diào)用的輸入?yún)?shù)
typedef void (*kk)(double a, double b, double c[], double* aplusb) ;
void (*atk)()=(void (*)())GetProcAddress(hDLLv2,"mainDll"); // 強制類型轉(zhuǎn)換為函數(shù)指針,然后成為atk 的替身
typedef void(*ATK)(); // 定義函數(shù)指針類型,類型名為 ATK ,返回值類型是 void, 參數(shù)是 void
ATK ky=(ATK)GetProcAddress(hDLLv2,"mainDll");
func callDll =(func)GetProcAddress(hDLL, "mainDll"); // 填調(diào)用的 dll 函數(shù)名
kk callDllv2= (kk) GetProcAddress(hDLL, "mainDll");
double a = 1, b = 2, c[3] = {4, 5, 6}, result;
callDll(a, b, c, &result);
cout << a << endl;
cout << b << endl;
cout << result << endl;
cout << c[0] << c[1] << c[2] << endl;
cout<<endl;
callDllv2(a, b, c, &result);
cout << a << endl;
cout << b << endl;
cout << result << endl;
cout << c[0] << c[1] << c[2] << endl;
int i=5;
while(1)
{
atk();
if(i>=5)
{
cout<<"數(shù)據(jù)是否共享: ";
ky();
cout<<endl;
i=0;
}
i++;
Sleep(200);
}
}
到了這里,關于以動態(tài)庫鏈接庫 .dll 探索結構體參數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!