MFC中使用自定义资源 隐藏文件 把文件集成到软件中
目标:把证书文件隐藏封装到软件中,软件初始化时复制到C:UsersFT.android下,退出时删除。
实现:如下
1.先加入资源
2.代码
//把资源文件写入到指定路径 //id 资源id 比如IDR_ADBKEY1 //type 导入资源时的命名,比如adbkey //desPath 目标路径,包含文件名。 C:UsersFT.androidadbkey int writeBin(int id,CString type,CString desPath) { HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(id), type); if (hRes == NULL) { printf(__FUNCTION__"Error:%d", GetLastError()); return -2; } DWORD len = SizeofResource(NULL, hRes); HGLOBAL hg = LoadResource(NULL, hRes);//载入资源 if (hg == NULL) { printf(__FUNCTION__"Error:%d", GetLastError()); return -3; } LPVOID lp = (LPSTR)LockResource(hg);//锁定资源 if (lp == NULL) { printf(__FUNCTION__"Error:%d", GetLastError()); return -4; } //TCHAR path[512]; //::GetTempPathW(512, path); //CString temp; //temp.Format(_T("%s//a.bin"), path); //写入到系统临时文件夹 //Upgrade_Path = temp; HANDLE hFile1 = CreateFile(desPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); DWORD dwWrite = 0; if (WriteFile(hFile1, lp, len, &dwWrite, NULL)==FALSE) { printf(__FUNCTION__"Error:%d", GetLastError()); } CloseHandle(hFile1); FreeResource(hg); return 0; }