C# – How to merge assembly?

In my two previous blogs C# – How to read header of SWF file? and C# – How to play a .swf file? , I showed you how to work around to read the header of a swf file so that we can load it exactly. In my blogs you can see that we need a reference to an ocx control so that we can play a swf file. When adding this ocx control, two referenced dll will be automatically added into project. They are AxInterop.ShockwaveFlashObjects.dll and Interop.ShockwaveFlashObjects.dll. Shipping these two dlls will make my tool broken because I would like to convert a swf file to an exe. I would not like to convert one file to 3 files. Therefore I think about method to merge all assemblies into only one. So I can use either ILMerge (from Microsoft) or AssMer (from my site).


ILMerge takes a set of input assemblies and merges them into one target assembly. The first assembly in the list of input assemblies is the primary assembly. When the primary assembly is an executable, then the target assembly is created as an executable with the same entry point as the primary assembly. Also, if the primary assembly has a strong name, and a .snk file is provided, then the target assembly is re-signed with the specified key so that it also has a strong name.


And now I would like to introduce to you AssMer, a tool to merge your assemblies. As its name, you can merge only until now ONLY assemblies with each other. You can not merge an assembly with a native code file.This tool is based on source code of Mono.Merge.

You can choose any tool which you like. ILMerge is a console application. You can find its GUI at ILMerge GUI. The following code is a code snippet which I extract from my tool. You can see that after merging we must fix the reference too.

static void Process(MergeContext context)
AssemblyDefinition primary = AssemblyFactory.GetAssembly(context.Assemblies[0]);

for (int i = 1; i < context.Assemblies.Count; i++)
AssemblyDefinition asm = AssemblyFactory.GetAssembly(context.Assemblies[i]);
asm.Accept(new StructureMerger(context, primary, asm));

FixReflectionAfterMerge fix = new FixReflectionAfterMerge(context, primary, primary);

AssemblyFactory.SaveAssembly(primary, context.OutputPath);

I moved to new room

Last week I moved to new residential accommodation. The new room satisfies me. It is pretty large and beautiful. It is about 20m2, inclusive restroom and bathroom. The floor was not covered by old carpet like the other rooms. The previous tenant removed the old one and replaced it with new, beautiful carpet.
The weather on the day I moved was pretty bad. It rained all day. I must carry my things under the rain. Believe me; it is not romantic at all when you must carry a heavy luggage under the rain. Everything was ok except these following problems.
1. The previous tenant removed herself the old carpet. She must recover the room as it was before when she gave the room back but she did not. She was even not at the room at that day. Her friend met me and gave me the key. And he does not know anything about the status of the room. So if I were not responsible for the new one, I had to wait for 3 days (working days) so that she recovered the room. It is already weekend. Nobody works. So if I don’t like, I must wait 3 days weekend plus 3 working days. Where will I live in these days? I was put in case that everything was over. I must accept it.
2. I have no internet connection now. I made an appointment for moving internet to my new room. The technician came but he did not have a key to basement so that he could activate my telephone access. The concierge (janitor) was not in charge too. I called him and he said that he could not come. :(. I like the job of concierge. I think he really did not work. Some hours pro week and he received at least one thousand euro pro month. :).
3. For each room there are two keys but the previous tenant gave me only one. She is now somewhere
There are always something make me unhappy but that is life. I live happily with them. I think I will have internet connection from next week. My new room is beautiful. What a beautiful life.

C# – How to play a .swf file?

I am making a small research to convert a .swf file to an .exe file. I intend to build an .exe file with its resources which are .swf files. I will use this .exe file as a template later and just replace the resources with the .swf file which I want to convert. During the development I discover that I had a problem which I still did not solve. I could never load a .swf file in my application before. There are always problem when I tried to use the OCX control provided by Adobe.

Error loading ocx control
It happens always with Visual Studio 2005 at my computer although I registered it and re-registered it many times. I think there is a conflict between the version of .ocx control which I use and the version which VS 2005 expects. So today I tried with Visual Studio 2008 with the hope that it will work. So I do as following to insert a flash player to my application.

1. Right click on Toolbox window –> Choose Items…
Toolbox - Choose Items
2. Wait for a moment. On new window, click on Tab “COM Components” –> Shockwave Flash Object –> Click OK.

COM Component - Shockwave Flash Object

3. If it was successful, you would see the object Shockwave Flash Object on the Toolbox windows.

Shockwave Flash Object

4. Drag and Drop this component on Form.

Component on Form

5. Rename the control to chartFlashPlayer .Add this code to Form_Load event.

private void Form1_Load(object sender, EventArgs e)
chartFlashPlayer.LoadMovie(0, "D:\\fmared.swf");

6. Run form and I get it run. Ju hu.
Load successfully

I know this post may contain nothing helpful but anyways it’s first time that I can load .swf file in my application my computer at home. I feel happy. In next step I will try to load .swf file from resources and create a program to change the resource section so that I can replace with the file which I want. I hope everything will run smoothly. For more detailed instructions, you can follow this link http://www.adobe.com/devnet/flash/articles/stock_history.html

C# – How to read header of SWF file?

As I said in my previous blog about “How to play a swf file” yesterday, I am making a small tool to convert .swf file to an .exe file. In this tool I will wrap the .swf file in a dialog and play it. So it leads to a small problem that the width and height of dialog and ocx control containing .swf file should be adjusted to show the file correctly. To achieve this requirement I need some info about the width and height of swf which can be derived directly from .swf file. After some searching with Google for a while to look for a available source code helping me read info with c#, I decide to write own for myself because I realize it is not difficult to write my own and the other reason is the source code I found, does not work correctly.

First step, I need the SWF Specification Format which can be downloaded from adobe’s site. Just a moment going through the document I found what I need. That is the SWF Header.

SWF Header

All detailed information you can read more in the documentation of adobe. I just post here some important information which I really need during programming.

1. An FWS signature indicates an uncompressed SWF file; CWS indicates that the entire file after the first 8 bytes (that is, after the FileLength field) was compressed by using the ZLIB open standard. <–  I did not read carefully at this point. I think it will compress data after the header not inclusive the header and as consequence  I lost half hour to find out the problem.

2. In the SWF format, a twip is 1/20th of a logical pixel. A logical pixel is the same as a screen pixel when the file is played at 100%—that is, without scaling. <– Nothing special here, I just do not know what a twip is :).

3. 8.8 signed fixed-point numbers. The high 8 bits represent the number before the decimal point, and the low 8 bits represent the number after the decimal point <– Are you sure that you understand this statement? I am not. An example must be better and here is it. For example: The real value 7.5 is equivalent to: 0x07.0x80.This value is stored in the SWF file as: 80 07. Still not understand. Change them to binary and you’ll understand it.

I think I discussed all important information about SWF Header. And at last, a source code as result which is SWFFileHeader. It is a .cs file (not a .dll) so that you can use it anywhere you want, for example in Silverlight.

UPDATE 04/09/2010
To read the version of ActionScript which used in swf file we need to dig deeper into the SWF File structure. As shown in documentation of Adobe, the SWF File structure looks like below

SWF file structure

As you can see, after the file header is the “File Attribute Tag” which contains a property indicating the version of ActionScript in that SWF file

File Attribute Tag

It’s easy to edit the code of our class to read this property. One thing we should know that the type RECORDHEADER for tag “File Attribute” is the short one. That means its size is 2 bytes therefore we just need to read 2 bytes to bypass this struct. Then extract from the next byte the value of ActionScript version

int nTemp = strmReader.ReadByte();
nTemp = strmReader.ReadByte();
nTemp = strmReader.ReadByte();
ActionScript3 = ((nTemp & 8 ) == 0)?false:true;

Now through file attributes we can only define the ActionScript is version 3.0 or version 1.0/2.0. The complete source code of this blog can be downloaded as “Read SWF Header

UPDATE 11.02.2014
Mirror links for SWF specification format

Mirror 1: http://www.mediafire.com/view/iiowu0eg9gx3auo/swf-file-format-spec.pdf
Mirror 2: https://app.box.com/s/9vo2s1oa23hkjdd0ms7z
Mirror 3: https://mega.co.nz/#!2kR1laIC!k4sKKnOlpDNpubfsFCLWDbgU2iFWOjA2d3RgxyJsywg