简体   繁体   中英

create file from resource.rc return null

i can't understand why findresource return null. i add new resources and import driver.sys.looked at all possible materials for studying this error, but found nothing to solve it. in all of the cases, zero is returned, what can i do wrong?

MAIN.CPP:

using namespace std;

#pragma warning(disable: 6387)

class Resource 
{
public:
    struct Parameters 
    {
        std::size_t size_bytes = 0;
        void* ptr = nullptr;
    };

private:
    HRSRC hResource = nullptr;
    HGLOBAL hMemory = nullptr;

    Parameters p;

public:
    Resource(int resource_id, const std::string& resource_class) {
        hResource = FindResource(nullptr, MAKEINTRESOURCEA(resource_id), resource_class.c_str());
        hMemory = LoadResource(nullptr, hResource);

        p.size_bytes = SizeofResource(nullptr, hResource);
        p.ptr = LockResource(hMemory);
    }

    auto GetResourceString() const 
    {
        std::string_view dst;
        if (p.ptr != nullptr)
            dst = std::string_view(reinterpret_cast<char*>(p.ptr), p.size_bytes);
        return dst;
    }
};

void GetFile() {
    Resource very_important(IDR_DRIVER1, "BINARY");
    auto dst = very_important.GetResourceString();
}

int main()
{
    Resource res(IDR_DRIVER1, "BINARY");

    system("pause");
    return 0;
}

RESOURCE.H

//{{NO_DEPENDENCIES}}
// Включаемый файл, созданный в Microsoft Visual C++.
// Используется windowsD resources.rc
//
#define IDR_DRIVER1                     101

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

$(ProjectName).rc:

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// Русский (Россия) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#pragma code_page(1251)

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""winres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// DRIVER
//

IDR_DRIVER1             DRIVER                  "driver.sys"

#endif    // Русский (Россия) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

IMG folder "driver.sys"/"DRIVER"

图片

You are defining a resource whose ID is 101 and type is DRIVER :

#define IDR_DRIVER1                     101
IDR_DRIVER1             DRIVER                  "driver.sys"

But, you are then asking FindResource() to find a resource whose ID is 101 and type is BINARY rather than DRIVER :

Resource very_important(IDR_DRIVER1, "BINARY");
Resource res(IDR_DRIVER1, "BINARY");

You need to change that to this:

Resource very_important(IDR_DRIVER1, "DRIVER");
Resource res(IDR_DRIVER1, "DRIVER");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM