简体   繁体   English

在Visual Studio中加载文本文件资源会比预期加载更多

[英]Loading of textfile resource in Visual Studio loads more than it should

As seen in various other posts, I have added a few text files as resources in a Visual Studio 2013 project in the following way: After right-clicking on the project and selecting Add -> Resources I added the following lines to the generated file resource.h : 从其他文章中可以看出,我已经通过以下方式在Visual Studio 2013项目中添加了一些文本文件作为资源:右键单击项目并选择“添加”->“资源”之后,将以下几行添加到了生成的文件resource.h

#define MY_TEXTFILE 256
#define MY_CONFIG_FILE_RELEASE 4313
#define MY_CONFIG_FILE_DEV1 4314
#define MY_CONFIG_FILE_DEV2 4315

Then, I added the following lines to the .rc file: 然后,我在.rc文件中添加了以下几行:

MY_CONFIG_FILE_RELEASE MY_TEXTFILE "configFiles/releaseConfig.properties"
MY_CONFIG_FILE_DEV1 MY_TEXTFILE "configFiles/devConfig.properties"
MY_CONFIG_FILE_DEV2 MY_TEXTFILE "configFiles/dev2Config.properties"

The content of those files is just simply one line, eg for the devConfig.properties it would be 这些文件的内容只是一行,例如对于devConfig.properties

# DEV1 CONFIG

To test the loading mechanism, I use the following directly within main 为了测试加载机制,我直接在main使用以下内容

int main(int argc, char *argv[]) {
  const char* data = NULL;
  loadTextFileResource(MY_CONFIG_FILE_DEV1, data);

  return 0;
}

where the loadTextFileResource is the following: 其中loadTextFileResource为以下内容:

bool loadTextFileResource(int inName, const char*& outData) {
  HMODULE _handle;
  GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
    GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)&someFunction, &_handle);
  if (_handle == NULL) return false;

  HRSRC _rc = FindResource(_handle, MAKEINTRESOURCE(inName), MAKEINTRESOURCE(MY_TEXTFILE));
  if (_rc == NULL) return false;

  HGLOBAL _rcData = LoadResource(_handle, _rc);
  if (_rcData == NULL) return false;

  LPVOID _rcDataLocked = LockResource(_rcData);
  if (_rcDataLocked == NULL) return false;

  DWORD _size = SizeofResource(_handle, _rc);
  if (_size == 0) return false;

  outData = static_cast<const char*>(_rcDataLocked);

  std::cout << "Loaded: " << outData << std::endl;

  return true;
}

The output of this little program is: 这个小程序的输出是:

Loaded: # DEV1 CONFIG
P# DEV2 CONFIG

PADPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADD
INGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADD
INGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADD
INGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADD
INGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDING

Note that the two lines in the beginning are the content of not only MY_CONFIG_FILE_DEV1 but also MY_CONFIG_FILE_DEV2 even though I only requested the former. 请注意,尽管我只要求前者,但开头的两行不仅是MY_CONFIG_FILE_DEV1的内容,而且还是MY_CONFIG_FILE_DEV2的内容。 The same happens when I request to load MY_CONFIG_FILE_RELEASE (ie all three files are loaded into outData ). 当我请求加载MY_CONFIG_FILE_RELEASE (即,所有三个文件都加载到outData ), MY_CONFIG_FILE_RELEASE发生相同的情况。 So it seems that somehow all "subsequent" resources are loaded together with the one I'm requesting. 因此,似乎所有“后续”资源都以某种方式与我请求的资源一起加载。 What is going on here exactly and why is my loadTextFileResource function not doing what I'm expecting, namely load only the content of the resource I'm requesting? 到底发生了什么,为什么我的loadTextFileResource函数没有按预期运行,即仅加载我请求的资源的内容?

Also: What is the "P" doing in front of "#DEV2 CONFIG" (which is the content of the respective file)? 另外:在“#DEV2 CONFIG”前面的“ P”在做什么(这是相应文件的内容)? Should I somehow clean the data that was loaded? 我应该以某种方式清除已加载的数据吗?

Note that this loading mechanism should also work if the project is compiled as a DLL. 请注意,如果将项目编译为DLL,则此加载机制也应起作用。

You assume that the text file data is zero-terminated by assigning the pointer to the beginning of the resource to the const char * : 您通过将指向资源开头的指针分配给const char *来假定文本文件数据以零结尾。

outData = static_cast<const char*>(_rcDataLocked);

Basically, you don't use the size at all. 基本上,您根本不用size All you need is to construct the string ( std::string or std::wstring depending on the encoding used by your original text files): 您需要构造的字符串( std::stringstd::wstring取决于原始文本文件使用的编码):

std::string result = { static_cast<const char *>(_rcDataLocked), size };

or 要么

std::wstring result = { static_cast<const wchar_t *>(_rcDataLocked), size / sizeof(wchar_t) };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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