Click here to Skip to main content
Click here to Skip to main content

Embed Win32 resources in C# programs

By , 10 Dec 2002
 

Introduction

When you code a C# application - let's say a Windows Forms application - you usually use a bunch of different resources, like icons, bitmaps, strings and so on. However, .NET in general uses a different approach to store resource data than it used to be in a plain Win32 environment. Win32 resources are held in a special section of the PE (portable executable) file format. .NET on the other side follows the concept of assemblies that are responsible of storing (and finding) their resources. For example a .NET resource can be stored in a different file in a multifile assembly. It does not have to be in the same file. The resource manager takes care of this. (I should note here that it is of course possible in Win32 to use special resource DLLs, but you have to deal with this manually in your code).

The big benefit of the new .NET approach is the easy - or easier - internationalization of applications. The big drawback is that all tools and procedures that rely on Win32 resources do not work anymore. Now you say: Why should I bother? I'm using C# only...?

A scenario

Some might already have heard about the res:// protocol. It's a protocol extension of the Internet Explorer and let's you access any type of resoures within a DLL or an EXE file. This usually makes sense for embedded HTML pages like user documentation or a fancy HTML credits page, that can be displayed with the WebBrowser control The URL looks like this:

res://resource file[/resource type]/resource id

If the resource type is omitted, a type of 23 (for HTML) is used. The resource file is the full path of the EXE or DLL. The resource id is the name of the resource.

Embedding resources

As we heard already, .NET resources are different from Win32 resources, so the res:// protocol does not work with them. VS.NET does not offer us to add Win32 resources in a C# or VB.NET project (it does in a Managed C++ project).

A .NET executable (or DLL) however, complies to the earlier mentioned PE file format, so it does support standard Win32 resources. And indeed csc.exe and al.exe - the command line compiler and linker - offer a /win32res switch. But we want to use VS.NET, so compiling the whole project with csc.exe does not make too much sense here.

What about linking the Win32 resources with al.exe and use VS.NET for the compilation? Not supported by VS.NET either.

The solution to this dilemma (at least until VS.NET supports this at some point) is to use a little freeware tool called Resource Hacker. This useful tool helped me out at some other occasions already (e.g. customizing your Windows XP boot screen), and is of use here as well.

You can compile and build your application with VS.NET as you have always done. Then you have to add the HTML pages, the images and all the other stuff you need as follows:

The sample

The sample program provided with this article has already two embedded HTML pages and shows them in a WebBrowser control. You can use all HTML features you could use in a normal web environment. This is especially useful for little JavaScript or DHTML hacks to show fancy UIs or credits dialogs. The benefit of embedding those files vs. providing them as separate files is obviously the protection from being deleted or moved and (to a little extent) being altered.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Christian Tratz
Web Developer
Germany Germany
Member
If you are not living on the edge you are wasting space Wink | ;)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGreat!memberdedlok14 Dec '09 - 9:43 
QuestionHow to change a bitmap resource in a PE from C#.net code....memberKunal Patel17 Oct '08 - 8:07 
GeneralSetting /win32res via MSBuildmemberChris Charabaruk6 Mar '07 - 13:42 
GeneralRe: Setting /win32res via MSBuildmvpMike Dimmick25 Apr '08 - 3:02 
QuestionDebug / Release buildmemberloonyjuice6 Oct '06 - 3:56 
GeneralCorrection in all .html files..memberLiang-Kuan Hu26 Aug '06 - 5:18 
Generalit won´t gosussDigitalD12 Jun '03 - 23:16 
GeneralRe: it won´t gosussDigitalD12 Jun '03 - 23:48 
GeneralRe: it won´t gomemberDigitalD13 Jun '03 - 0:02 
GeneralVS.NET can already do this!memberRichard_D11 Dec '02 - 3:49 
GeneralRe: VS.NET can already do this!memberNic Oughton29 Jul '04 - 2:43 
GeneralMissing Zip + Incomplete articlemember.S.Rod.11 Dec '02 - 2:10 
GeneralZip fixed + I think it's completememberChristian Tratz11 Dec '02 - 2:49 
GeneralRe: Zip fixed + I think it's completemember.S.Rod.11 Dec '02 - 2:57 
GeneralRe: Zip fixed + I think it's completemembercaiafaverde1 Mar '05 - 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 11 Dec 2002
Article Copyright 2002 by Christian Tratz
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid