基于C/C++的libcurl多协议文件传输库dll二次封装开发使用

2023-12-15 19:39:42

libcurl 可能是最便携、最强大和最常用的 这个星球上的网络传输库。官方提供的示例,需要在项目中引用到libcurl-imp.lib才能使用。

这里我改造了下工程,将常用的接口导出到了libcurl.dll中方便直接在后续的工程代码中应用,下面可以看到dll常用的使用接口:

libcurl.dll中接口的导出: libcurl.c

#include<stdio.h>
#include "curl/curl.h"
#include "libcurl.h"
#define UNZ_OK                          (0)
#define Z_ERRNO							(-1)
#define UNZ_ERRNO                       (Z_ERRNO)

typedef  CURLcode(*Curl_global_init)(long);
typedef  struct Curl_easy* (*Curl_easy_init)(void);
//typedef  CURLcode(*Curl_easy_setopt)(CURL*, CURLoption, ...);
typedef  CURLcode(*Curl_easy_perform)(CURL*);
typedef  void (*Curl_easy_cleanup)(CURL*);
typedef  void (*Curl_global_cleanup)(void);

//定义API函数变量
HMODULE curlDll;
int curl_load = UNZ_ERRNO;
static Curl_global_init	_curl_global_init;
static Curl_easy_init _curl_easy_init;
//static Curl_easy_setopt _curl_easy_setopt;
static Curl_easy_perform _curl_easy_perform;
static Curl_easy_cleanup _curl_easy_cleanup;
static Curl_global_cleanup _curl_global_cleanup;

int libcurl_init() {
	char path[] = "D:\\D-Pro\\Summary\\libcurl\\Debug\\libcurl.dll";
	if (curl_load == UNZ_OK)
		return UNZ_OK;
	curlDll = LoadLibrary(path);
	if (curlDll != NULL) {
		_curl_global_init = (Curl_global_init)GetProcAddress(curlDll, "curl_global_init");
		_curl_easy_init = (Curl_easy_init)GetProcAddress(curlDll, "curl_easy_init");
		_curl_easy_setopt = (Curl_easy_setopt)GetProcAddress(curlDll, "curl_easy_setopt");
		_curl_easy_perform = (Curl_easy_perform)GetProcAddress(curlDll, "curl_easy_perform");
		_curl_easy_cleanup = (Curl_easy_cleanup)GetProcAddress(curlDll, "curl_easy_cleanup");
		_curl_global_cleanup = (Curl_global_cleanup)GetProcAddress(curlDll, "curl_global_cleanup");
		curl_load = UNZ_OK;
		return UNZ_OK;
	}
	return UNZ_ERRNO;
}
CURLcode libcurl_global_init(long flags)
{
	if (curl_load == UNZ_OK)
		return _curl_global_init(flags);
}
CURLcode libcurl_easy_perform(CURL* curl) {
	return _curl_easy_perform(curl);
}
void libcurl_easy_cleanup(CURL* curl) {
	_curl_easy_cleanup(curl);
}
void libcurl_global_cleanup(void) {
	_curl_global_cleanup();
}
struct Curl_easy* libcurl_easy_init()
{
	return _curl_easy_init();
}
//CURLcode libcurl_easy_setopt(struct Curl_easy* data, CURLoption tag, ...)
//{
//	va_list arg;
//	CURLcode result;
//
//	if (!data)
//		return CURLE_BAD_FUNCTION_ARGUMENT;
//
//	va_start(arg, tag);
//
//	result = _curl_easy_setopt(data, tag, arg);
//
//	va_end(arg);
//	return result;
//}

相关操作接口的声明:libcurl.h

#pragma once
#include <curl/curl.h>
#ifndef	__LIBCURL_H__
#define __LIBCURL_H__
#ifdef __cplusplus
extern "C" {
#endif
	typedef  CURLcode(*Curl_easy_setopt)(CURL*, CURLoption, ...);
	Curl_easy_setopt _curl_easy_setopt;
	int libcurl_init();
	void libcurl_easy_cleanup(CURL* curl);
	void libcurl_global_cleanup(void);
	struct Curl_easy* libcurl_easy_init();
	CURLcode libcurl_global_init(long flags);
	CURLcode libcurl_easy_perform(CURL* curl);
	//CURLcode libcurl_easy_setopt(struct Curl_easy* data, CURLoption tag, ...);
#define  libcurl_easy_setopt _curl_easy_setopt
#ifdef __cplusplus
}
#endif

#endif	/* __LIBCURL_H__ */

?http访问测试应用示例: test.c

#include <stdio.h>
#include "curl/curl.h"
#include "libcurl.h"
#include "gb2312.h"
unsigned int read_callback(void* ptr, size_t size, size_t nmemb, void* userp)
{
	char* pTmp = (char*)userp;

	unsigned long sizes = size * nmemb;
	if (!ptr)
	{
		return 0;
	}
	strncpy(pTmp, (char*)ptr, sizes);
	return sizes;


}
int Post(char* szUrl, char* szParameter, char* szResult, char* szErrorMessage)
{
	CURL* curl = NULL;
	CURLcode res;
	struct curl_slist* headers = NULL;

	res = libcurl_global_init(CURL_GLOBAL_ALL);
	if (res != CURLE_OK)
	{
		szErrorMessage = "下载过程初始化错误!";
		return 0;
	}
	curl = libcurl_easy_init();
	if (curl)
	{
		libcurl_easy_setopt(curl, CURLOPT_URL, szUrl);
		libcurl_easy_setopt(curl, CURLOPT_POST, 1L);
		libcurl_easy_setopt(curl, CURLOPT_POSTFIELDS, szParameter);
		libcurl_easy_setopt(curl, CURLOPT_ENCODING, "");
		libcurl_easy_setopt(curl, CURLOPT_WRITEDATA, szResult);
		libcurl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, read_callback);
		libcurl_easy_setopt(curl, CURLOPT_TIMEOUT, 4); 
		res = libcurl_easy_perform(curl);
		if (res != CURLE_OK)
		{
			szErrorMessage = "通讯失败, 请检查线路!";
			return 0;
		}
		libcurl_easy_cleanup(curl);
	}
	libcurl_global_cleanup();
	return 1;
}
int main() {
	char result[1000];
	char msg[1000];
	memset(result, 0, 1000);
	libcurl_init();
	Post("http://XXXXXXXXXXXX", "", result, msg);
	printf("\nresult:%s", result);
	return 0;
}

http访问运行结果:?

?

跟网页访问的结果是一样的。?这里我发现有个乱码问题。利用前面提到的转编码问题即可解决。

?

文章来源:https://blog.csdn.net/github_37687123/article/details/135021761
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。