Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use WatchDog and KernelLibIoControl correctly

77 views
Skip to first unread message

matvdl

unread,
Mar 17, 2009, 8:30:01 PM3/17/09
to
I need some help understanding how this works. I am not that familiar with
C code and I am calling this functiuon from within vb.net to operate the
software watchDog on a winCEDevice.

When you call KernelLibIoControl with the correct parameters what does it
return??

The document indicates that it returns a boolean - but the code I have seen
used returns a handle - I have than found code that allows us to
waitForSingle on this handle for when the WatchDog is actuall triggered. Is
someone able to explain if this is correct or if I have it completly wrong.

In addition to this I have noticed that the action that you are taking with
calling KernelLibIoControl is controlled by the second last parameter call
nOutBufSize. It is in this parameter that you pass in if we are starting the
watchDog, refreshing it etc.

Some of the Code

DECLARATION:
Private Declare Function KernelLibIoControl Lib "coredll.dll" (ByVal hLib As
IntPtr, _
ByVal dwIoControlCode As UInteger, _
ByRef lpInBuf As WDAPIStruct, _
ByVal nInBufSize As UInteger, _
ByVal lpOutBuf As IntPtr, _
ByVal nOutBufSize As UInteger, _
ByVal lpBytesReturned As IntPtr) As IntPtr


CREATING THE WATCH DOG - Notice the WDAPI_START
hWatchDog = KernelLibIoControl(New IntPtr(KMOD_CORE),
IOCTL_KLIB_WDOGAPI, wdas, _
CType(Marshal.SizeOf(wdas), UInteger), IntPtr.Zero,
WDAPI_START, IntPtr.Zero)

Is the hWatchDog actually a handle or a boolean True false indicating sucess.

If it is a handle can I than wait on this handle as in following

waitThread = New Threading.Thread(AddressOf WaitThreadStart)
waitThread.Start()

Private Shared Sub WaitThreadStart()
Dim Res As Integer = WaitForSingleObject(hWatchDog, INFINITE)
End Function

Thanks for your help
--
matthew

--
matthew

Luca Calligaris

unread,
Mar 18, 2009, 3:37:35 AM3/18/09
to
KernelLibIoControl returns a boolean value. I've never seen
IOCTL_KLIB_WDOGAPI/WDAPI_START before: maybe is something specific to the
BSP you're using. The standard API to manage kernel watchdog is
CreateWatchdDogTimer (http://msdn.microsoft.com/en-us/library/aa910693.aspx)
and the other functions of the family

--

Luca Calligaris (MVP-Windows Embedded)
http://geekswithblogs.net/WindowsEmbeddedAnnotations
l.calliga...@eurotech.it.nospam
www.eurotech.it

"matvdl" <mat...@discussions.microsoft.com> ha scritto nel messaggio
news:B35377AC-C199-4A67...@microsoft.com...

matvdl

unread,
Mar 18, 2009, 5:34:05 AM3/18/09
to
So - the KernelLibIoControl is a way of accessing functions and features that
are within the kernel - or generally out of reach - how the internal code
uses the parameters passed to it is up to the code - and the data that is
passed back to the calling function appears to be customizable - would this
be correct.

I have reviewed my code and this function does returns a Handle - and to
control the Watchdog you do pass your instructions into the second last
parameter that is meant to be nOutBufSize.

Unless I have got something very wrong - this makes understanding code very
very difficult - it appears that the function has been used in an entirely
different way to how it was intended to be used.

Interested to here from people if this is correct.

--
matthew

Tony Hedge

unread,
Mar 18, 2009, 7:28:11 AM3/18/09
to
Watchdogs are a bit of a problem area! It is all down to how the OEM
decides too implement them, if at all.

In your case, your OEM has decided to use KernelLibIoControl, and it
seems from what you say that it is rather unconventional in returning a
handle to something. Nobody but the OEM can say what use you can make of
the handle, unfortunately.

Some OEM's implement their own watchdog control via a DLL of their own,
especially if they are using an "external" (to the microcontroller)
hardware watchdog.

Some OEM's *may* even implement the official SetWatchDogTimer etc API to
which Luca referred you. But I've got two boards from different OEM's
on my bench right now which don't (though they return valid handles etc)!

Unfortunately only your OEM is likely to be of much help, unless you
have the relevant BSP source code .

Tony

Tony Hedge

unread,
Mar 18, 2009, 7:44:30 AM3/18/09
to
Ooops - sorry, let me retract that bit about the software watchdog API
(CreateWatchDogTimer, SetWatchDogTimer etc) not being implemented by
some OEM's. I've just got it to work correctly on both. By the way, Luca
modestly forgot to point out that there is an excellent article on how
the WatchDog API works on his own blog!

http://geekswithblogs.net/WindowsEmbeddedAnnotations/Default.aspx

Apologies if I misled anyone! And thanks to Luca for his blog article -
it certainly helped me fix mine!

Tony

Luca Calligaris

unread,
Mar 18, 2009, 11:00:12 AM3/18/09
to
It was not modesty: I actually wrote the blog post after reading this
newsgroup post!

--

Luca Calligaris (MVP-Windows Embedded)
http://geekswithblogs.net/WindowsEmbeddedAnnotations
l.calliga...@eurotech.it.nospam
www.eurotech.it

"Tony Hedge" <tonyatbenthicsciencesdotcodotuk> ha scritto nel messaggio
news:O3iin77p...@TK2MSFTNGP06.phx.gbl...

matvdl

unread,
Mar 18, 2009, 11:47:28 AM3/18/09
to
Thanking you all very much - I have had a read through the blog - and I think
it helped.

I am new to C++ and much of what I see can be confusing - I hope that this
situation is unusual because I found trying to understand it very difficult.

A few questions:
The KernelLibIoControl function is defined in Microsoft literature as
returning as a boolean - in vb.net if something is defined as boolean - it
returns a boolean - is C++ different - obviously what something is defined as
returning is not set in stone when using C++.

Also - this applies with what is passed in to a function - something that is
defined as an nOutBufSize has been used as something completely different??

I spent a few hours for looking for anything that pointed to how functions
defined as boolean can return a handle - couldn't find anything.

Also
what is a OAL

Another questions - the reason that the KernelLibIoControl is used is
because the calls that it makes (CreateWatchDogTimer in the background) are
protected within the kernel or similar - therefore I would not be able to
call CreateWatchDogTimer from my .net app - is this correct?

Hopefully I don't sound too stupid - but just trying to understand.

Thanks very much for your help and input so far - it has been great.

Matthew

--
matthew

matvdl

unread,
Mar 18, 2009, 11:47:30 AM3/18/09
to
Thanking you all very much - I have had a read through the blog - and I think
it helped.

I am new to C++ and much of what I see can be confusing - I hope that this
situation is unusual because I found trying to understand it very difficult.

A few questions:
The KernelLibIoControl function is defined in Microsoft literature as
returning as a boolean - in vb.net if something is defined as boolean - it
returns a boolean - is C++ different - obviously what something is defined as
returning is not set in stone when using C++.

Also - this applies with what is passed in to a function - something that is
defined as an nOutBufSize has been used as something completely different??

I spent a few hours for looking for anything that pointed to how functions
defined as boolean can return a handle - couldn't find anything.

Also
what is a OAL

Another questions - the reason that the KernelLibIoControl is used is
because the calls that it makes (CreateWatchDogTimer in the background) are
protected within the kernel or similar - therefore I would not be able to
call CreateWatchDogTimer from my .net app - is this correct?

Hopefully I don't sound too stupid - but just trying to understand.

Thanks very much for your help and input so far - it has been great.

--
matthew

Chris Tacke, eMVP

unread,
Mar 18, 2009, 12:01:32 PM3/18/09
to
> A few questions:
> The KernelLibIoControl function is defined in Microsoft literature as
> returning as a boolean - in vb.net if something is defined as boolean - it
> returns a boolean - is C++ different - obviously what something is defined
> as
> returning is not set in stone when using C++.

No, it returns a BOOL, which is defined as 4-bytes. In managed code it's an
int. It should only be declared as a bool if you decorate it with a
MarshalAs attribute (which is actully how you should define it). What it
returns *is* set in stone. It's a 4-byte value. Since it's a BOOL, it
should be either 0 or non-zero equating to false or true.

> Also - this applies with what is passed in to a function - something that
> is
> defined as an nOutBufSize has been used as something completely
> different??

No. nOutBufSize is a DWORD, which is a 4-byte unsigned number. Since the
high bit is really unlikely to be used, it is also an int.

> I spent a few hours for looking for anything that pointed to how functions
> defined as boolean can return a handle - couldn't find anything.

A HANDLE is a 4-byte number, just like a BOOL, so there's nothing that says
you *can't* return a HANDLE (or any other 4-byte thing) but it's absolutely,
positively wrong for whoever implemented that specific IOCTL to be using it
that way. If they wanted to return a handle, that's what the out buffer
parameter is for.

> what is a OAL

OEM Adaptation Layer. Analogous to the HAL on a PC.

> Another questions - the reason that the KernelLibIoControl is used is
> because the calls that it makes (CreateWatchDogTimer in the background)
> are
> protected within the kernel or similar - therefore I would not be able to
> call CreateWatchDogTimer from my .net app - is this correct?

You shouldn't be calling KernelLibIoControl from an app *period*. If you
need to make a call into the kernel, it should be with KernelIoControl (note
the lack of 'Lib' in there). The reason you would call this is because the
OEM is lazy and didn't provide you with a nicer, more protected API. In CE
6.0 this call may get closed on you anyway becasue unless the OEM added an
exclusion for the IOCTL, the app probably can't call it anyway.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


matvdl

unread,
Mar 18, 2009, 12:43:41 PM3/18/09
to
Thankyou for your response - it is very helpfull.

So this means simply that this is a nasty hack - and as I get more involved
in C++ this should not occour that often??

I hope not as it is all very confusing

Thanks
--
matthew

Chris Tacke, eMVP

unread,
Mar 18, 2009, 12:55:31 PM3/18/09
to
> So this means simply that this is a nasty hack - and as I get more
> involved
> in C++ this should not occour that often??

Yes, it's a hack and I wouldn't use it. It's very brittle and prone to
breaking the next time you update the device OS.

Luca Calligaris

unread,
Mar 19, 2009, 9:19:27 AM3/19/09
to
Just an update: IOCTL_KLIB_WDOGAPI is actually defined in
%_WINCEROOT%\PUBLIC\COMMON\oaK\INC\pkfuncs.h but it is not referenced
anywhere (specifically KernelLibIoControl does not care of it)

--

Luca Calligaris (MVP-Windows Embedded)
http://geekswithblogs.net/WindowsEmbeddedAnnotations
l.calliga...@eurotech.it.nospam
www.eurotech.it

"matvdl" <mat...@discussions.microsoft.com> ha scritto nel messaggio
news:01BF0275-A39E-4072...@microsoft.com...

0 new messages