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

Embed Win32 resources in C# programs

, 10 Dec 2002
Rate this:
Please Sign up or sign in to vote.
How to emebd and use Win32 resources with a C# program

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

Share

About the Author

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

Comments and Discussions

 
GeneralGreat! Pinmemberdedlok14-Dec-09 10:43 
QuestionHow to change a bitmap resource in a PE from C#.net code.... PinmemberKunal Patel17-Oct-08 9:07 
GeneralSetting /win32res via MSBuild PinmemberChris Charabaruk6-Mar-07 14:42 
GeneralRe: Setting /win32res via MSBuild PinmvpMike Dimmick25-Apr-08 4:02 
QuestionDebug / Release build Pinmemberloonyjuice6-Oct-06 4:56 
GeneralCorrection in all .html files.. PinmemberLiang-Kuan Hu26-Aug-06 6:18 
Generalit won´t go PinsussDigitalD13-Jun-03 0:16 
GeneralRe: it won´t go PinsussDigitalD13-Jun-03 0:48 
GeneralRe: it won´t go PinmemberDigitalD13-Jun-03 1:02 
GeneralVS.NET can already do this! PinmemberRichard_D11-Dec-02 4:49 
GeneralRe: VS.NET can already do this! PinmemberNic Oughton29-Jul-04 3:43 
GeneralMissing Zip + Incomplete article Pinmember.S.Rod.11-Dec-02 3:10 
GeneralZip fixed + I think it's complete PinmemberChristian Tratz11-Dec-02 3:49 
GeneralRe: Zip fixed + I think it's complete Pinmember.S.Rod.11-Dec-02 3:57 
GeneralRe: Zip fixed + I think it's complete Pinmembercaiafaverde1-Mar-05 4:20 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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