C語言 動態庫簡單開發

2021-09-08 15:00:35 字數 3491 閱讀 1212

動態庫專案

//

簡單的動態庫開發----報文傳送

#define _crt_secure_no_warnings#include

#include

#include

//定義上下文結構體

typedef struct

_sck_handlesck_handle;

//初始化上下文

_declspec(dllexport)

int cltsocketinit(void **handle/*

out*/

)

//定義上下文指標

sck_handle *shandle =null;

//分配記憶體

//詳述:此處分配記憶體必須分配堆記憶體(malloc函式分配),這也正是malloc函式真正的用途所在

//此處不可以分配棧記憶體,棧記憶體會被系統自動**,但是報文的傳送與接受所使用的上下文sck_handle,必須長時間存在

//何時**必須由使用者決定,而不能隨便的被**

//同樣使用靜態區也不合適,因為無法人為**記憶體空間,必須等待電腦關機,綜上所述,只能使用malloc函式分配記憶體

shandle = (sck_handle *)malloc(sizeof

(sck_handle));

//重置記憶體空間

memset(shandle, 0, sizeof

(sck_handle));

strcpy(shandle->ipaddress, "

192.168.0.128");

strcpy(shandle->port, "88"

); *handle =shandle;

return

erro_msg;}//

客戶端發報文

_declspec(dllexport)

int cltsocketsend(void *handle/*

in*/, unsigned char *buf/*

in*/, int buflen/*

in*/

)

if (buf ==null)

sck_handle *sh =null;

sh = (sck_handle *)handle;

//為報文字元開闢記憶體空間

sh->buf = (char *)malloc(sizeof(char)*buflen);

if (sh->buf ==null)

//給上下文中的報文字元賦值

//memcpy()函式詳解

//函式原型

//void *memcpy(void *dest, const void *src, size_t n);

//功能

//從源src所指的記憶體位址的起始位置開始拷貝n個位元組到目標dest所指的記憶體位址的起始位置中

//所需標頭檔案

//c語言:#include

//c++:#include

//返回值

//函式返回指向dest的指標。

//說明

和destin所指的記憶體區域可能重疊,但是如果source和destin所指的記憶體區域重疊, 那麼這個函式並不能夠確保source所在重疊區域在拷貝之前不被覆蓋。而使用memmove可以用來處理重疊區域。函式返回指向destin的指標.

//2.如果目標陣列destin本身已有資料,執行memcpy()後,將覆蓋原有資料(最多覆蓋n)。如果要追加資料,則每次執行memcpy後,要將目標陣列位址增加到你要追加資料的位址。

//注意:source和destin都不一定是陣列,任意的可讀寫的空間均可。

memcpy(sh->buf, buf, buflen);

sh->buflen =buflen;

return

erro_msg;}//

客戶端收報文

_declspec(dllexport)

int cltsocketrev(void *handle/*

in*/, unsigned char **buf/*

out*/, int *buflen/*

out*/

)

//定義臨時上下文變數

sck_handle *sh =null;

sh = (sck_handle *)handle;

//分配返回報文字串記憶體

char *cbuf = (char *)malloc(sizeof(char)*sh->buflen);

memcpy(cbuf, sh->buf, sh->buflen);

*buf =cbuf;

*buflen = sh->buflen;

//釋放上下文中字串陣列的記憶體空間(至於具體應用還是看場景)

if (sh->buf !=null)

sh->buflen = 0

;

return

erro_msg;}//

客戶端釋放資源

_declspec(dllexport)

int cltsocketdestory(void **handle)

//轉換型別

sck_handle *sh =null;

sh = (sck_handle *)(*handle);

//判斷字串陣列是否釋放--嚴謹做法

if (sh->buf!=null)

if (sh !=null)

return

erro_msg;

}

測試專案

#define _crt_secure_no_warnings#include 

#include

#include

#include

"socketclientdll.h

"void

main()

//傳送報文

unsigned char *str = "

1234567890qwertyuiop";

int buflen = 10

; ret =cltsocketsend(handle, str, buflen);

if (ret == 0

)

unsigned

char *str2 =null;

int buflen2 = 0

;

//接受報文

ret = cltsocketrev(handle, &str2, &buflen2);

if (ret == 0

)

//釋放上下文

cltsocketdestory(&handle);

printf(

"%p\n

", handle);

system(

"pause");

}

效果圖

簡單開發流程

回顧以前的開發歷程,偶得出乙個屬於自己的經典經驗,那就是,開發流程根據專案規模和團隊成員情況而定 ok,不相信,我舉乙個例子,現在要寫乙個helloworld,請問您會選擇rup還是agile?很簡單,答案不言而喻,那就是直接寫了 下面是我的一些總結,應該有利於新手打破在學校裡面所學的軟體工程的障礙...

webapp 的簡單開發

前端 mui view層 control層 後端 php model層 control層 mysql 後端忽略 搭建環境使用hbuilder ide 配套使用mui,裡面可以mui建立模板,支援mui語法提示 ps 也可以使用不同的移動框架,如 sui frozen ui ionic。看看專案目錄結...

從簡單開始學習C語言 5

掌握了c語言中的基本知識,我們就需要將知識串聯起來,今天就學習兩種簡單的語句if語句和while語句,也只是以了解為主,知道什麼是選擇語句?什麼是迴圈語句?選擇語句 首先,我們來梳理乙個含有選擇的邏輯,假設乙個場景。假設,如果我好好學習程式設計技術,畢業時就可以找乙個好工作,拿到offer 如果我不...