最近使用ShellExecute遇到中文路径乱码问题,记录一下。
char complete_path[1024];
GetModuleFileNameA(NULL, complete_path, sizeof(complete_path));
//UTF-8到GB2312的转换,防止中文路径乱码
int len = MultiByteToWideChar(CP_UTF8, 0, filepath, -1, NULL, 0);
wchar_t* wstr = (wchar_t *)malloc(sizeof(wchar_t)*(len + 1));
memset(wstr, 0, sizeof(wchar_t)*(len + 1));
MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char *path1 = (char *)malloc(sizeof(char)*(len + 1));
memset(path1, 0, sizeof(char)*(len + 1));
WideCharToMultiByte(CP_ACP, 0, wstr, -1, path1, len, NULL, NULL);
free(wstr);
//开启进程,complete_path是要启动的exe全路径,path1是cmd参数,比如某个要用exe打开的文件的路径
ShellExecute(NULL, "open", complete_path, path1, NULL, 1);
free(path1);