Advertisement
bherbert

Hosting .NET controls in visual foxpro 9

Aug 5th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. ActiveX code
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. //using System.Linq;
  6. using System.Text;
  7. using System.Data;
  8. using System.Reflection;
  9. using System.Runtime.InteropServices;
  10. using Microsoft.Win32;
  11. using System.Windows.Forms;
  12.  
  13. namespace EventLib
  14. {
  15.     //[ComVisible(false)]
  16.     //public delegate void eventDel (string message);
  17.  
  18.     //[GuidAttribute("93b89bc5-ad20-4e10-a02c-ff256b36dea8")]
  19.     //[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
  20.     [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  21.     public interface EventsImp
  22.     {
  23.         [DispId(1)] void SendMessage (string message);
  24.     }
  25.  
  26.     //[Guid("d8f9f8d4-d395-49c6-97d7-ac319f12d085")]
  27.     //[ProgId("EventLib.EventTest")]
  28.     //[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(EventsImp))]
  29.     [ComSourceInterfaces(typeof(EventsImp))]
  30.     [ClassInterface(ClassInterfaceType.AutoDual)]
  31.     [ProgId("EventLib.EventTest")] 
  32.     public class EventTest
  33.     {
  34.         public event eventDel testEvent;
  35.         public delegate void eventDel (string message);
  36.  
  37.         /// <summary>
  38.         ///
  39.         /// </summary>
  40.         /// <returns></returns>
  41.         public string GetEvent ()
  42.         {
  43.             try
  44.             {
  45.                 // fire event;
  46.                 if (testEvent != null)
  47.                 {
  48.                     testEvent("Message from Event !");
  49.                 }
  50.                 return "Message not with event !";
  51.             }
  52.             catch (Exception ex)
  53.             {
  54.                 return ex.Message;
  55.             }
  56.         }
  57.  
  58.         /// <summary>
  59.         ///
  60.         /// </summary>
  61.         /// <param name="message"></param>
  62.         public void Testing (string message)
  63.         {
  64.             MessageBox.Show(message);
  65.         }
  66.  
  67.         #region COM Registration
  68.  
  69.         //For more information on why the following two functions are necessary
  70.         //http://www.codeproject.com/cs/miscctrl/exposingdotnetcontrols.asp
  71.         //copyright Morgan Skinner, 2001
  72.         [ComRegisterFunction()]
  73.         public static void RegisterClass (string key)
  74.         {
  75.             // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
  76.             StringBuilder sb = new StringBuilder(key);
  77.             sb.Replace(@"HKEY_CLASSES_ROOT\", "");
  78.  
  79.             // Open the CLSID\{guid} key for write access
  80.             RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);
  81.  
  82.             // And create the 'Control' key - this allows it to show up in
  83.             // the ActiveX control container
  84.             RegistryKey ctrl = k.CreateSubKey("Control");
  85.             ctrl.Close();
  86.  
  87.             // Next create the CodeBase entry - needed if not string named and GACced.
  88.             RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
  89.             inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
  90.             inprocServer32.Close();
  91.  
  92.             // Finally close the main key
  93.             k.Close();
  94.         }
  95.  
  96.         [ComUnregisterFunction()]
  97.         public static void UnregisterClass (string key)
  98.         {
  99.             StringBuilder sb = new StringBuilder(key);
  100.             sb.Replace(@"HKEY_CLASSES_ROOT\", "");
  101.  
  102.             // Open HKCR\CLSID\{guid} for write access
  103.             RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);
  104.  
  105.             // Delete the 'Control' key, but don't throw an exception if it does not exist
  106.             k.DeleteSubKey("Control", false);
  107.  
  108.             // Next open up InprocServer32
  109.             RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
  110.  
  111.             // And delete the CodeBase key, again not throwing if missing
  112.             k.DeleteSubKey("CodeBase", false);
  113.  
  114.             // Finally close the main key
  115.             k.Close();
  116.         }
  117.  
  118.         #endregion
  119.     }
  120.  
  121.    
  122. }
  123.  
  124. FoxPro testing code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement