Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Article

Embed Win32 resources in C# programs

Rate me:
Please Sign up or sign in to vote.
3.54/5 (10 votes)
10 Dec 20023 min read 140.9K   1.9K   31   15
How to emebd and use Win32 resources with a C# program

Image 1

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:

Image 2

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


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

Comments and Discussions

 
GeneralGreat! Pin
dedlok14-Dec-09 9:43
dedlok14-Dec-09 9:43 
QuestionHow to change a bitmap resource in a PE from C#.net code.... Pin
Kunal Patel17-Oct-08 8:07
Kunal Patel17-Oct-08 8:07 
GeneralSetting /win32res via MSBuild Pin
Chris Charabaruk6-Mar-07 13:42
professionalChris Charabaruk6-Mar-07 13:42 
GeneralRe: Setting /win32res via MSBuild Pin
Mike Dimmick25-Apr-08 3:02
Mike Dimmick25-Apr-08 3:02 
In Visual Studio 2005 (SP1, I haven't checked the original release), there's an option for this. On the Application tab, under Resources, select Resource File and enter the path to the resource file.

The documentation for /win32res (or /win32resource if you speak VB) claims that there is no UI in Visual Studio for this. It's wrong.


DoEvents: Generating unexpected recursion since 1991

QuestionDebug / Release build Pin
loonyjuice6-Oct-06 3:56
loonyjuice6-Oct-06 3:56 
GeneralCorrection in all .html files.. Pin
Liang-Kuan Hu26-Aug-06 5:18
Liang-Kuan Hu26-Aug-06 5:18 
Generalit won´t go Pin
DigitalD12-Jun-03 23:16
DigitalD12-Jun-03 23:16 
GeneralRe: it won´t go Pin
DigitalD12-Jun-03 23:48
DigitalD12-Jun-03 23:48 
GeneralRe: it won´t go Pin
DigitalD13-Jun-03 0:02
DigitalD13-Jun-03 0:02 
GeneralVS.NET can already do this! Pin
Richard Deeming11-Dec-02 3:49
mveRichard Deeming11-Dec-02 3:49 
GeneralRe: VS.NET can already do this! Pin
Nic Oughton29-Jul-04 2:43
professionalNic Oughton29-Jul-04 2:43 
GeneralMissing Zip + Incomplete article Pin
Stephane Rodriguez.11-Dec-02 2:10
Stephane Rodriguez.11-Dec-02 2:10 
GeneralZip fixed + I think it's complete Pin
Christian Tratz11-Dec-02 2:49
Christian Tratz11-Dec-02 2:49 
GeneralRe: Zip fixed + I think it's complete Pin
Stephane Rodriguez.11-Dec-02 2:57
Stephane Rodriguez.11-Dec-02 2:57 
GeneralRe: Zip fixed + I think it's complete Pin
caiafaverde1-Mar-05 3:20
caiafaverde1-Mar-05 3:20 

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

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