Posted by: Zeeshan Amjad | November 25, 2009

WPF Programming in MSIL


Today i got some free time and decided to play with MSIL and did little experiment with it. I did one small experiment and try to make one simple application of WPF in MSIL.

  1: .assembly Wpfil{}
  2: 
  3: .method static void start ()
  4: {
  5:     .entrypoint
  6:     .maxstack 8
  7: 
  8: 	ldstr "Hello World from MSIL"
  9: 	call valuetype [PresentationFramework]System.Windows.MessageBoxResult [PresentationFramework]

 10: System.Windows.MessageBox::Show(class System.String)
 11: 	pop
 12: 	ret
 13: }
 14: 

The output of this program is one message box with “Hello World from MSIL”. But this message box is from WPF not a windows standard message box.

But if we want to display a window in WPF then we have to create object of at least two class Application and Window class. Here is a MSIL code to create object of Window and Application class.

  1: newobj     instance void [PresentationFramework]System.Windows.Window::.ctor()
  2: stloc.0
  3: 
  4: newobj     instance void [PresentationFramework]System.Windows.Application::.ctor()
  5: stloc.1
  6: 

But first we have to set the attribute of program as a single threaded. We can do it with the following MSIL statement.

  1: .custom instance void [mscorlib]System.STAThreadAttribute::.ctor()

Here is a complete MSIL code of the program.

  1: .assembly Wpfil{}
  2: 
  3: .method static void start ()
  4: {
  5:     .entrypoint
  6: 	.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
  7:     .maxstack 8
  8: 
  9: 	.locals init ([0] class [PresentationFramework]System.Windows.Window win,
 10: 		[1] class [PresentationFramework]System.Windows.Application app)
 11: 		
 12: 	newobj     instance void [PresentationFramework]System.Windows.Window::.ctor()
 13: 	stloc.0
 14: 
 15: 	newobj     instance void [PresentationFramework]System.Windows.Application::.ctor()
 16: 	stloc.1
 17: 	
 18: 	ldloc.0
 19: 	ldstr "Hello World from MSIL"
 20: 	callvirt   instance void [PresentationFramework]System.Windows.Window::set_Title(string)
 21: 	
 22: 	ldloc.1
 23: 	ldloc.0
 24: 	callvirt   instance int32 [PresentationFramework]System.Windows.Application::Run(class [PresentationFramework]System.Windows.Window)
 25: 	pop
 26: 	ret
 27: }
 28: 

The output of this program is one window with “Hello World from MSIL” caption.

Similarly we can define other properties of the window. Here is an example toe set the width and height of the window.

  1: .assembly Wpfil{}
  2: 
  3: .method static void start ()
  4: {
  5:     .entrypoint
  6: 	.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
  7:     .maxstack 8
  8: 
  9: 	.locals init ([0] class [PresentationFramework]System.Windows.Window win,
 10: 		[1] class [PresentationFramework]System.Windows.Application app)
 11: 		
 12: 	newobj     instance void [PresentationFramework]System.Windows.Window::.ctor()
 13: 	stloc.0
 14: 
 15: 	newobj     instance void [PresentationFramework]System.Windows.Application::.ctor()
 16: 	stloc.1
 17: 	
 18: 	ldloc.0
 19: 	ldc.r8     400
 20: 	callvirt   instance void [PresentationFramework]System.Windows.FrameworkElement::set_Width(float64)
 21: 
 22: 	ldloc.0
 23: 	ldc.r8     300
 24: 	callvirt   instance void [PresentationFramework]System.Windows.FrameworkElement::set_Height(float64)
 25: 
 26: 	ldloc.0
 27: 	ldstr "Hello World from MSIL"
 28: 	callvirt   instance void [PresentationFramework]System.Windows.Window::set_Title(string)
 29: 	
 30: 	ldloc.1
 31: 	ldloc.0
 32: 	callvirt   instance int32 [PresentationFramework]System.Windows.Application::Run(class [PresentationFramework]System.Windows.Window)
 33: 	pop
 34: 	ret
 35: }
 36: 

Here is the output of this program.

MSILWPF

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: