Home » Top Secret » Using Inline x64 Assembler with a Visual C++ Windows Forms Based Application

Using Inline x64 Assembler with a Visual C++ Windows Forms Based Application

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 43 other followers

Leveraging Visual Studio 2010 and C++, here’s how I created a simple 64 bit forms based application which calls out to x64 assembler to retrieve a value and then display that in a message box.

Step 1:

Create your project….

Create a new “Windows Forms Project” titled “FormsBasedAssembler” like this:


Step 2:

Allow your project to compile x64 assembly files.

Change Build Customizations to compile assembly code into object form using Microsoft’s inbuilt MASM Assemble. This step is necessary to compile the ASM file you will be creating which contains the assembly code into a linkable format which will be combined with C++ object files to merge and create the executable by right clicking on the project file, selecting build customizations, and then placing a check mark next to MASM like this:


Step 3:

Convert your project from 32 bit to 64 bit.

Change the project type in the configuration manager to build for x64 based applications. Out of the box this will default to Win32 – or 32 bit applications, but we need to change this to support the 64 bit forms based application we’re creating.

Here’s a visual of how ya’s do this in Visual Studio 2010:


Afterwards, you should see your solution platform drop down box has ‘x64’ like this:


Step 4:

Allow your project to have unmanaged assembly code interacting with managed C++ code.

Right click on your project, and then left click on Properties.


When that dialog pops up, select “Configuration Properties”, C/C++, General, and then change common language support from ‘pure’ (C++/managed only) to ‘Common Language Support’, then select ‘ok’. This tells the compiler to include support for our standard C++ functions (like sprintf, which we’ll be using later) – and that we need versions of these common language routines which will play nice with our unmanaged assembly code.

A visual of this is as follows:


Step 5:

Allow your project to link in the standard windows libraries.

To explain this, the project I had you select was a Windows Forms based application. But by default, Windows forms does not include implicit support for Windows API calls which we’ll be using later with the MessageBox function.

We need to tell the project to reference the solution – which DOES include these libraries. Weird why the solution would have it but the project doesn’t automatically inherit from it unless explicitly told to do so, I know.

Don’t blame me. Microsoft built this tool.

So here’s how you do it:

Right click on your project, and then left click on Properties.


When that dialog pops up, select “Configuration Properties”, Linker, INPUT, and then change ADDITIONAL DEPENDENCIES from blank (”) to ‘inherit from parent of project defaults’. This tells the linker to include support for all the standard windows libraries (kernel32.lib, user32.lib, etc) so our Messagebox function is resolved properly.

Like this:


Step 6:

Drop a button on your form…

From the Toolbox, select a “Button” object and drop that in the middle of your form. This button will be used when you click on it to display information returned from the Assembly function.

Here’s how ya create the button:



Step 7:

Create the Button’s code to respond to your click.

Create the C++ code in the button’s code behind method. This code will prepare a string which will display in the message box the information returned from the x64 assembly call.

So first, double click on the button you just placed on the form, which creates the code behind frame, and from there here’s the C++ code:


To explain the code:

First, the assembly code we’re going to create will have an integer return value. The variable named ‘test’, defined as an integer, will contain this value.

Second, I call the assembly function we’re going to create which is named ‘GetValueFromASM’, which will return a value we place in that assembly code.

Third, we define a message buffer that’s 64 characters or 64 bytes in length, more than enough to print the value retrieved from the ‘test’ integer and place a little string message around it (ie: “GetValueFromASM returned xxxx”)

And finally, I display the message box to us the user and creator of this application leveraging the standard Windows API call (Messagebox) with the message and the title “This is a test”

Step 8:

Create a prototype function for the external assembly code.

Create a GetValueFromASM.h Header file. This file will provide the C++ functional prototype definition for the x64 assembly so C++ doesn’t complain about resolving the assembly function when it goes to compile our C++ code.

You create this by right clicking on the header files (a habit I like being in), selecting ‘add’, then ‘new item’ then ‘header file’ then renaming it to “GetValueFromASM.H” and then hitting the ‘add’ button as depicted below:


One this file is open, type the following code in:


To explain the code: The #ifndef/#define/and #endif are standard practice for C and C++ header files, you do this to prevent things from being defined multiple times should the header file be included more than once by accident in your project.

And the critical part – we already know from our prior function definition that we’re expecting an integer to be returned from the assembly function call, so now we tell the C++ compiler that this function – GetValueFromASM is externally defined and will return an int type.

Step 9:

Create the x64 Assembly code file.

Next up to bat is the meat of the assembly code we’re generating. Create an assembly file by right clicking on the ‘source’ files in the solution, then clicking on ‘add’, then ‘New Item’, then when that dialog pops up – just type in the value ‘GetValueFromASM.asm’ as the file name and then click the ‘add’ button.

Like this:


Now from there, type this x64 assembly code in:


To explain the code:

The first line defines the code section of the assembler file. The second line declares a procedure by the same name we defined previously, GetValueFromASM, the third line moves into the return (eax) register the value of 8675309 (Jenny’s Number), the fourth ‘ret’ returns from the procedure call.

And the final line ‘end’ terminates the code section.

That’s it for the assembler.

Step 10:

Make the changes we’ve made visible to the entire project by…

Updating the STDAFX.H Header file.

In the button code behind I had you create, you referenced three functions which need defined. “Messagebox”, which the definition can be found in windows.h, “sprintf”, which can be found in the standard c and C++ include file stdio.h, and the function I had you create in assembler “GetValueFromASM.h”

So now, you simply modify the STDAFX.H file by double clicking on it, and then adding these lines:



Step 11:

DONE! Run the application using F5, and press the button on the form when it pops up.

Here’s what I saw, you should see the same thing!


Exactly as expected.

That’s it. Hope this helps!



Leave a Reply

Please log in using one of these methods to post your comment:

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

Enter your email address to follow this blog and receive notifications of new posts by email.