I have a c++ dll project, in which, I have embedded some raw data through "resource.rc" file.
IDR_TEMPLATE1 RCDATA "areaTemplate.bin"
Now I want to access the data of "areaTemplate.bin" file from the dll. How can I read the contents of "areaTemplate.bin" in a byte array?
Add DLL As Embedded Resource First, add the DLL as Reference. Then, add the same DLL as file into the project. Right click the project's name > Add > Existing Item... The same DLL will exist twice in different folder in the project.
Method 1: Add existing file, set property to Embedded Resource. Add the file to your project, then set the type to Embedded Resource . NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from @dtb).
A dynamic link library (DLL) is a collection of small programs that larger programs can load when needed to complete specific tasks. The small program, called a DLL file, contains instructions that help the larger program handle what may not be a core function of the original program.
Double click your Properties -> Resources. resx file to open the resource designer. From the first small drop down on the top, select "Files" as resource type. Click on Add Resource -> Add Existing File... and select your text files.
As Manuell says, you use FindResource(), LoadResource() and probably LockResource() and SizeofResource()
I happen to have some code which does pulls out a resource and writes it to a file, and may help with your understanding of the API in question.
void WriteResourceToFile(
HANDLE hFile,
const _tstring &resourceName,
const _tstring &resourceType,
HMODULE hModule)
{
HRSRC hResource = ::FindResource(
hModule,
resourceName.c_str(),
resourceType.c_str());
if (!hResource)
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(
_T("WriteResourceToFile() - FindResource"),
lastError);
}
HGLOBAL hGlobal = ::LoadResource(hModule, hResource);
if (!hGlobal)
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(
_T("WriteResourceToFile() - LoadResource"),
lastError);
}
void *pData = ::LockResource(hGlobal);
if (!pData)
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(
_T("WriteResourceToFile() - LockResource"),
lastError);
}
const DWORD bytes = ::SizeofResource(hModule, hResource);
DWORD bytesWritten = 0;
if (!::WriteFile(hFile, pData, bytes, &bytesWritten, 0))
{
const DWORD lastError = ::GetLastError();
throw CWin32Exception(
_T("WriteResourceToFile() - WriteFile"),
lastError);
}
if (bytesWritten != bytes)
{
throw CWin32Exception(
_T("WriteResourceToFile() - WriteFile"),
_T("Wrote less bytes (") + ToString(bytesWritten) +
_T("( than expected: ") + ToString(bytes));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With