从资源中提取错误 1814
Getting Error 1814 Extracting from Resource
尝试从资源加载可执行文件时,出现错误 1814,显然我似乎不明白为什么会这样。
我的源代码看起来像这样
Resource.h
IDR_EXE1 EXE "C:\Users\*****\Documents\Visual Studio 2015\Projects\HelloWorldMessageBoxExample\Debug\HelloWorldMessageBoxExample.exe"
现在我从资源加载它的源代码如下所示:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include "resource.h"
int main()
{
HMODULE hModule = GetModuleHandle(NULL);
HRSRC hrsrc = FindResource(hModule, MAKEINTRESOURCE("IDR_EXE1"), L"EXE");
if (hrsrc == NULL) {
printf("Error finding Resource, Reason :%d", GetLastError());
getchar();
}
HGLOBAL hLoaded = LoadResource(NULL, hrsrc);
if (hLoaded == NULL) {
printf("Error Loading Resource, Reason: %d", GetLastError());
getchar();
}
LPVOID lpLock = LockResource(hLoaded);
if (lpLock == NULL) {
printf("Error Loading Locking Resource, Reason: %d", GetLastError());
getchar();
}
DWORD dwSize = SizeofResource(NULL, hrsrc);
if (dwSize == NULL) {
printf("Cannot Get Size of Resource, Reason: %d", GetLastError());
getchar();
}
HANDLE hFile = CreateFile(L"C:\Users\*******\AppData\Local\vaxi\hiMsg.exe", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == NULL) {
printf("Cannot Create File,Reason : %d", GetLastError());
getchar();
}
DWORD dwBytesWritten;
if (!WriteFile(hFile, lpLock, dwSize, &dwBytesWritten, NULL))
{
printf("Write File Failed,Reason : %d", GetLastError());
}
if (!CloseHandle(hFile))
{
printf("Close Handle Failure, Reason: %d", GetLastError());
}
else {
printf("Extracted Correctly!\n");
}
if (!ShellExecuteA(NULL, "open", (char*)hFile, 0, 0, SW_SHOWNORMAL)) {
printf("Shell Execute Failed, Reason: %d", GetLastError());
}
return 0;
}
而且我仍然收到错误 1814,这里到底出了什么问题?
错误代码 1814 是 ERROR_RESOURCE_NAME_NOT_FOUND
:
The specified resource name cannot be found in the image file.
错误正确。 MAKEINTRESOURCE("IDR_EXE1")
生成了错误的名称,因此找不到资源。您滥用了 MAKEINTRESOURCE()
宏。正确的用法是MAKEINTRESOURCE(IDR_EXE1)
。
尝试从资源加载可执行文件时,出现错误 1814,显然我似乎不明白为什么会这样。 我的源代码看起来像这样
Resource.h
IDR_EXE1 EXE "C:\Users\*****\Documents\Visual Studio 2015\Projects\HelloWorldMessageBoxExample\Debug\HelloWorldMessageBoxExample.exe"
现在我从资源加载它的源代码如下所示:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include "resource.h"
int main()
{
HMODULE hModule = GetModuleHandle(NULL);
HRSRC hrsrc = FindResource(hModule, MAKEINTRESOURCE("IDR_EXE1"), L"EXE");
if (hrsrc == NULL) {
printf("Error finding Resource, Reason :%d", GetLastError());
getchar();
}
HGLOBAL hLoaded = LoadResource(NULL, hrsrc);
if (hLoaded == NULL) {
printf("Error Loading Resource, Reason: %d", GetLastError());
getchar();
}
LPVOID lpLock = LockResource(hLoaded);
if (lpLock == NULL) {
printf("Error Loading Locking Resource, Reason: %d", GetLastError());
getchar();
}
DWORD dwSize = SizeofResource(NULL, hrsrc);
if (dwSize == NULL) {
printf("Cannot Get Size of Resource, Reason: %d", GetLastError());
getchar();
}
HANDLE hFile = CreateFile(L"C:\Users\*******\AppData\Local\vaxi\hiMsg.exe", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == NULL) {
printf("Cannot Create File,Reason : %d", GetLastError());
getchar();
}
DWORD dwBytesWritten;
if (!WriteFile(hFile, lpLock, dwSize, &dwBytesWritten, NULL))
{
printf("Write File Failed,Reason : %d", GetLastError());
}
if (!CloseHandle(hFile))
{
printf("Close Handle Failure, Reason: %d", GetLastError());
}
else {
printf("Extracted Correctly!\n");
}
if (!ShellExecuteA(NULL, "open", (char*)hFile, 0, 0, SW_SHOWNORMAL)) {
printf("Shell Execute Failed, Reason: %d", GetLastError());
}
return 0;
}
而且我仍然收到错误 1814,这里到底出了什么问题?
错误代码 1814 是 ERROR_RESOURCE_NAME_NOT_FOUND
:
The specified resource name cannot be found in the image file.
错误正确。 MAKEINTRESOURCE("IDR_EXE1")
生成了错误的名称,因此找不到资源。您滥用了 MAKEINTRESOURCE()
宏。正确的用法是MAKEINTRESOURCE(IDR_EXE1)
。