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
On the File menu, point to New, click Project, expand Visual C++ in the Project types pane, and then click Smart Device.
In the Templates pane, click ATL Smart Device Project.
In the Name box, type HelloCOMObject.
In the Solution name box, type InteropSolution.
Click OK to start the ATL Smart Device Project Wizard.
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
In Solution Explorer, right-click the HelloCOMObject project, point to Add, and then click Class to open the Add Class dialog box.
In the Categories pane, click Smart Device.
In the Templates pane, click ATL Simple Object, and then click Add to open the ATL Simple Object Wizard.
In the Short name box, type Hello.
In the left pane, click Options to open the Options page.
In Threading model group, select Free, and then click Finish.
To add a method to the class
Open the Class View window from the tab on the desktop or from the View menu.
Expand HelloCOMObject to display the IHello interface.
Right-click IHello, point to Add, and then click Add Method to open the Add Method Wizard.
In the Method name box, type HelloWorld.
In the Parameter type box, select BSTR*.
In the Parameter name box, type text.
In the Parameter attributes group, select out.
Click Add.
The method box displays [out] BSTR* text.
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
In Solution Explorer, double-click Hello.cpp to open that file in the Code Editor.
In the STDMETHODIMP section, insert the following implementation code before the return statement:
*text = SysAllocString(L"Hello World!");
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
In Solution Explorer, right-click InteropSolution, point to Add, add then click New Project.
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.
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
In Solution Explorer, right-click the SayHello project, and then click Add Reference.
In the Add Reference dialog box, click Browse.
The SayHello folder is displayed in the Look in box.
Navigate to the parent folder (in this walkthrough, InteropSolution).
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.
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.
In Solution Explorer, right-click Form1.cs, and then click View Code.
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
Open the Form1 Designer.
From the Toolbox, drag a Button onto the form.
Double-click the button to open the code editor at the click event.
Insert the following event-handling code for the button:
string text; HelloClass h = new HelloClass(); h.HelloWorld(out text); MessageBox.Show(text);
On the Build menu, click Build SayHello.
Make Final Adjustments to the Solution
To configure the solution for deployment
In Solution Explorer, right-click the SayHello project, and then click Set as StartUp Project.
In Solution Explorer, right-click the InteropSolution solution, and then click Project Dependencies.
In the Project Dependencies dialog box, select SayHello in the Projects drop-down, and then in the Depends on box, select HelloCOMObject.
Click OK.
The solution is ready for deployment.
Deploy the Mixed Solution
To deploy the solution
On the Debug menu, click Start Without Debugging.
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