[Windows Phone 7 Development and Hacking] Martin7Pro:
Hi friends.

There is simple tutorial. We use WP7 DllIprort Project, or another native Dlls, to enable our managed (mostly Silverlight) applications to do any system calling. But, debuging of hybrid applications is difficult. May be, somebody is able to make publicly usable source to use standard Visual Studio way to test and debug native Dlls, but Ultrashot and others have no time, they have important things to do.

Then, I prepared simple library, which export native TRACE macro and managed wrapper too.

Using:

1. Copy all content from attachment NativeDebugTraceVS2008 directory to your VS2008 native dll project directory.

2. Add NativeDebug2008.dll and NativeDebugTrace.h to your native dll project.

3. Add line:

#include "NativeDebugTrace.h"

on top of your native c++ source code.

4. Use TRACE macro in your code. Syntax is usual, example:

...

int nVal = 1+1;

TRACE(L"%s = %d", L"nVal", nVal);

...

Output is showed in "normal" VS2008 Output window too, when you debug under VS2008.

If you have used another TRACE macro in your code, you can rename TRACE macro to ANY_OTHER_TRACE_NAME in NativeDebugTrace.h and call this another macro.

5. Copy all content from attachment NativeDebugTraceVS2010 directory to your VS2010 managed application or dll project directory.

6. Add both NativeDebug2010.dll and NativeDebug2008.dll to your project, set Copy if newer its option.

7. Add reference to NativeDebug2010.dll to your project.

8. Add Initialisation to your App constructor:

...
public App()
{
...
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
...
NativeDebug.Init();
}
else
{
NativeDebug.RegisterOnly();
}
}
...

I am not sure why, but RegisterOnly() calling is necessary to work without debugging, your native dll will not load properly instead.

9. Add garbage release to your App closing:

...
private void Application_Closing(object sender, ClosingEventArgs e)
{
NativeDebug.Destroy();
...
}
...

10. Change your VS2010 view to Expert (Menu/Tools/Settings/Expert Settings).

11. Open your Output window on top (Menu/View/Output, click to Window Position/Float).

Now you can see all managed System.Diagnostics.Debug.WriteLine and native TRACE ouput in one Output window. Attention - option "Menu/Tools/Options/Debugging/Redirect all Output text to Immediate window" does not work for me.

You can also write output to TRACE file by standard way (see System.Diagnostics.Debug.WriteLine help).

Attention: Debug Trace Library is fully synchronised, but I did not test it to debug more then one application alternately. May be any deadlock can occure, but I beleive will not. When your managed code use another unicode string prefix then L"...", try to change calling, and write your experience here.

See example picture, follow red text:

Picture is from old version, included to W.I.N.C.O native library. But, yestarday I have got total hard disc crash (:crying::crying::crying:), all my programs from long time are lost. I must programm this feature again today, I believe it is better then last weak:o.

Note:

Attachment is functional, but will be changed in future. Little COM mishmash and WP7 WatchDog problems occured.

Next code probably solve it, but I am not sure. Do not forget to set HKLM\Software\Microsoft\TaskHost\DehydrateOnPause to 0 first, when you want to debug application calling ShellExecuteEx() etc.

private void Application_Activated(object sender, ActivatedEventArgs e)
{
NativeDebug.Start();
}

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
NativeDebug.Stop();
}