简体   繁体   中英

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. The WINAPI FindResource function throws and error 1813: this means that the resource type is not found. 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 . 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. 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. All the solution's on the web are with .RC and resource.h files on Visual Studio , is there anything to work with eclipse? Thanks

To load a specific resource from a binary, you need to know the resource's type and its id.

If either one of those or both is not know you can use the function EnumResourceTypes() and/or EnumResourceNames() to find out.

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


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. Anyhow, I do not know its current state.

The error is correct. There is no resource of type "EXE" in your file. You said so yourself when you say that you don't have an .RC file. Resources are defined in .RC files, and if you don't have one, then you have no resources. 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

10 EXE "somefile"

where "somefile" is some file whose contents you want to use as the resource data.

If your IDE does not support .RC files then you will not be able to add resources via the 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. 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. 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); ]

If you get a duplicate resource warning, you probably have another {$R} compiler directive elsewhere in your code that also links in MyProject.res. It could be a duplicate {$R *.res} in your .dpr file, or a {$R MyProject.res} in another .pas file. (Just) Delete the other compiler directive instead of the one that Delphi generates automatically, and your project will compile just fine. *)

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