Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

Programmatically embed non-localizable resource in assembly?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Notre Poubelle - 18 Aug 2005 18:43 GMT
Hi,

I would like to know how to programmatically embed a non-localizable
resource in assembly.  I know I can do this in the IDE (using 'Embedded
Resource' Build Action) or using the C# compiler in the command line like
this:

csc /res:myImage.bmp /out:myassembly.exe /target:exe /recurse:*.cs

and I know I can programatically create the resource at runtime:

Assembly executingAssembly = GetExecutingAssembly();
Stream resourceStream = executingAssembly. GetManifestResourceStream(
"myImage.bmp" );
Bitmap image = new Bitmap(resourceStream);

But how I can programmatically generate an assembly that contains some
non-localizable resources, e.g like an XML file, a bitmap, and some arbitrary
binary content?

Thanks,
Notre
Notre Poubelle - 18 Aug 2005 18:53 GMT
Little typo on the original post. I meant to say, I know how to
progammatically get the resource at runtime. I have no idea how to
programatically create it run time, if I did, I wouldnt' need to post the
question!
Nicolas Guinet - 18 Aug 2005 19:27 GMT
Hi,

Look for  Reflection.Emit, and ICodeCompiler

The ICodeCompiler interface can be implemented for a specific compiler to
enable developers to programmatically compile assemblies from Code Document
Object Model (CodeDOM) compile units, strings containing source code, or
source code files.

Nicolas Guinet

The next 2 samples show how to programmatically generate an assembly
(.exe...)

#1
---------------------------------------------------

public static CompilerResults CompileCode(CodeDomProvider provider,
                                         String sourceFile,
                                         String exeFile)
{
   // Configure a CompilerParameters that links System.dll
   // and produces the specified executable file.
   String[] referenceAssemblies = { "System.dll" };
   CompilerParameters cp = new CompilerParameters(referenceAssemblies,
exeFile, false);

   // Generate an executable rather than a DLL file.
   cp.GenerateExecutable = true;

   // Invoke compilation.
   CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile);

   // Return the results of compilation.
   return cr;
}
#2 ---------------------------------------------------using System;using
System.Reflection;using System.Reflection.Emit;using System.Threading;public
class EmitHelloWorld{      static void Main(string[] args)
{            // create a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "HelloWorld";             AssemblyBuilder
assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName,
AssemblyBuilderAccess.RunAndSave);            ModuleBuilder module;
module = assemblyBuilder.DefineDynamicModule("HelloWorld.exe");
// create a new type to hold our Main method            TypeBuilder
typeBuilder = module.DefineType("HelloWorldType", TypeAttributes.Public |
TypeAttributes.Class);                        // create the Main(string[]
args) method            MethodBuilder methodbuilder =
typeBuilder.DefineMethod("Main", MethodAttributes.HideBySig |
MethodAttributes.Static | MethodAttributes.Public, typeof(void), new Type[]
{ typeof(string[]) });                        // generate the IL for the
Main method            ILGenerator ilGenerator =
methodbuilder.GetILGenerator();            ilGenerator.EmitWriteLine("hello,
world");            ilGenerator.Emit(OpCodes.Ret);             // bake it
Type helloWorldType = typeBuilder.CreateType();             // run it
helloWorldType.GetMethod("Main").Invoke(null, new string[] {null});
// set the entry point for the application and save it
assemblyBuilder.SetEntryPoint(methodbuilder,
PEFileKinds.ConsoleApplication);
assemblyBuilder.Save("HelloWorld.exe");      }}
Notre Poubelle - 18 Aug 2005 21:18 GMT
Hello Nicolas,

Thank you for your quick reply.  I have reviewed your second example program
in some level of detail and I've scanned your first sample program.  As I
understand it, what you are showing me is how to write a program that
generates an assembly, in both cases, an exe.  This generated assembly can
actually be run by an end user (the second program is a console application
that displays hello, world).

I'm not sure if my original question was misleading, or perhaps I'm not
fullly appreciating your advice.  What I'd like to do is generate an assembly
(a DLL rather than an EXE) that contains a number of embedded resources like
text files, XML files, bitmaps, etc.  I listed a few ways to generate such an
assembly (with an embedded resource) in the original post.  I'm not really
concerned that the assembly I generate can run; I just want to use it to
store some content which I can later query for its resources.  I'm not sure
if what you've suggested so far (which has been pretty interesting in it's
own right) will help me do this, or whether there is a more straightforward
way to create a 'resource only' DLL (whose manifest resources I can query)?

Thanks again!
"Peter Huang" [MSFT] - 19 Aug 2005 06:13 GMT
Hi

Here is a google link you may take a look.
Dynamically creating satellite assemblies with embedded resources
http://groups.google.com/group/microsoft.public.dotnet.framework/browse_thre
ad/thread/9864e29b70506025/21759e4b1e5fbd81?lnk=st&q=IResourceWriter+Assembl
yBuilder&rnum=4&hl=zh-CN#21759e4b1e5fbd81

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Notre Poubelle - 19 Aug 2005 17:02 GMT
Thank you Peter.  I'm very familiar with that post, as I was the one who
posed the question! :)  A community member did give a response which answered
my question.

Now, the question is slightly different.  In the earlier post, I was
creating a satellite assembly, so I was embedding culture specific content
(in that example, I was creating a french satellite resource assembly).  In
this case, I want my resource to be neutral (not associated with any specific
culture).  Furthermore, I would like to be able to use files as input
(probably in the form of some sort of stream) rather than encoding simple
(name, value) pairs as was demonstrated before.
"Peter Huang" [MSFT] - 22 Aug 2005 10:50 GMT
Hi

Based on my researching, another way to add a resource file  is to use the
AddResourceFile.
You may have a look at the link below which has some code sinppet.
AssemblyBuilder.AddResourceFile
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemReflectionEmitAssemblyBuilderClassAddResourceFileTopic2.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.