This documentation is archived and is not being maintained.

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

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

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

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

To create a Smart Device ATL project

  1. On the File menu, point to New, click Project, expand the Visual C++ node 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 launch the ATL Smart Device Project Wizard.

  6. Click Finish to close the wizard.

    For this walkthrough, you do not need 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. Under Threading model, 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. Under Parameter attributes, 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 complete.

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, navigate to the Visual C# node, expand it, expand the Smart Device node, and then click Pocket PC 2003.

  3. In the Templates pane, click Device Application.

  4. In the Name box, type SayHello, and then click OK.

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

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

  1. In Solution Explorer, right-click the SayHello project, and then on the shortcut menu, 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 showing folder contents, double-click HelloCOMObject, double-click Pocket PC 2003 (ARMV4), double-click Debug, and then click HelloCOMObject.dll.

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

  6. In Solution Explorer, right-click Form1.cs, and then on the shortcut menu, click View Code.

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

    using HelloCOMObjectLib;
    

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.

To configure the solution for deployment

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

  2. In Solution Explorer, right-click the InteropSolution, and then on the shortcut menu, click Project Dependencies.

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

  4. Click OK.

    The solution is ready for deployment.

To deploy the solution

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

  2. In the Deploy dialog box, select Pocket PC 2003 SE Emulator, and then click Deploy.

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

Show: