[Windows Phone 7 Development and Hacking] Martin7Pro:
Kernel mode universal library, usable from GCC for any code part kernel mode running

There is updated version. You can see principles and extract HaRET parts needed to run in kernel mode to simple pure C dll, like SimpleKernelDll1.dll in source zip (it contains VS project file, but you can make totally simple makefile for other compilers. Dll must contain no UI/API calling. If you want to make any UI results, use your own Output structure to marshall results to user mode). Calling is exampled in testing appplication,

Calling description:

HaRET user mode application (filled Input structure, defined Output structure, filled dll and function names) --> KMDriverWrapper.dll --> (Marshalled Input structure) KMDriver.dll --> HaRETKernelPart.dll (called wanted function, used Input structure, filled Output structure) --> return to KMDriver.dll (Results unmarshalled) --> KMDriverWrapper.dll (TRACE Output added) --> return to HaRET user mode app (used Output structure, possibly displayed TRACE Output).

It is usable for every (not only HaRET) code Kernel mode running, but use it very carefully before full testing will finished. I will prepare managed wrapper too to be usable from within WP7DllImport project in future.

...

Output is now:

StructOut1 filled, KernelDllFn1 returns ERROR_SUCCESS

This means own function from separated pure C dll is called in Kernel Mode from independent user mode appliation.

You can see result from Kernel mode code part on testing application:

How to use (still unfinished tutorial):

Copy KMDriverWrapper.lib to your (HaRET or other) application project source directory.

Add KMDriverWrapper.lib to your project linking.

Copy Headers and Inc subdirectories to your application project source directory.

Include needed headers to your project:

#include "..\Headers\SimpleKernelDll1.h" // Your own definition of exported functions and marshaled structures.
#include "..\inc\KMDriverAPI.h"

Change your dll specific definitons in Headers subdirectory.

Deploy KMDriver.dll and KMDriverWrapper.dll to phone \Windows directory from Bin subdirectory of attached zip file.

Use _TestDriver\KMDriver.reg file or register kernel driver in registry editor. Reboot is not needed, when you use driver loading from next line.

Call WP7InitDriverAPI(); and WP7LoadDriver(L"Drivers\\BuiltIn\\KMDriver"); functions from your application.

WP7DeinitDriverAPI(); calling is needed, WP7UnloadDriver(L"KMD1:"); calling is recomended on end of application (unload driver when it is not used from more applications together and you want to be able driver dll changing in OS runtime).

Call WP7RunInKernelMode(YourDllName, YourFunctionName, InStructurePointer, InStructureLength, OutStructurePointer, OutStructureLength, TraceOutBuffer, TraceOutBufferLength); function from your application.

--- It is predefined as RunKernelDllFn1(...); in example Headers\SimpleKernelDll1.h header.

Deploy and start your application.

Little bonus (thanks to Bruce Eitman opensource API):

If you know, what is GPIO, you can use driver wrapper other exported functions:

BOOL WP7SetGPIOPin( DWORD PinNumber );
BOOL WP7ClearGPIOPin( DWORD PinNumber );
BOOL WP7SetInputGPIOPin( DWORD PinNumber );
BOOL WP7SetOutputGPIOPin( DWORD PinNumber );
BOOL WP7ReadGPIOPin( DWORD PinNumber );

Debug driver version writes TRACE output to \DriverOut.txt file, SimpleKernelDll1.dll example writes TRACE output to \DllOut.txt file. Remember to clear them sometime to save phone space.

You can also try precompilled SimpleKernelDll1.dll and WP7TestDriverApp.exe from zip Bin subdirectory to your unlocked phone.