[英]create file from resource.rc return null
i can't understand why findresource return null.我不明白为什么 findresource 返回 null。 i add new resources and import driver.sys.looked at all possible materials for studying this error, but found nothing to solve it.我添加了新资源并导入了 driver.sys。查看了所有可能的材料来研究这个错误,但没有找到解决它的方法。 in all of the cases, zero is returned, what can i do wrong?在所有情况下,都返回零,我能做错什么?
MAIN.CPP:主.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资源.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: $(项目名称).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" IMG 文件夹“driver.sys”/“DRIVER”
You are defining a resource whose ID is 101 and type is DRIVER
:您正在定义一个 ID 为 101 且类型为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
:但是,您随后要求FindResource()
查找 ID 为 101 且类型为BINARY
而不是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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.