Alex xelA
Alex xelA

Reputation: 85

C++: Add resources to project template

Currently I have a C++ project template that contains some pre-installed images, defined as an array of bytes inside the code. I want to reorganize the project by moving images to resources, but the problem is that .rc script only accepts absolute paths to images, thus I don't quite understand how can I properly use them for project template. All I want is to place image files somewhere inside the project directory, and reference them in project .rc script using relative paths for portability. Is it even possible?

VS2019 Community 16.5.4 btw.

UPDATE1: I need to include my resources at compile time, thus dynamic file management is not an option. I have .rc file in a project and some PNG images inside it. They are defined using absolute paths like IDB_PNG1 PNG "D:\\absolute\\path\\to\\file.png". The problem is that I can't use paths relative to the project, like IDB_PNG1 PNG "\\path\\inside\\project\\file.png", getting zero portability of my resources in the template.

UPDATE2: One possible solution I've found is to use CustomParameter element and write paths in .rc file like IDB_PNG1 PNG "$projectpath$\\path\\inside\\project\\file.png", then set ReplaceParameters=True for .rc file in .vstemplate. But there is only $projectname$ default parameter and I'm not even sure I can do it for absolute path.

UPDATE3 (ANSWER): Thanks to this question I've found there is undocumented parameter $solutiondirectory$ (hopefully MS won't drop it from future releases). So I simply write my resources as IDB_PNG1 PNG "$solutiondirectory$\\$projectname$\\path\\inside\\project\\file.png" and then these paths are automatically updated by the Visual Studio when project is created. Problem solved!

Upvotes: 0

Views: 551

Answers (2)

Alex xelA
Alex xelA

Reputation: 85

Thanks to this question I've found there is undocumented parameter $solutiondirectory$ (hopefully MS won't drop it from future releases). So I simply write my resources as IDB_PNG1 PNG "$solutiondirectory$\\$projectname$\\path\\inside\\project\\file.png" and then these paths are automatically updated by the Visual Studio when project is created. Problem solved!

Upvotes: 1

Cryptic
Cryptic

Reputation: 21

Assuming you are using a .rc file in the context of something like an icon image, it is possible. You can just use standard library to link to the image file and then read it byte for byte and load it into the arrays that you were using earlier.

If this is not the right context you could share some code for better understanding.

Upvotes: 0

Related Questions