1 / 23

Lecture 2 COM - Example

Lecture 2 COM - Example. Integrate the web Webcam functionality using .Net and COM. First let's explain what we should do exactly.

hiero
Download Presentation

Lecture 2 COM - Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2COM - Example

  2. Integrate the web Webcam functionality using .Net and COM First let's explain what we should do exactly. • After selecting and connecting to the device, I'll try to invoke some external functions using the PInvoke platform in order to integrate the web cam functionalities in my application. To do that, I will wrap all the unmanaged code in a class so that the user deals indirectly with the unmanaged code via this one.

  3. Step 1 II. Second let's explain and indicates how to integrate the avicap32.dll in the application and witches functions are invoked via the PInvoke platform to integrate the web cam functionalities:The avicap32.dll is normally located in the WINDOWS\System32, but if it is not found you can download it from this link:http://www.dll-files.com/dllindex/dll-files.shtml?avicap32 After downloading it and verifying whether there are potentials risks or not when using the given assembly, a measure should be taken in this context by the way. The measure is to check the compatibility between the downloaded component and your system in order to prevent troubles caused by bugs, therefore it is recommended to download and use this tool:http://www.liutilities.com/products/campaigns/affiliate/cb/offer/dllfiles/rb/

  4. Step 2 Copy and paste the given assembly in C:\WINDOWS\System32 and then you have to register it using the regsvr32.exe tool.

  5. Step 3 But all those steps might not be necessary because in almost cases avicap32.dll is found in the System32. Now, let's see witches functions should be used to integrate the web cam functionalities.

  6. Step 4 Code class

  7. Class diagram This class provides three functionalities through three functions Open Connection : This method opens the connection to the devise Save Image : This method takes a snapshot view and save it in the hard disk Dispose : This method releases the connection to the device

  8. Step 5 The container is set to the picture box witch dedicated to display the image streaming.

  9. public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            oWebCam = new WebCam();            oWebCam.Container = pictureBox1;        }        WebCam oWebCam;        private void btnStart_Click(object sender, EventArgs e)        {oWebCam.OpenConnection();        }        private void btnSave_Click(object sender, EventArgs e)        {oWebCam.SaveImage();        }        private void btnStop_Click(object sender, EventArgs e)        {            oWebCam.Dispose();} } Result

  10. COM & Delphi 6,7.. < 2005 Example

  11. How to create a COM object using VS 2008 and consume it from VB 6.0 client application Build the COM serviced component First, let's start by creating a new (*.dll) application under Visual Studio 2005/2008 by selecting File>New> Project>Class library then name it COM. Then create a new object called Person that looks like bellow and don't forget to add reference to System.IO and System.Xml.Serialization in order to use the StreamWriter and the XmlSerializer objects.public class Person    {        public string FirstName { get;set;}        public string LastName { get; set; }        public bool IsMale { get; set; }        public void Persist(string FilePath)        {            StreamWriter oFile = new StreamWriter(FilePath);            XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Person));            oXmlSerializer.Serialize(oFile, this);            oFile.Flush();            oFile.Close();        }

  12. Add a reference to the System.Runtime.InteropServices that contains some useful classes used for exposing the initial .Net assembly to COM. • In order to be exposed to COM, an assembly has to be strongly named. It means that it must be signed before. To sign the given assembly, go to Project menu> <Project name> property .

  13. Check the "Sign the assembly checkbox" • A combo box just as in the figure 3 will invite you either to create a new key pair for the assembly and store them in a *.snk file or to use an already existing *.snk file. • Choose the first alternative. • Then set the file name in the above text box and the password in the other text boxes then click OK

  14. As you create a key pair, the assembly could be strongly named. • In the solution explorer, expand the properties node of the project and edit the AssemblyInfo.cs then add those three attributes, if you can't find ApplicationName, ApplicationActivation and AssemblyKeyFile elements then add a reference to System.EnterpriseServices    //Those are additional attributes that have to be added to the project    [assembly: ApplicationName("COM")]    [assembly: ApplicationActivation(ActivationOption.Library)]    [assembly: AssemblyKeyFile(@"C:\COM\COM\mySignature.snk")] • The first attribute indicates the application name. The second one indicates the activation option, by the way, the activation option could have • one among two values either library or server.Library indicates that the component runs in the creator process. In the other hand, server option indicates that component runs in the system process or in remoting context. • Always within the AssemblyInfo.cs, do change the ComVisible attribute to true in order to be visible to the COM client application    // Setting ComVisible to false makes the types in this assembly not visible     // to COM components. If you need to access a type in this assembly from     // COM, set the ComVisible attribute to true on that type.    [assembly: ComVisible(true)]

  15. // The following GUID is for the ID of the typelib if this project is exposed to COM • [assembly: Guid("6383a29e-fbf7-4c95-bbea-eb929e027f6a")] • Now, turn back to the Person class because there are some modifications and tasks that one should do before exposing the assembly to COM. • Remarque about the GUID: At the contrast of the .Net serviced component that is identified by the IP address and a given serial port. The COM serviced component is • identified by the GUID that stands for globally unique identifier. It is a 128-bit integer (16 bytes) that could be used across all computers and networks as • a COM assembly unique identifier. • In fact when you expose the assembly to COM, the client application will interact with your serviced component via interface. Therefore you have • to extract an interface from your class as follow.a) Go to Refactor menu then select extract interface, but first be sure to put the cursor inside the class.

  16. In the other side, Person class inherits from System.EnterpriseServices.ServicedComponent as an obligation, because all the classes exposed to COM must inherits from this class , the resulted class must look like thisnamespace COM{    [ClassInterface( ClassInterfaceType.None)]    public class Person : System.EnterpriseServices.ServicedComponent, COM.IPerson    {        public string FirstName{get;set;}        public string LastName { get; set; }        public bool IsMale { get; set; }        public void Persist(string FilePath)        {            StreamWriter oFile = new StreamWriter(FilePath);            XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Person));            oXmlSerializer.Serialize(oFile, this);            oFile.Flush();            oFile.Close();         } static public Person Retrieve(string FilePath)    {        StreamReader oFile = new StreamReader(FilePath);        XmlSerializer oXmlSerilizer = new XmlSerializer(typeof(Person));        Person oPerson = oXmlSerilizer.Deserialize(oFile) as Person;        return oPerson;        }    }}

  17. Implement the button event handler as follow:Private Sub Command1_Click()Dim proxy As Person Set proxy = New Person proxy.FirstName = "Bejaoui" proxy.LastName = "Bechir" proxy.Persist ("C:\myFile.xml")End Sub

  18. the Person object is ready to use

More Related