Udostępnij za pośrednictwem


Walkthrough: Debugging a Solution that Includes Both Managed and Native Code

This walkthrough provides steps for debugging a solution that includes both managed, .NET Compact Framework, and native components. Visual Studio does not support interop debugging of device applications as such. That is, you cannot have the native and managed debuggers attached at the same time.

The recommended technique for debugging a solution that incorporates both native and managed elements is to attach the debugger that is required for a certain section, for example, a managed section, detach that debugger, and then attach the other when the other is needed. You can repeat these detach/attach steps repeatedly to step through your program.

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. It contains the following sections:

  • Enabling attachment of the managed debugger

  • Starting the application

  • Setting a breakpoint in the native code

  • Attaching with the native debugger

  • Running to the native breakpoint

  • Attaching with the managed debugger

  • Setting a breakpoint in the managed code

  • Running to the managed breakpoint

  • Conclusion

Prerequisites

This walkthrough relies on the solution that is built with another walkthrough, Walkthrough: Hello World: A COM Interop Example for Smart Devices. Ensure that this walkthrough has been built and run.

Enabling Attachment of the Managed Debugger

By default, devices, including emulators, do not allow the managed debugger to attach to processes that are already running. Attaching the managed debugger to an already running process is a situation that you typically encounter in device solutions that include both managed and native code.

Your first step is to set the device to allow for the managed debugger to attach to an already running process. You do this by setting a registry key on the device.

Note

Setting the key affects only attaching to already-running managedprocesses. It does not affect starting a project by using Start with Debugging (F5). However, if you detach after Start with Debugging, you will need this process to reattach and start debugging again.

To enable the managed debugger to attach to a running process

  1. On the Windows Start menu, point to All Programs, point to Microsoft Visual Studio 2008, point to Visual Studio Remote Tools, and then click Remote Registry Editor.

  2. In the Select a Windows CE Device window, expand Windows Mobile 5.0 Pocket PC SDK, and then click Windows Mobile 5.0 Pocket PC Emulator. This is the target device for this walkthrough.

  3. Click OK.

    The Connecting to Device progress window opens, followed by the opening of the Device Emulator and the Windows CE Remote Registry Editor.

  4. In the Registry Editor, expand Windows Mobile 5.0 Pocket PC Emulator, and then create the following key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsot\.NETCompactFramework\Managed Debugger.

    Create the key by right-clicking .NETCompactFramework, pointing to New, and then clicking Key.

    Note that there is a space between "Managed" and "Debugger".

  5. Create a DWORD named AttachEnabled.

    Create the DWORD by right-clicking Managed Debugger, pointing to New, and then clicking DWORD Value.

  6. Set the Name as AttachEnabled, and the Value as 1.

    Note

    Setting this device debug key significantly reduces performance. When you are not debugging, disable this functionality by resetting the data value to 0.

  7. Leave the Device Emulator open for the remaining steps to preserve the registry setting. You can close the Registry Editor.

Launching the Application

Your next step is to start the InteropSolution application.

To start the application

  1. Open the solution you created in Walkthrough: Hello World: A COM Interop Example for Smart Devices.

    Ensure that Windows Mobile 5.0 Pocket PC Emulator appears in the Target Device box on the toolbar.

  2. On the Visual Studio Debug menu, click Start Debugging or press F5.

    This step immediately deploys the native project, HelloCOMObject, to the emulator without additional user intervention.

  3. When the Deploy SayHello dialog box opens, select Windows Mobile 5.0 Pocket PC Emulator, and then click Deploy.

    This step deploys the managed project.

    The application opens in the emulator. Do not click the button yet.

Setting a Breakpoint in the Native Code

Your next step is to set a breakpoint in the native code to prepare for attaching the native debugger.

To set a breakpoint in the native code

  1. In Solution Explorer, right-click Hello.cpp, and then click View Code.

  2. Insert a breakpoint at the line that starts *text by clicking in the left margin of the Code Editor.

    The breakpoint symbol appears as an empty circle with an exclamation point that indicates that the breakpoint cannot currently resolve. This is because it lacks the appropriate symbols and sources at this point.

  3. On the Visual Studio Debug menu, point to Windows, and then click Modules.

    The Modules window displays all the modules loaded to this point, for example, the managed application SayHello.exe. Note that the native HelloCOMObject.dll has not yet been loaded, because you have not yet clicked the button in the application.

Attaching with the Native Debugger

Your next step is to detach the managed debugger so that you can attach with the native debugger. Recall that both debuggers cannot be attached at the same time for device projects. These are the steps that you would use any time that you have to switch from the managed to the native debugger.

To attach with the native debugger

  1. On the Visual Studio Debug menu, click Detach All.

    This step detaches the managed debugger but enables the application to continue to run.

  2. On the Debug menu, click Attach to Process.

  3. In the Transport box, select Smart Device.

  4. To populate the Qualifier box, click Browse.

  5. In the Connect to Device dialog box, select Windows Mobile 5.0 Pocket PC Emulator, and then click Connect.

  6. To populate the Attach to box, click Select.

  7. In the Select Code Type dialog box, select Debug these code types, clear the Managed check box, select the Native check box, and then click OK.

  8. In the Available Processes box, select SayHello.exe, and then click Attach.

    You are now attached with the native debugger.

Running to the Native Breakpoint

You are now ready to advance to the breakpoint you set in the native code. When you examine the Modules window again, you see native modules now present. However, HelloCOMObject.dll is not yet loaded because you have not yet clicked button1.

Note

If you have run this walkthrough earlier, debug symbols might already be loaded, and you can skip those steps. If not, the following section provides steps for loading them.

To advance execution to the native breakpoint

  1. On the Device Emulator form, click button1.

    The Hello World! message appears on the form, and hellocomobject.dll appears in the Modules window.

    If the Symbol Status column for hellocomobject.dll does not show Symbols loaded, follow these steps:

    1. Right-click hellocomobject.dll, and then click Load Symbols.

    2. In the Find Symbols dialog box, navigate to InteropSolution\HelloCOMObject\Windows Mobile 5.0 Pocket PC SDK (ARMV4I)\Debug\HelloCOMObject.pdb.

    3. Click Open.

      The Symbol Status column changes to Symbols loaded, and the breakpoint indicator now shows the breakpoint as resolved.

  2. On the form on the Device Emulator, click OK on the Hello World! window, and then click button1 again.

    The breakpoint indicator shows that execution has stopped at the breakpoint.

  3. On the Debug menu, click Step into or press F11.

    Note that execution moves to the next line. This shows that you can now step through the native section of your solution.

Attaching with the Managed Debugger

Your next step is to detach the native debugger so that you can attach to the managed debugger. Recall that both debuggers cannot be attached at the same time for device projects. These are the steps that you would use any time that you have to switch from the native to the managed debugger.

To attach with the managed debugger

  1. On the Visual Studio Debug menu, click Detach All.

    This step detaches the native debugger, but the application continues to run.

  2. On the Debug menu, click Attach to Process, and ensure that the Transport box contains Smart Device.

  3. Populate the Qualifier box by clicking Select, selecting Windows Mobile 5.0 Pocket PC Emulator, and then clicking Connect.

  4. To populate the Attach to box, click Select, select Debug these code types, check the Managed box, clear the Native box, and then click OK.

    If a message appears reminding you that managed and native debugging are not compatible, click OK.

  5. In the Available Processes box, select SayHello.exe, and then click Attach.

    Now the managed debugger is attached.

Setting a Breakpoint in the Managed Code

Your next step is to set a breakpoint in the managed code to prepare for attaching the managed debugger.

To set a breakpoint in the managed code

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

  2. Insert a breakpoint at the line string text;.

Running to the Managed Breakpoint

You are now ready to advance to the breakpoint you set in the managed code.

To advance execution to the managed breakpoint

  • In the Device Emulator, click button1.

    Execution stops at the breakpoint.

Conclusion

For performance reasons, remember to reset the device registry key to 0 when you no longer have to attach the managed debugger to an already running process.

See Also

Tasks

How to: Attach to Managed Device Processes

How to: Change Device Registry Settings

Other Resources

Debugging Device Projects

Debugging in Visual Studio