说创建不大准确,毕竟我写这个的目的是为了做个炉石传说拔线工具,实际实现的内容是将现有的快捷方式用程序勾上用管理员方式运行。
后面我会把工具源码、已经编译好的文件发出来。
想要学会一种fopen()打开包含中文的路径的方法的话可以仔细看看源码,这里我就不分离成案例了。
#include <iostream>
#include <stringapiset.h>
using namespace std;
typedef unsigned char byte;
bool hashSuffix(string str, string &suffix) { return str.size() >= suffix.size() && !str.compare(str.size() - suffix.size(), str.size(), suffix); }
wchar_t *toWChar(char *str, UINT codePage);
int lnkUpLevel(char *fileName);
static string fileSuffix = ".lnk";
int main(int argc, char** argv) {
for (int i = 1; i < argc; i++) lnkUpLevel(argv[i]);
return 0;
}
int lnkUpLevel(char *fileName) {
if (!hashSuffix(string(fileName), fileSuffix)) return 0;
FILE *file = fopen(fileName, "r+");
if (file == NULL) {
file = _wfopen(toWChar(fileName, CP_UTF8), L"r+");
if (file == NULL) return 0;
}
fseek(file, 0x15, SEEK_SET);
byte bit = 0x20;
fwrite(&bit, 1, 1, file);
fclose(file);
}
wchar_t *toWChar(char *str, UINT codePage) {
int len = MultiByteToWideChar(codePage, 0, str, strlen(str), NULL, 0);
wchar_t *w_char = new wchar_t[len + 1];
MultiByteToWideChar(codePage ,0, str, strlen(str), w_char, len);
w_char[len] = '\0';
return w_char;
}