Dynamic Library Project
//Simple Dynamic Library development----message sending#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<string.h>//defining the context structure bodytypedefstruct_sck_handle{//Define message IP Charipaddress[ -]; //define the relaying port of the newspaper Charport[Ten]; //defining message Acceptance ArraysUnsignedChar*buf; //Define Message length intBuflen;} Sck_handle;//Initializing Context_declspec (dllexport)intCltsocketinit (void**handle/* out*/){ intErro_msg =0; if(handle==NULL) {erro_msg=1; printf ("handle==null Message initialization failed Erro msg:%d\n", erro_msg); returnerro_msg; } //Defining context PointersSck_handle *shandle =NULL; //Allocating Memory//in detail: allocating memory here must allocate heap memory (malloc function Assignment), which is exactly what the malloc function is really used for.//The stack memory can not be allocated here, the stack memory will be automatically reclaimed by the system, but the sending and receiving of the message is used in the context Sck_handle, must exist for a long time//when the recycling must be decided by the user, but not casually be recycled//It is also inappropriate to use static zones because the memory space cannot be reclaimed and must wait for the computer to shut down, in summary, only use the malloc function to allocate memoryShandle = (Sck_handle *)malloc(sizeof(Sck_handle)); //Resetting memory Spacememset (Shandle,0,sizeof(Sck_handle)); strcpy (Shandle->ipaddress,"192.168.0.128"); strcpy (Shandle->port," the"); *handle =Shandle; returnerro_msg;}//Client Sender Text_declspec (dllexport)intCltsocketsend (void*handle/*inch*/, unsignedChar*buf/*inch*/,intBuflen/*inch*/){ intErro_msg =0; if(handle==NULL) {erro_msg=1; printf ("Handle==null handle cannot be considered NULL erro msg:%d\n", erro_msg); returnerro_msg; } if(BUF = =NULL) {erro_msg=2; printf ("Buf==null buf cannot be considered NULL erro msg:%d\n", erro_msg); returnerro_msg; } sck_handle*sh =NULL; SH= (Sck_handle *) handle; //open up memory space for newspaper charactersSh->buf = (Char*)malloc(sizeof(Char)*Buflen); if(sh->buf==NULL) {erro_msg=3; printf ("Sh->buf==null memory allocation failure erro msg:%d\n", erro_msg); returnerro_msg; } //Assigning a value to a text character in a context//memcpy () function explanation//function Prototypes//void *memcpy (void *dest, const void *SRC, size_t n); //function//copies n bytes from the starting position of the memory address referred to by the source SRC to the beginning of the memory address referred to by the target dest//Required header File//C language: #include <string.h>//C + +: #include <cstring>//return value//The function returns a pointer to Dest. //Description//The memory areas referred to by 1.source and Destin may overlap, but if the memory areas referred to by source and Destin overlap, this function does not ensure that the overlapping area of the source is not overwritten before the copy. The use of memmove can be used to process overlapping areas. The function returns a pointer to Destin. //2. If the target array Destin itself already has data, after executing memcpy (), the original data will be overwritten (up to N). If you want to append data, add the destination array address to the address you want to append the data to after each execution of memcpy. //Note: Both source and Destin are not necessarily arrays, and any readable and writable space is available. memcpy (sh->buf, buf, Buflen); SH->buflen =Buflen; returnerro_msg;}//Client Ticker Text_declspec (dllexport)intCltsocketrev (void*handle/*inch*/, unsignedChar**buf/* out*/,int*buflen/* out*/){ intErro_msg =0; if(handle = = NULL | | buf = = NULL | | buflen==NULL) {erro_msg=1; printf ("handle = = NULL | | buf = = NULL | | buflen==null erro msg:%d\n", erro_msg); returnerro_msg; } //Defining temporary context variablesSck_handle *sh =NULL; SH= (Sck_handle *) handle; //allocating return message string memory Char*cbuf = (Char*)malloc(sizeof(Char) *sh->Buflen); memcpy (cbuf, SH->buf, sh->Buflen); *buf =Cbuf; *buflen = sh->Buflen; //frees the memory space of a string array in context (for specific applications or scenes) if(sh->buf!=NULL) { //Freeing Memory Free(sh->buf); //Eliminate wild handsSh->buf =NULL; } SH->buflen =0; returnerro_msg;}//Client Frees Resources_declspec (dllexport)intCltsocketdestory (void**handle) { intErro_msg =0; if(handle==NULL) {erro_msg=1; printf ("handle==null%d\n", erro_msg); returnerro_msg; } if(*handle!=NULL) { //Freeing Memory Free(*handle); //Eliminate wild hands*handle =NULL; } returnerro_msg;}
Test Project
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<string.h>#include"socketclientdll.h"voidMain () {intRET =0; //Initialize Message void*handle =NULL; RET= Cltsocketinit (&handle); if(ret==0) {printf ("Message Initialization Successful! \ n"); } //Send a delivery paperUnsignedChar*str ="1234567890qwertyuiop"; intBuflen =Ten; RET=cltsocketsend (handle, str, buflen); if(ret = =0) {printf ("message sent successfully! \ n"); } unsignedChar*STR2 =NULL; intBuflen2 =0; //Receiving Messagesret = Cltsocketrev (handle, &STR2, &buflen2); if(ret = =0) {unsignedChar*BUF3 = (Char*)malloc(sizeof(Char)* A); memset (BUF3,0,sizeof(Char)* A); memcpy (BUF3, str2,Ten); //strcpy (BUF3, str2);printf"Message accepted successfully! \ n"); printf ("Receive message:%s; message length is%d\n", BUF3, buflen2); } //Release ContextCltsocketdestory (&handle); printf ("%p\n", handle); System ("Pause");}
Simple development of C language dynamic library