A nearshore team from Uruguay, South America (GMT-3) July 2009 - Posts - UruIT Blog

UruIT Blog

A nearshore team working with Microsoft technologies.

Sponsors

News

UruIT at WEBLOGS asp.net

July 2009 - Posts

Interoperability between MFC/WIN32 and WPF

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”.

clip_image001

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.

clip_image002

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.

clip_image003

We also need to reference the WPF assemblies in Common Properties tab.

clip_image005

Then open the “MFCWPFEventsInterop.h” file and add the declaration of OnFileNew method as it is shown in the following code:

   1:  
   2: class CMFCWPFEventsInteropApp : public CWinAppEx
   3: {
   4: public:
   5: CMFCWPFEventsInteropApp();
   6: // Overrides
   7: public:
   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();
  17: DECLARE_MESSAGE_MAP()
  18: };

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.

 

   1:  
   2: // MFCWPFEventsInterop.cpp : Defines the class behaviors for the application.
   3: //
   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
  15: #endif
  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)
  24: END_MESSAGE_MAP()
  25: Finally we need to add our implementation code at the end of the file as 
  26: follows:
  27: // CMFCWPFEventsInteropApp message handlers
  28: void OnWPFAppClosed(Object ^sender, EventArgs ^args)
  29: {
  30: AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);
  31: }
  32: void CMFCWPFEventsInteropApp::OnFileNew()
  33: {
  34: CWinAppEx::OnFileNew();
  35: Window^ wpfWindow = gcnew Window();
  36: wpfWindow->Title = "This is a WPF window";
  37: wpfWindow->Closed += gcnew EventHandler(OnWPFAppClosed);
  38: wpfWindow->Show();
  39: }

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.

clip_image006

 

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

Validating Stored Procedures using SQL Server 2008 policies and C#

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;
   2:  
   3: using System.Data.SqlClient;
   4:  
   5: using Microsoft.SqlServer.Management.Sdk.Sfc;
   6:  
   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)
   2:  
   3: {
   4:  
   5: PolicyStore policyStore = new PolicyStore();
   6:  
   7: SqlConnection oSqlConn = new SqlConnection(ConnectionString);
   8:  
   9: SqlStoreConnection oStoreConn = new SqlStoreConnection(oSqlConn);
  10:  
  11: policyStore.SqlStoreConnection = oStoreConn;
  12:  
  13: return policyStore.Policies;
  14:  
  15: }

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)
   2:  
   3: {
   4:  
   5: Object[] parameters = new Object[1];
   6:  
   7: parameters[0] = sp;
   8:  
   9: PolicyCollection policyCollection = GetPolicyCollection(strConnectionString);
  10:  
  11: foreach (Policy policyItem in policyCollection)
  12:  
  13: {
  14:  
  15: if (policyItem.Evaluate(AdHocPolicyEvaluationMode.Check, parameters))
  16:  
  17: {
  18:  
  19: return true;
  20:  
  21: }
  22:  
  23: return false;
  24:  
  25: }
  26:  
  27: }

 

 

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

More Posts