我编写了一个程序,需要将一些信息输出到文件,当我手动运行它时,它可以正常工作,但在启动时运行时,它无法写入文件。
//set run at startup
HMODULE GetModH = GetModuleHandle(NULL);
WCHAR lPath[MAX_PATH];
HKEY hKey;
memset(lPath, 0, sizeof(lPath));
GetModuleFileName(GetModH, lPath, sizeof(lPath));
RegOpenKeyEx(HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
0, KEY_SET_VALUE, &hKey);
RegSetValueEx(hKey, L"testProgram", 0,
REG_SZ, (const unsigned char*)lPath, sizeof(lPath));
RegCloseKey(hKey);
//write to file
FILE* fp;
const char* logName = "output.txt";
fp = fopen(logName, "a+");
CString temp;
...
_ftprintf(fp, temp);
fclose(fp);