Write DLL code

New construction

New empty project project

 

Click OK. Then right-click the project and select New item.

 

Select New again, select the C++ file, change it to MySocketClient.c, and select Add.

At this point, the new construction is completed.

Then right-click the project and select Properties to change the configuration type to dynamic library (.dll)

Write the header file mysocketClient.h

#ifndef _INC_MYSOCKETCLIENT_H_

#define _INC_MYSOCKETCLIENT_H_

#define Import_SSS

#ifdef Import_SSS

#define API _declspec(dllexport)

#else

#define API _declspec(dllimport)

#endif

 

#ifdef _cplusplus//extern”C” tells the compiler: this is a library file written in C, please link them in C style.

extern “C” {

#endif // _cplusplus

API// Export functions for external calls.

    int socketClient_Init(void **handle);

    API

    int socketClient_Send(void *handle, unsigned char *buf, int buflen);

    API

    int socketClient_Recv(void *handle, unsigned char *buf, int *buflen);

    API

    int socketClient_Destory(void *handle);

#ifdef _cplusplus

}

#endif // _cplusplus

#endif //_INC_MYSOCKETCLIENT_H_

Write MySocketClient. C

#define _CRT_SECURE_NO_WARNINGS

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include “MySocketClient.h”

typedef struct _Sck_Handle

{

    char version[16];

    char ip[16];

    int port;

    unsigned char *p;

    int len;

}Sck_Handle; // Define the structure of Handle.

int socketClient_Init(void **handle)

{

    int ret = 0;

    Sck_Handle *tmpHandle = NULL;

    if (handle == NULL)

    {

         ret = -1;

         printf(“[socketClient_Init] err %d handle=NULL \n”, ret);

         return ret;

    }

    tmpHandle = (Sck_Handle *)malloc(sizeof(Sck_Handle));

    if (tmpHandle == NULL)

    {

         ret = -2;

         printf(“[socketClient_Init] err:%d malloc err \n”, ret);

    }

memset(tmpHandle, 0, sizeof(Sck_Handle)); // Initializes the structure.

Strcpy (tmpHandle – > version, “1.0.0.1”);

Strcpy (tmpHandle – > IP, “192.168.12.121”);

    tmpHandle->port = 11111;

    *handle = tmpHandle;

    return ret;

}

Sending Socket packets

//__declspec(dllexport)

int socketClient_Send(void *handle, unsigned char *buf, int buflen)

{

    int          ret = 0;

    Sck_Handle   *tmpHandle = NULL;

    if (handle == NULL || buf == NULL || buflen <= 0)

    {

         ret = -2;

         printf(“func socketclient_send() err :%d  (handle == NULL ||  buf==NULL || buflen <=0 ) \n”, ret);

         return ret;

    }

    tmpHandle = (Sck_Handle *)handle;

    tmpHandle->len = buflen;

    tmpHandle->p = (unsigned char *)malloc(buflen);

    if (tmpHandle->p == NULL)

    {

         ret = -2;

         printf(“func socketclient_send() err :%d  malloc len:%d \n”, ret, buflen);

         return ret;

    }

memcpy(tmpHandle->p, buf, buflen); // Cache data to memory

Printf (” Received send data is %s \n”, tmpHandle->p);

    return ret;

}

Socket Packet Acceptance

//__declspec(dllexport)

int socketClient_Recv(void *handle, unsigned char *buf, int *buflen)

{

    int          ret = 0;

    Sck_Handle   *tmpHandle = NULL;

    if (handle == NULL || buf == NULL || buflen == NULL)

    {

         ret = -2;

         printf(“func socketclient_recv() err :%d  (handle == NULL ||  buf==NULL || buflen==NULL ) \n”, ret);

         return ret;

    }

    tmpHandle = (Sck_Handle *)handle;

    memcpy(buf, tmpHandle->p, tmpHandle->len);

*buflen = tmpHandle->len; // Indirect assignment tells the caller the length of the received data

Printf (” Data length is %d \n”, tmpHandle->len);

    return ret;

}

Socket environment Release

//__declspec(dllexport)

int socketClient_Destory(void *handle)

{

    int          ret = 0;

    Sck_Handle   *tmpHandle = NULL;

    if (handle == NULL)

    {

         return -1;

    }

    tmpHandle = (Sck_Handle *)handle;

if (tmpHandle->p ! = NULL)

    {

free(tmpHandle->p); // Free the memory space pointed to by the pointer to the structure member field

    }

free(tmpHandle); // Release the structure inside the body

    return 0;

}

Then right-click to compile the project. You can see the generated DLL in the Debug folder

Call the DLL

Create a C++ console application

After the creation, place the compiled DLL files, lib files, and H files under the project.

Right-click the project properties and change the character set to use a multi-byte character set.

Here is the code to invoke the DLL.

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <windows.h>

#include <iostream>

using namespace std;

// define a pointer function

typedef int(*SOCLETCLIENT_INIT)(void **handle);

typedef int(*SOCKETCLIENT_SEND)(void *handle, unsigned char *buf, int buflen);

typedef int(*SOCKETCLIENT_RECV)(void *handle, unsigned char *buf, int *buflen);

typedef int(*SOCKETCLIENT_DESTORY)(void *handle);

int main()

{

    HINSTANCE hInstance;

    hInstance = LoadLibrary(“MySocketClient.dll”);

    SOCLETCLIENT_INIT socketClient_Init= (SOCLETCLIENT_INIT)GetProcAddress(hInstance, “socketClient_Init”);

    SOCKETCLIENT_SEND socketClient_Send=(SOCKETCLIENT_SEND)GetProcAddress(hInstance, “socketClient_Send”);;

    SOCKETCLIENT_RECV socketClient_Recv= (SOCKETCLIENT_RECV)GetProcAddress(hInstance, “socketClient_Recv”);;

    SOCKETCLIENT_DESTORY socketClient_Destory= (SOCKETCLIENT_DESTORY)GetProcAddress(hInstance, “socketClient_Destory”);;

    unsigned char buf[1024];

    int buflen;

    unsigned char out[1024];

    int outlen;

    void *handle = NULL;

    int ret = 0;

    strcpy((char *)buf, “aaaaAAAAAFFffffffdddddddd”);

    buflen = 9;

// The client initializes and retrieves the upper and lower handle

    ret = socketClient_Init(&handle /*out*/);

if (ret ! = 0)

    {

         printf(“func socketclient_init() err:%d \n “, ret);

         goto End;

    }

// The client sends packets

    ret = socketClient_Send(handle /*in*/, buf /*in*/, buflen /*in*/);

if (ret ! = 0)

    {

         printf(“func socketclient_send() err:%d \n “, ret);

         goto End;

    }

// The client receives the message

    ret = socketClient_Recv(handle /*in*/, out /*in*/, &outlen/*in out*/);

if (ret ! = 0)

    {

         printf(“func socketclient_recv() err:%d \n “, ret);

         goto End;

    }

Printf (” Received data length is %d \n”,outlen);

End:

// The client releases resources

    ret = socketClient_Destory(handle/*in*/);

if (hInstance ! = NULL)

    {

         FreeLibrary(hInstance);

         hInstance = NULL;

    }

printf(“hello… \n”);

    system(“pause”);

    return 0;

}

Code links: download.csdn.net/download/hh…