简体   繁体   English

FindResource 在 exe 文件上给出错误 1813

[英]FindResource gives error 1813 on exe file

I have been trying this for like the last 3 days and I still can't make it work.过去 3 天我一直在尝试这个,但我仍然无法让它工作。 The WINAPI FindResource function throws and error 1813: this means that the resource type is not found. WINAPI FindResource函数抛出错误 1813:这意味着未找到资源类型。 I am using eclipse so there is no .RC file or resource.h file so I have loaded my exe file with LoadLibraryA and then used the HANDLE in FindResourceA .我正在使用 eclipse,所以没有 .RC 文件或 resource.h 文件,所以我用LoadLibraryA加载了我的 exe 文件,然后在FindResourceA使用了HANDLE Here is my code:这是我的代码:

HRSRC hRsrc;
HMODULE hExe;

/** Loading resource file in the program */
if (!(hExe = LoadLibraryA(TEXT(filename)))) {
    printf("Cannot load resource error: %lu \n", GetLastError());
    return 0;
}
printf("Resource file %s is loaded to the program \n", filename);

/** Finding resource */
if (!(hRsrc = FindResourceA(hExe, MAKEINTRESOURCE(10), "EXE"))) {
    printf("Cannot find resource error: %lu \n", GetLastError());
    return 0;
}
puts("Resource found");

Now, from what I understand is that the MAKEINTRESOURCE(10) lets us define a custom resource.现在,据我所知, MAKEINTRESOURCE(10)允许我们定义自定义资源。 Is this correct or I am just taking it wrong?这是正确的还是我只是看错了? In the resource type I tried giving "EXE" "BIN" "Exehead" and RT_RCDATA but nothing works, and it still gives me the same error as before.在资源类型中,我尝试给出“EXE”“BIN”“Exehead”和RT_RCDATA但没有任何效果,它仍然给我与以前相同的错误。 All the solution's on the web are with .RC and resource.h files on Visual Studio , is there anything to work with eclipse?网络上的所有解决方案都带有Visual Studio上的.RCresource.h文件,有什么可以与 eclipse 一起使用的吗? Thanks谢谢

To load a specific resource from a binary, you need to know the resource's type and its id. 要从二进制文件加载特定资源,您需要知道资源的类型及其ID。

If either one of those or both is not know you can use the function EnumResourceTypes() and/or EnumResourceNames() to find out. 如果其中一个或两个都不知道,则可以使用函数EnumResourceTypes()和/或EnumResourceNames()进行查找。

An example on how to use EnumResourceTypes() can be found here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms648008%28v=vs.85%29.aspx#_win32_Creating_a_Resource_List 可以在此处找到有关如何使用EnumResourceTypes()的示例: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms648008%28v=vs.85%29.aspx#_win32_Creating_a_Resource_List


Update: 更新:

If the recource editor provided by MS is not avaible this free tool might help: http://www.resedit.net/ I used to use it and worked for my needs. 如果无法使用MS提供的资源编辑器,则此免费工具可能会有所帮助: http : //www.resedit.net/我曾经使用过它并且可以满足自己的需求。 Anyhow, I do not know its current state. 无论如何,我不知道它的当前状态。

The error is correct. 错误是正确的。 There is no resource of type "EXE" in your file. 您的文件中没有类型为"EXE"资源。 You said so yourself when you say that you don't have an .RC file. 您说自己没有.RC文件时是这样说的。 Resources are defined in .RC files, and if you don't have one, then you have no resources. 资源是在.RC文件中定义的,如果您没有资源,则没有资源。 To add a resource of type "EXE" with resource ID 10, you need to create an .RC file, add it to your project, and say 要添加资源ID为10的"EXE"类型的资源,您需要创建一个.RC文件,将其添加到您的项目中,然后说

10 EXE "somefile"

where "somefile" is some file whose contents you want to use as the resource data. 其中“ somefile”是一些文件,您要将其内容用作资源数据。

If your IDE does not support .RC files then you will not be able to add resources via the IDE. 如果您的IDE不支持.RC文件,则您将无法通过IDE添加资源。 You will have to add them some other way, say, via a postprocessing step. 您将不得不以其他方式添加它们,例如,通过后处理步骤。

But the point is that you are getting the error "resource type not found" because you did nothing to create a resource of that type. 但是,要点是,您收到错误“找不到资源类型”,因为您没有执行任何操作来创建该类型的资源。 What did you expect? 您期望什么?

(* (*

Remember: When You start a new Project.... Delphi adds {$R *.res} to your .dpr file to link the .res file that it generates into your application.请记住:当您开始一个新项目时.... Delphi 将 {$R *.res} 添加到您的 .dpr 文件以将它生成的 .res 文件链接到您的应用程序中。 Eg if you save your project as MyProject.dpr, Delphi will create a file MyProject.res that contains your application icon and version information that you specify in Project Options in Delphi.例如,如果您将项目保存为 MyProject.dpr,Delphi 将创建一个文件 MyProject.res,其中包含您在 Delphi 的项目选项中指定的应用程序图标和版本信息。 Without this .res file, your .exe won't have an icon or version info AND YOU'LL GET 1813.[EG.: from GetFileVersionInfoSize( PChar( @sFileName[ 1 ] ), 0);没有这个 .res 文件,你的 .exe 不会有图标或版本信息,你会得到 1813.[EG.: from GetFileVersionInfoSize( PChar( @sFileName[ 1 ] ), 0); ] ]

If you get a duplicate resource warning, you probably have another {$R} compiler directive elsewhere in your code that also links in MyProject.res.如果您收到重复资源警告,则您的代码中其他地方可能还有另一个 {$R} 编译器指令,该指令也链接到 MyProject.res。 It could be a duplicate {$R *.res} in your .dpr file, or a {$R MyProject.res} in another .pas file.它可能是 .dpr 文件中的重复 {$R *.res},或另一个 .pas 文件中的 {$R MyProject.res}。 (Just) Delete the other compiler directive instead of the one that Delphi generates automatically, and your project will compile just fine. (只是)删除另一个编译器指令而不是 Delphi 自动生成的那个,您的项目将编译得很好。 *) *)

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

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