基于C/C++的Chinese中文编码dll封装使用

2023-12-15 19:39:01

Chinese.c

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

typedef  char*(*Format_gbk)(const char*);
typedef  char*(*Format_utf8)(const char*);


//定义API函数变量
HMODULE chineselDll;
int chinese_load = UNZ_ERRNO;
static Format_gbk	_Format_gbk;
static Format_utf8	_Format_utf8;


int chinese_init() {
	char path[] = "D:\\D-Pro\\Summary\\libcurl\\chinese\\Debug\\chinese.dll";
	if (chinese_load == UNZ_OK)
		return UNZ_OK;
	chineselDll = LoadLibrary(path);
	if (chineselDll != NULL) {
		_Format_gbk = (Format_gbk)GetProcAddress(chineselDll, "format_gbk");
		_Format_utf8 = (Format_gbk)GetProcAddress(chineselDll, "format_utf8");
		chinese_load = UNZ_OK;
		return UNZ_OK;
	}
	return UNZ_ERRNO;
}
char* format_gbk(const char *text) {
	chinese_init();
	return _Format_gbk(text);
}
char* format_utf8(const char* text) {
	chinese_init();
	return _Format_utf8(text);
}

chinese.h

#pragma once
#ifndef	__CHINESE_H__
#define __CHINESE_H__
#ifdef __cplusplus
extern "C" {
#endif
char* format_gbk(const char* text);
char* format_utf8(const char* text);
#ifdef __cplusplus
}
#endif

#endif	/* __CHINESE_H__ */

应用示例:

?printf("\nresult:%s", result);

?printf("\nresult:%s", format_gbk(result));

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