Walkthrough: Hello World: A COM Interop Example for Smart Devices

This walkthrough combines in one solution a simple COM object and a managed application.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

This walkthrough was written using Visual C++ Development Settings.

Create a COM Object

To create a Smart Device ATL project

  1. On the File menu, point to New, click Project, expand Visual C++ in the Project types pane, and then click Smart Device.

  2. In the Templates pane, click ATL Smart Device Project.

  3. In the Name box, type HelloCOMObject.

  4. In the Solution name box, type InteropSolution.

  5. Click OK to start the ATL Smart Device Project Wizard.

  6. Click Finish to close the wizard.

    For this walkthrough, you do not have to change any default settings in the wizard.

To add a class

  1. In Solution Explorer, right-click the HelloCOMObject project, point to Add, and then click Class to open the Add Class dialog box.

  2. In the Categories pane, click Smart Device.

  3. In the Templates pane, click ATL Simple Object, and then click Add to open the ATL Simple Object Wizard.

  4. In the Short name box, type Hello.

  5. In the left pane, click Options to open the Options page.

  6. In Threading model group, select Free, and then click Finish.

To add a method to the class

  1. Open the Class View window from the tab on the desktop or from the View menu.

  2. Expand HelloCOMObject to display the IHello interface.

  3. Right-click IHello, point to Add, and then click Add Method to open the Add Method Wizard.

  4. In the Method name box, type HelloWorld.

  5. In the Parameter type box, select BSTR*.

  6. In the Parameter name box, type text.

  7. In the Parameter attributes group, select out.

  8. Click Add.

    The method box displays [out] BSTR* text.

  9. Click Finish to close the Add Method Wizard.

    The method STDMETHOD(HelloWorld)(BSTR* text) is displayed in the file Hello.h.

To add implementation to the method

  1. In Solution Explorer, double-click Hello.cpp to open that file in the Code Editor.

  2. In the STDMETHODIMP section, insert the following implementation code before the return statement:

    *text = SysAllocString(L"Hello World!");
    
  3. On the Build menu, click Build HelloCOMObject.

    A COM object is now part of the solution, and the first part of the walkthrough is finished.

Create a Managed Project

To add a managed project to the solution

  1. In Solution Explorer, right-click InteropSolution, point to Add, add then click New Project.

  2. In the Project Types pane, expand Visual C#, click Smart Device, click Smart Device Project in the Templates pane, type SayHello in the Name box, and then click OK.

  3. In the Add New Smart Device Project dialog box, click Device Application, and then click OK.

    The SayHello managed project is created as part of the solution, and a Windows Mobile 5.0 Pocket PC form appears in the Designer window.

Add the COM Object as a Reference in the Managed Project

To add the COM object as a reference in the managed project

  1. In Solution Explorer, right-click the SayHello project, and then click Add Reference.

  2. In the Add Reference dialog box, click Browse.

    The SayHello folder is displayed in the Look in box.

  3. Navigate to the parent folder (in this walkthrough, InteropSolution).

  4. In the window that displays folder contents, double-click HelloCOMObject, double-click Windows Mobile 5.0 Pocket PC SDK (ARMV4), double-click Debug, and then click HelloCOMObject.dll.

  5. Click OK to close the Add Reference dialog box.

    Note

    You must be logged in as an Administrator. In Windows Vista, select Run as administrator when you start Visual Studio.

  6. In Solution Explorer, right-click Form1.cs, and then click View Code.

  7. In the Using directives region at the top of the file, add the following code:

    using HelloCOMObjectLib;
    

Add Event Handling to the Managed Project

To add event handling to the managed project and build it

  1. Open the Form1 Designer.

  2. From the Toolbox, drag a Button onto the form.

  3. Double-click the button to open the code editor at the click event.

  4. Insert the following event-handling code for the button:

    string text;
    HelloClass h = new HelloClass();
    h.HelloWorld(out text);
    MessageBox.Show(text);
    
  5. On the Build menu, click Build SayHello.

Make Final Adjustments to the Solution

To configure the solution for deployment

  1. In Solution Explorer, right-click the SayHello project, and then click Set as StartUp Project.

  2. In Solution Explorer, right-click the InteropSolution solution, and then click Project Dependencies.

  3. In the Project Dependencies dialog box, select SayHello in the Projects drop-down, and then in the Depends on box, select HelloCOMObject.

  4. Click OK.

    The solution is ready for deployment.

Deploy the Mixed Solution

To deploy the solution

  1. On the Debug menu, click Start Without Debugging.

  2. In the Deploy dialog box, click Windows Mobile5.0 Pocket PC Emulator, and then click Deploy.

Save this solution for use in the Walkthrough: Debugging a Solution that Includes Both Managed and Native Code.

See Also

Concepts

COM Interoperability for Devices

Reference

Visual Basic and Visual C# (How Do I in Smart Devices)