c++ 资源文件操作
2007-12-10 01:18
519 查看
CtstApp::CreateFileFromResource(LPCTSTR lpszResourceName, LPCTSTR lpszType, LPCTSTR lpszTargetName) { ASSERT(lpszResourceName != NULL); if(lpszTargetName==_T(""))return false; // determine location of the binary resource in resource fork HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, lpszType); HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, lpszType); // If failed, try instance handle if (hRsrc == 0) hRsrc = ::FindResource(hInst = AfxGetInstanceHandle(), lpszResourceName, lpszType); if (hRsrc == NULL) return false; HGLOBAL hData = LoadResource(hInst, hRsrc); if (hData == NULL) return false; DWORD dwSize = SizeofResource(hInst, hRsrc); LPBYTE pData = (LPBYTE) LockResource(hData); CFile m_File; CFileException e; if( !m_File.Open( lpszTargetName, CFile::modeCreate | CFile::modeWrite, &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif UnlockResource(hData); FreeResource(hRsrc); return false; } else { m_File.Write(pData,dwSize); m_File.Close(); } UnlockResource(hData); FreeResource(hRsrc); return true; } 参数说明: lpszResourceName 资源名称, 7&X[垠wY:业vI^网#国S业*Y (Xt理\]|w络T%K,;7rAvnnFY理垠OT使用字符串的方式标定ID值,如” config.xml” lpszType 资源类型,如”ICON”,”Jpg”,”XML” lpszTargetName 目标文件名称,如”AppConfig.xml” 调用示例: CreateFileFromResource(_T("config.xml"),_T("xml"),_T("AppConfig.xml")); 有时我们会在资源里加上一些文件,BMP、ICON、WAVE等文件读取时没什么问题, 主要的区别在于RT_TOOLBAR,RT_MENU,RT_... HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_TOOLBAR);
HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR);
if (hRsrc == NULL)
return FALSE; HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
if (hGlobal == NULL)
return FALSE; CToolBarData* pData = (CToolBarData*)LockResource(hGlobal); 而对于其它文件呢 进行加入普通的资源一样,然后用我下面的函数就可以了:
//
// 从资源中读取数据
//
LONG GetDataFromRes(PBYTE pData,UINT nIDResource)
{
// 将资源ID转成资源名称
LPSTR szResourceName = MAKEINTRESOURCE(LOWORD(nIDResource));
// 资源的类型
LPSTR szResourceType = "BIN_DATA";
// 查找资源句柄
HRSRC hRes = FindResource(NULL, szResourceName,szResourceType);
// 获得资源数据的句柄
HGLOBAL hResData;
if (!hRes || !(hResData = LoadResource(NULL,hRes)))
{
return -1;
};
// 资源数据的大小
DWORD dwSize = SizeofResource(NULL,hRes);
if(pData == NULL)
{
return dwSize;
}
// 资源加锁
PBYTE pSrc = (PBYTE)LockResource(hResData);
if (!pSrc)
{
FreeResource(hResData);
return -1;
};
// 复制数据
CopyMemory(pData,pSrc,dwSize);
// 释放资源
FreeResource(hResData);
return dwSize;
}
//
// DUMP生成文件
//
BOOL MakeFileFromData(PBYTE pData,LONG lSize,LPCSTR lpszFilePath)
{
if(pData == NULL || lpszFilePath == NULL || lpszFilePath[0] == '\0')
return FALSE;
HANDLE hFile = CreateFile(
lpszFilePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN,
NULL
);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
DWORD dwWrited = 0;
BOOL bRet = WriteFile(hFile,pData,lSize,&dwWrited,NULL);
CloseHandle(hFile);
return bRet;
}
我想起来了,改语言设置不行,必须到相语言的操作系统才能看到
我曾经做过一个双语言的程序,就是装了一个英文的系统才能看到效果
你看看MSDN上的这个例子吧,可以枚举到的. C/C++ code
HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_TOOLBAR);
if (hRsrc == NULL)
return FALSE; HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
if (hGlobal == NULL)
return FALSE; CToolBarData* pData = (CToolBarData*)LockResource(hGlobal); 而对于其它文件呢 进行加入普通的资源一样,然后用我下面的函数就可以了:
//
// 从资源中读取数据
//
LONG GetDataFromRes(PBYTE pData,UINT nIDResource)
{
// 将资源ID转成资源名称
LPSTR szResourceName = MAKEINTRESOURCE(LOWORD(nIDResource));
// 资源的类型
LPSTR szResourceType = "BIN_DATA";
// 查找资源句柄
HRSRC hRes = FindResource(NULL, szResourceName,szResourceType);
// 获得资源数据的句柄
HGLOBAL hResData;
if (!hRes || !(hResData = LoadResource(NULL,hRes)))
{
return -1;
};
// 资源数据的大小
DWORD dwSize = SizeofResource(NULL,hRes);
if(pData == NULL)
{
return dwSize;
}
// 资源加锁
PBYTE pSrc = (PBYTE)LockResource(hResData);
if (!pSrc)
{
FreeResource(hResData);
return -1;
};
// 复制数据
CopyMemory(pData,pSrc,dwSize);
// 释放资源
FreeResource(hResData);
return dwSize;
}
//
// DUMP生成文件
//
BOOL MakeFileFromData(PBYTE pData,LONG lSize,LPCSTR lpszFilePath)
{
if(pData == NULL || lpszFilePath == NULL || lpszFilePath[0] == '\0')
return FALSE;
HANDLE hFile = CreateFile(
lpszFilePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN,
NULL
);
if(hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
DWORD dwWrited = 0;
BOOL bRet = WriteFile(hFile,pData,lSize,&dwWrited,NULL);
CloseHandle(hFile);
return bRet;
}
我想起来了,改语言设置不行,必须到相语言的操作系统才能看到
我曾经做过一个双语言的程序,就是装了一个英文的系统才能看到效果
你看看MSDN上的这个例子吧,可以枚举到的. C/C++ code
char szBuffer[80]; // print buffer for EnumResourceTypes DWORD cbWritten; // number of bytes written to res. info. file size_t cbString; // length of string in sprintf HRESULT hResult; // Declare callback functions. BOOL EnumTypesFunc( HANDLE hModule, LPTSTR lpType, LONG lParam); BOOL EnumNamesFunc( HANDLE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam); BOOL EnumLangsFunc( HANDLE hModule, LPCTSTR lpType, LPCTSTR lpName, WORD wLang, LONG lParam); // Load the .EXE whose resources you want to list. hExe = LoadLibrary("hand.exe"); if (hExe == NULL) { // Add code to fail as securely as possible. return; } // Create a file to contain the resource info. hFile = CreateFile("resinfo.txt", // name of file GENERIC_READ | GENERIC_WRITE, // access mode 0, // share mode (LPSECURITY_ATTRIBUTES) NULL, // default security CREATE_ALWAYS, // create flags FILE_ATTRIBUTE_NORMAL, // file attributes (HANDLE) NULL); // no template if (hFile == INVALID_HANDLE_VALUE) { ErrorHandler("Could not open file."); } // Find all of the loaded file's resources. hResult = StringCchPrintf(szBuffer, 80/sizeof(TCHAR), // cbString = sprintf(szBuffer, "The file contains the following resources:\n\n"); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } hResult = StringCchLength(szBuffer, 80/sizeof(TCHAR), cbString); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } WriteFile(hFile, // file to hold resource info. szBuffer, // what to write to the file (DWORD) cbString, // number of bytes in szBuffer &cbWritten, // number of bytes written NULL); // no overlapped I/O EnumResourceTypes(hExe, // module handle (ENUMRESTYPEPROC)EnumTypesFunc, // callback function 0); // extra parameter // Unload the executable file whose resources were // enumerated and close the file created to contain // the resource information. FreeLibrary(hExe); CloseHandle(hFile); // FUNCTION: EnumTypesFunc(HANDLE, LPSTR, LONG) // // PURPOSE: Resource type callback BOOL EnumTypesFunc( HANDLE hModule, // module handle LPTSTR lpType, // address of resource type LONG lParam) // extra parameter, could be // used for error checking { size_t cbString; HRESULT hResult; // Write the resource type to a resource information file. // The type may be a string or an unsigned decimal // integer, so test before printing. if ((ULONG)lpType & 0xFFFF0000) { hResult = StringCchPrintf(szBuffer, 80/sizeof(TCHAR), "Type: %s\n", lpType); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } // cbString = sprintf(szBuffer, "Type: %s\n", lpType); } else { hResult = StringCchPrintf(szBuffer, 80/sizeof(TCHAR), "Type: %u\n", (USHORT)lpType); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } // cbString = sprintf(szBuffer, "Type: %u\n", (USHORT)lpType); } hResult = StringCchLength(szBuffer, 80/sizeof(TCHAR), cbString); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } WriteFile(hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL); // Find the names of all resources of type lpType. EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)EnumNamesFunc, 0); return TRUE; } // FUNCTION: EnumNamesFunc(HANDLE, LPSTR, LPSTR, LONG) // // PURPOSE: Resource name callback BOOL EnumNamesFunc( HANDLE hModule, // module handle LPCTSTR lpType, // address of resource type LPTSTR lpName, // address of resource name LONG lParam) // extra parameter, could be // used for error checking { size_t cbString; HRESULT hResult; // Write the resource name to a resource information file. // The name may be a string or an unsigned decimal // integer, so test before printing. if ((ULONG)lpName & 0xFFFF0000) { hResult = StringCchPrintf(szBuffer, 80/sizeof(TCHAR), "\tName: %s\n", lpName); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } // cbString = sprintf(szBuffer, "\tName: %s\n", lpName); } else { hResult = StringCchPrintf(szBuffer, 80/sizeof(TCHAR), "\tName: %u\n", (USHORT)lpName); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } // cbString = sprintf(szBuffer, "\tName: %u\n", (USHORT)lpName); } hResult = StringCchLength(szBuffer, 80/sizeof(TCHAR), cbString); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } WriteFile(hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL); // Find the languages of all resources of type // lpType and name lpName. EnumResourceLanguages(hModule, lpType, lpName, (ENUMRESLANGPROC)EnumLangsFunc, 0); return TRUE; } // FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG) // // PURPOSE: Resource language callback BOOL EnumLangsFunc( HANDLE hModule, // module handle LPCTSTR lpType, // address of resource type LPCTSTR lpName, // address of resource name WORD wLang, // resource language LONG lParam) // extra parameter, could be // used for error checking { HANDLE hResInfo; char szBuffer[80]; size_t cbString = 0; HRESULT hResult; hResInfo = FindResourceEx(hModule, lpType, lpName, wLang); // Write the resource language to the resource information file. hResult = StringCchPrintf(szBuffer, 80/sizeof(TCHAR), "\t\tLanguage: %u\n", (USHORT) wLang); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } hResult = StringCchLength(szBuffer, 80/sizeof(TCHAR), cbString); if (FAILED(hResult)) { // Add code to fail as securely as possible. return; } // cbString = sprintf(szBuffer, "\t\tLanguage: %u\n", (USHORT)wLang); WriteFile(hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL); // Write the resource handle and size to buffer. cbString = sprintf(szBuffer, "\t\thResInfo == %lx, Size == %lu\n\n", hResInfo, SizeofResource(hModule, hResInfo)); WriteFile(hFile, szBuffer, (DWORD) cbString, &cbWritten, NULL); return TRUE; }
相关文章推荐
- C/C++ 文件读写操作总结
- Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
- c与c++中的文件操作【二】
- C++ 文件流fstream对象操作文件(文本文件和二进制文件)
- C++ builder 的文件读写操作总结
- C++ 的各种文件读写操作总结 [转载]
- C/C++对文件操作的小结
- 【c++笔记十三】c++中的输入、输出和文件操作
- Windows 8学习笔记(二十一)--C++ 文件异步操作
- C++ ofstream和ifstream详细用法 及文件操作 导入到EXCEL
- C++之文件IO操作流
- scala文件操作--自动关闭打开的资源,读取properties文件
- Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
- C++使用BOOST操作文件、目录
- c++ 文件操作
- c++ 文件操作
- [转]c++: fstream文件操作及对二进制文件的操作
- C/C++文件IO输入输出操作——FILE*、fstream、windowsAPI
- c++对文件的操作汇总
- C++ 文件操作(ftream ofstream ifstream)