lecture 2 com - example. integrate the web webcam functionality using.net and com first let's...

23
Lecture 2 Lecture 2 COM - Example COM - Example

Upload: jennifer-lawrence

Post on 16-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

Lecture 2Lecture 2

COM - ExampleCOM - Example

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

Integrate the web Webcam Integrate the web Webcam functionality using .Net and COMfunctionality using .Net and COM

First let's explain what we should do exactly.First let's explain what we should do exactly.

After selecting and connecting to the After selecting and connecting to the device, I'll try to invoke some external device, I'll try to invoke some external functions using the PInvoke platform in functions using the PInvoke platform in order to integrate the web cam order to integrate the web cam functionalities in my application. To do functionalities in my application. To do that, I will wrap all the unmanaged code in that, I will wrap all the unmanaged code in a class so that the user deals indirectly a class so that the user deals indirectly with the unmanaged code via this one. with the unmanaged code via this one.

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

Step 1Step 1

II. Second let's explain and indicates how to integrate the avicap32.dll II. Second let's explain and indicates how to integrate the avicap32.dll in the application and witches functions are invoked via the PInvoke in the application and witches functions are invoked via the PInvoke platform to integrate the web cam functionalities:platform to integrate the web cam functionalities:

The avicap32.dll is normally located in the WINDOWS\System32, The avicap32.dll is normally located in the WINDOWS\System32, but if it is not found you can download it from this link:but if it is not found you can download it from this link:

http://www.dll-files.com/dllindex/dll-files.shtml?avicap32http://www.dll-files.com/dllindex/dll-files.shtml?avicap32 After After downloading it and verifying whether there are potentials risks or not downloading it and verifying whether there are potentials risks or not when using the given assembly, a measure should be taken in this when using the given assembly, a measure should be taken in this context by the way. context by the way.

The measure is to check the compatibility between the downloaded The measure is to check the compatibility between the downloaded component and your system in order to prevent troubles caused by component and your system in order to prevent troubles caused by bugs, therefore it is recommended to download and use this tool:bugs, therefore it is recommended to download and use this tool:

http://http://www.liutilities.comwww.liutilities.com/products/campaigns/affiliate//products/campaigns/affiliate/cb/offer/dllfiles/rbcb/offer/dllfiles/rb//

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

Step 2Step 2

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

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

Step 3Step 3

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

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

Step 4Step 4

Code classCode class

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

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

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

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

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

ResultResultpublic 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();} }

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

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

Example

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

How to create a COM object using VS 2008 and consume it How to create a COM object using VS 2008 and consume it

from VB 6.0 client applicationfrom 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();        }    

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

Add a reference to the Add a reference to the System.Runtime.InteropServices that System.Runtime.InteropServices that contains some useful classes used for contains some useful classes used for exposing the initial .Net assembly to COM.exposing the initial .Net assembly to COM.

In order to be exposed to COM, an In order to be exposed to COM, an assembly has to be strongly named. It assembly has to be strongly named. It means that it must be signed before. To means that it must be signed before. To sign the given assembly, go to sign the given assembly, go to Project Project menu> <Project name> property menu> <Project name> property ..

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

Check the "Sign the assembly checkbox"Check the "Sign the assembly checkbox"

A combo box just as in the figure 3 will invite you either 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 to create a new key pair for the assembly and store them in a *.snk file or to use an already existing *.snk file.in a *.snk file or to use an already existing *.snk file.

Choose the first alternative.Choose the first alternative.

Then set the file name in the above text box and the Then set the file name in the above text box and the password in the other text boxes then click OKpassword in the other text boxes then click OK

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

•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)]

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

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

                                            

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

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 this

namespace 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;

        }    }}

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

Implement the button event handler as follow:

Private Sub Command1_Click()Dim proxy As Person

Set proxy = New Personproxy.FirstName = "Bejaoui"proxy.LastName = "Bechir"proxy.Persist ("C:\myFile.xml")

End Sub

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

the Person object is ready to use