Advertisement
bherbert

.NET controls in visual foxpro 9

Aug 5th, 2011
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.69 KB | None | 0 0
  1. ActiveX code
  2.  
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. //using System.Linq;
  7. using System.Text;
  8. using System.Data;
  9. using System.Reflection;
  10. using System.Runtime.InteropServices;
  11. using Microsoft.Win32;
  12. using System.Windows.Forms;
  13.  
  14. namespace EventLib
  15. {
  16.     //[ComVisible(false)]
  17.     //public delegate void eventDel (string message);
  18.  
  19.     //[GuidAttribute("93b89bc5-ad20-4e10-a02c-ff256b36dea8")]
  20.     //[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
  21.     [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  22.     public interface EventsImp
  23.     {
  24.         [DispId(1)] void SendMessage (string message);
  25.     }
  26.  
  27.     //[Guid("d8f9f8d4-d395-49c6-97d7-ac319f12d085")]
  28.     //[ProgId("EventLib.EventTest")]
  29.     //[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(EventsImp))]
  30.     [ComSourceInterfaces(typeof(EventsImp))]
  31.     [ClassInterface(ClassInterfaceType.AutoDual)]
  32.     [ProgId("EventLib.EventTest")] 
  33.     public class EventTest
  34.     {
  35.         public event eventDel testEvent;
  36.         public delegate void eventDel (string message);
  37.  
  38.         /// <summary>
  39.         ///
  40.         /// </summary>
  41.         /// <returns></returns>
  42.         public string GetEvent ()
  43.         {
  44.             try
  45.             {
  46.                 // fire event;
  47.                 if (testEvent != null)
  48.                 {
  49.                     testEvent("Message from Event !");
  50.                 }
  51.                 return "Message not with event !";
  52.             }
  53.             catch (Exception ex)
  54.             {
  55.                 return ex.Message;
  56.             }
  57.         }
  58.  
  59.         /// <summary>
  60.         ///
  61.         /// </summary>
  62.         /// <param name="message"></param>
  63.         public void Testing (string message)
  64.         {
  65.             MessageBox.Show(message);
  66.         }
  67.  
  68.         #region COM Registration
  69.  
  70.         //For more information on why the following two functions are necessary
  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. Assemby file
  125.  
  126. using System.Reflection;
  127. using System.Runtime.CompilerServices;
  128. using System.Runtime.InteropServices;
  129.  
  130. // General Information about an assembly is controlled through the following
  131. // set of attributes. Change these attribute values to modify the information
  132. // associated with an assembly.
  133. [assembly: AssemblyTitle("EventLib")]
  134. [assembly: AssemblyDescription("")]
  135. [assembly: AssemblyConfiguration("")]
  136. [assembly: AssemblyCompany("")]
  137. [assembly: AssemblyProduct("EventLib")]
  138. [assembly: AssemblyCopyright("Copyright ©  2011")]
  139. [assembly: AssemblyTrademark("")]
  140. [assembly: AssemblyCulture("")]
  141.  
  142. // Setting ComVisible to false makes the types in this assembly not visible
  143. // to COM components.  If you need to access a type in this assembly from
  144. // COM, set the ComVisible attribute to true on that type.
  145. [assembly: ComVisible(true)]
  146.  
  147. // The following GUID is for the ID of the typelib if this project is exposed to COM
  148. [assembly: Guid("4c80a921-5180-4b43-8b73-88cd93fa91f0")]
  149.  
  150. // Version information for an assembly consists of the following four values:
  151. //
  152. //      Major Version
  153. //      Minor Version
  154. //      Build Number
  155. //      Revision
  156. //
  157. // You can specify all the values or you can default the Build and Revision Numbers
  158. // by using the '*' as shown below:
  159. // [assembly: AssemblyVersion("1.0.*")]
  160. [assembly: AssemblyVersion("1.0.*")]
  161. [assembly: AssemblyFileVersion("1.0.0.0")]
  162.  
  163.  
  164. FoxPro 9 testing code
  165.  
  166. PUBLIC myObjt, loEvents
  167.  
  168. myObjt = CREATEOBJECT("EventLib.EventTest")
  169.  
  170. loEvents = CREATEOBJECT("EventsImp")
  171. ?EVENTHANDLER(myObjt,loEvents)
  172.  
  173. DOEVENTS
  174.  
  175. * callin activeX method to fire the event
  176. ?myObjt.GetEvent()
  177.  
  178. *x=NEWOBJECT("myEvent")
  179.  
  180. DEFINE CLASS EventsImp AS session
  181.  
  182.     IMPLEMENTS EventsImp IN "EventLib.EventTest"
  183.  
  184.     PROCEDURE EventsImp_SendMessage(message AS STRING) AS VOID
  185.    
  186.         ? message + " (From .net Event)"
  187.        
  188.     ENDPROC
  189.  
  190. ENDDEFINE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement