Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c++ - How to load a custom binary resource in a VC++ static library as part of a dll?

I have custom binary resources (animated cursors) that would like to store as resources in a static lib in Visual Studio C++. It turns out that custom binary resources will not get loaded by ::LoadCursor() or found by ::FindResource() if it is a custom resource and in a static library.

This question gives some work around.

Following its advice, if I add the *.res file to an exe as a "Configuration Property->Linker->Additional Dependency" then the static library will be able to find the resource.

BUT if the static library is part of a dll and I link it in as an Additional Dependency it is not found again!

How can I link the resources in a dll?

Or just make the binary be found in the static lib? The methods in the question are pretty cumbersome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Add Resource dialog click Import, select "All Files (.)" so that it allows you to import file of any type, and then just select the file you want there. When Custom Resource Type dialog pops up, type RCDATA into "Resource type" field.

If you open .rc file, you will see something like this:

/////////////////////////////////////////////////////////////////////////////
//
// RCDATA
//

IDR_RCDATA1          RCDATA               "myfile.whatever"

and it will generate resource.h with following line:

#define IDR_RCDATA1                  101

In code you access it like this:

#include "resource.h"
#include <windows.h>

int main(int argc, char* argv[])
{
    HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
    HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
    void* pMyBinaryData = ::LockResource(myResourceData);
    return 0;
}

where pMyBinaryData is pointer to first byte of this executable. For more information visit Resource Functions

Here's an example how you would save binary resource like this on disk:

#include "resource.h"
#include <windows.h>
#include <fstream>

int main(int argc, char* argv[])
{
    HRSRC myResource = ::FindResource(NULL, MAKEINTRESOURCE(IDR_RCDATA1), RT_RCDATA);
    unsigned int myResourceSize = ::SizeofResource(NULL, myResource);
    HGLOBAL myResourceData = ::LoadResource(NULL, myResource);
    void* pMyBinaryData = ::LockResource(myResourceData);

    std::ofstream f("C:\x.bin", std::ios::out | std::ios::binary);
    f.write((char*)pMyBinaryData, myResourceSize);
    f.close();

    return 0;
}

When you build project with resource like that, this resource will become part of your program (dll).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...