Export (0) Print
Expand All

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 2005 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 whichever debugger is required for a certain section, for example, a managed section, then detach that debugger and attach the other when the other is needed. You can repeat these detach/attach steps as often as necessary to step through your program.

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

  • Enabling Attachment of the Managed Debugger

  • Launching 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

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

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, then, is to set the device to allow the managed debugger to attach to an already running process. You do this by setting a registry key on the device.

NoteNote

Setting the key affects only attaching to already-running managed processes. It does not affect launching a project 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 Visual Studio 2005, point to Visual Studio Remote Tools, and then click Remote Registry Editor.

  2. In the Select a Windows CE Device window, expand Pocket PC 2003, and then click Pocket PC 2003 SE 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 Pocket PC 2003 SE 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.

    NoteNote

    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.

Your next step is to launch the InteropSolution application.

To launch the application

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

    Ensure that Pocket PC 2003 SE 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 further user intervention.

  3. When the Deploy SayHello dialog box opens, select Pocket PC 2003 SE Emulator, and then click Deploy.

    This step deploys the managed project.

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

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 on the shortcut menu, click View Code.

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

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

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

    The Modules window displays all the modules loaded so far, 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.

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 you would use any time you need 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 allows the application to continue running.

  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 Pocket PC 2003 SE 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.

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

NoteNote

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 shows No symbols loaded, follow these steps:

    1. Right-click hellocomobject.dll, and then on the shortcut menu, click Load Symbols.

    2. In the Find Symbols dialog box, navigate to InteropSolution\HelloCOMObject\Pocket PC 2003 (ARMV4)\Debug\HelloCOMObject.pdb.

    3. Click Open.

      The Symbol Status column changes from No symbols loaded 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 portion of your solution.

Your next step is to detach the native debugger so that you can attach with the managed debugger. Recall that both debuggers cannot be attached at the same time for device projects. These are the steps you would use any time you need 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, then selecting Pocket PC 2003 SE Emulator, and then clicking Connect.

  4. To populate the Attach to box, click Select, then select Debug these code types, then 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.

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 on the shortcut menu, click View Code.

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

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.

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

Community Additions

ADD
Show:
© 2014 Microsoft