C++/CLI extends C++ allowing the usage of .NET managed code and offer a new and more transparent way to interop managed and native worlds.
Let’s create a simple MFC application showing an example how to listen to events raised from a WPF object embedded on it, and then react to them within the MFC application.
Open Visual Studio 2008 and create a new MFC application project. I’m naming it “MFCWPFEventsInterop”.
The MFC wizard will be displayed.
The only change needed is to set the project style to Office as it is shown in the following picture and click Finish button.
Now we have to change project settings in order to use C++/CLI. Enter project properties and change to Common Language Runtime Support (/clr) and then apply your changes.
We also need to reference the WPF assemblies in Common Properties tab.
Then open the “MFCWPFEventsInterop.h” file and add the declaration of OnFileNew method as it is shown in the following code:
2: class CMFCWPFEventsInteropApp : public CWinAppEx
6: // Overrides
8: virtual BOOL InitInstance();
9: // Implementation
10: UINT m_nAppLook;
11: BOOL m_bHiColorIcons;
12: virtual void PreLoadState();
13: virtual void LoadCustomState();
14: virtual void SaveCustomState();
15: afx_msg void OnAppAbout();
16: afx_msg void OnFileNew();
Then open “MFCWPFEventsInterop.cpp” file, add the two “using” sentences as shown below, and also modify the “ON_COMMAND” so it will use our own implementation of the OnFileNew method.
2: // MFCWPFEventsInterop.cpp : Defines the class behaviors for the application.
4: #include "stdafx.h"
5: #include "afxwinappex.h"
6: #include "MFCWPFEventsInterop.h"
7: #include "MainFrm.h"
8: #include "ChildFrm.h"
9: #include "MFCWPFEventsInteropDoc.h"
10: #include "MFCWPFEventsInteropView.h"
11: using namespace System;
12: using namespace System::Windows;
13: #ifdef _DEBUG
14: #define new DEBUG_NEW
16: // CMFCWPFEventsInteropApp
17: BEGIN_MESSAGE_MAP(CMFCWPFEventsInteropApp, CWinAppEx)
18: ON_COMMAND(ID_APP_ABOUT, &CMFCWPFEventsInteropApp::OnAppAbout)
19: // Standard file based document commands
20: ON_COMMAND(ID_FILE_NEW, OnFileNew)
21: ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
22: // Standard print setup command
23: ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinAppEx::OnFilePrintSetup)
25: Finally we need to add our implementation code at the end of the file as
27: // CMFCWPFEventsInteropApp message handlers
28: void OnWPFAppClosed(Object ^sender, EventArgs ^args)
32: void CMFCWPFEventsInteropApp::OnFileNew()
35: Window^ wpfWindow = gcnew Window();
36: wpfWindow->Title = "This is a WPF window";
37: wpfWindow->Closed += gcnew EventHandler(OnWPFAppClosed);
As you cannotice in the code, we are creating and showing a WPF window at runtime when we add a new file on the MFC application.
We are also subscribing to the Closed event of the WPF window and in the event handler we are executing some native C++ code, in this case forcing the application to close.
When you debug the application you will see something similar to the picture below and the if you click the close button within the WPF object, the entire application will shutdown.
This is a simple example on how you can handle WPF window events (or any event) within a MFC or WIN32 application. You can extend it to handle any event thrown from your WPF application and react to it within the native code, so you can have the best of both native and managed worlds.
This post was written by Marcelo Martinez – Lead WPF Developer @ UruIT
Microsoft SQL Server 2008 incorporates a new feature named Policy Based Management, allowing the administration of SQL 2008-based policies. DBAs can control many different aspects of SQL Server and automate several time-consuming tasks.
Among other uses, the policies can help to ensure that Stored Procedures follow a desired standard (set of policies).
Then you could also write your own application to validate a set of stored procedures against the different policies defined in the target server, using .NET code and a set of classes available to interact with SQL Server objects.
Additionally, you could validate stored procedures from previous SQL versions such as 2005 and 2000.
In order to write some .NET code to achieve this, first we have to refer to a few assemblies:
1: using Microsoft.SqlServer.Management.Dmf;
3: using System.Data.SqlClient;
5: using Microsoft.SqlServer.Management.Sdk.Sfc;
7: using Microsoft.SqlServer.Management.Smo;
Then we should get the policies collection from our target server. We will use the GetPolicyCollection method which receives a parameter with the connection string information from our SQL Server 2008 hosting the policies.
1: private PolicyCollection GetPolicyCollection(string ConnectionString)
5: PolicyStore policyStore = new PolicyStore();
7: SqlConnection oSqlConn = new SqlConnection(ConnectionString);
9: SqlStoreConnection oStoreConn = new SqlStoreConnection(oSqlConn);
11: policyStore.SqlStoreConnection = oStoreConn;
13: return policyStore.Policies;
Then we can write some code to ensure that our StoredProcedure complies with established policies.
This method receives a StoredProcedure object, which represents the stored procedure we will evaluate.
Policy class exposes some methods to validate an StoredProcedure objects agains it.
We will iterate through the collection of policies and invoke the Evaluate method passing the sp object as parameters, and telling the framework to check whether the validation fails or not, using the AdHocPolicyEvaluationMode enum (in this case will be set to Check), and an array with the objects to validate.
1: private bool Validate(StoredProcedure sp)
5: Object parameters = new Object;
7: parameters = sp;
9: PolicyCollection policyCollection = GetPolicyCollection(strConnectionString);
11: foreach (Policy policyItem in policyCollection)
15: if (policyItem.Evaluate(AdHocPolicyEvaluationMode.Check, parameters))
19: return true;
23: return false;
And that’s it! You can have a friendly UI to display how a set of stored procedures will comply or not with the defined policies at the target server, before you actually decide to move them.
(by the way, it’s quite easy to move stored procedures from one server to another using the classes included in these libraries and a few lines of .NET code).
Post written by Santiago Gonnet - .NET Developer @ UruIT