One of the cool new features in the Windows Azure Mix 09 CTP is the ability to run your Web and/or Worker Roles in full trust (non-admin). This opens up all kinds of features that weren’t available in the previous CTPs.
An interesting feature, which is the subject of this post is the ability to make PInvoke calls.
One of the things I have noticed in playing with calling native code is that you have to be more aware of the actual environment in the cloud. For example, Windows Azure in the cloud:
I also want to reiterate here that your Role Instances run in separate VMs and those VMs can come and go – it’s important not to count on any file system persistence in the VM.
1. Create a new Web Cloud Service project. File | New | Project. Select Visual C#/VB | Cloud Services | Web Cloud Services
2. Add a project to your solution for your native code. Right click on the Solution Explorer and select Add | New Project…
3. Select “Win32 Project” and call it “NativeCalc”
Setup the win32 project to be a DLL – we’ll just call a simple C function from it. Select “Export symbols”.
3. Open NativeCalc.h and change the dummy fnNativeCalc function to:
NATIVECALC_API int AddNumbers(int left, int right);
Note that the extern “C” specifies to use C linkage convention – what you see for the exported symbol if you used dumpbin – instead of the C++ decorations. This is what PInvoke will look for.
4. Now implement the method in NativeCalc.cpp
NATIVECALC_API int AddNumbers(int left, int right)
return left + right;
5. Next we have to ensure that this DLL is included as part of the Service Package. See this post for more details.
First I set the output directory of the DLL to be in the Web Role project directory.
I build all then right click on the nativeCloudService_WebRole project in Solution Explorer and select Add | Existing Item | NativeCalc.dll
I then set the CopyToOutputDirectory property to “CopyIfnewer”
6. Add a button and a label on default.aspx and in the button handler, set the label to the output of the native call:
static extern int AddNumbers(int left, int right);
protected void Button1_Click(object sender, EventArgs e)
Label1.Text = AddNumbers(2, 3).ToString();
7. Set the enableNativeExecution attribute on the WebRole in the ServiceDefinition.csdef
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="NativeCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="WebRole" enableNativeCodeExecution="true">
8. Run the application and hit the button – everything will work as expected – you just called native code running on Windows Azure!
9. If you want to publish and deploy this to the Cloud – make sure that the native DLL is being built in release mode and is built for x64. See the troubleshooting section below for more details.
Small troubleshooting section:
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. Exception Details: System.Security.SecurityException: System.Security.Permissions.SecurityPermission
When trying in the Cloud:
Compiling for x64 in Visual Studio related:
Just in time for Mix '09 - new Windows Azure Tools and SDK . New Features: Combined installer that installs
New version of the Azure SDK and tools is available. Most notable new features: Managed full trust...
Last week at MIX09, several new announcements were made for Windows Azure.  First, Full Trust support
If you have attended MIX 2009, you may already know that with Windows Azure Tools and SDK March 2009