DllImport - Timer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

The C# version of CreateWaitableTimer and SetWaitableTimer should be
System.Threading.Timer.

1. Can this timer wake the system from standby / hibernate? I miss the
parameter "fResume". It is true, if the system should be restored.

BOOL WINAPI SetWaitableTimer(
HANDLE hTimer,
const LARGE_INTEGER* pDueTime,
LONG lPeriod,
PTIMERAPCROUTINE pfnCompletionRoutine,
LPVOID lpArgToCompletionRoutine,
BOOL fResume
);

2. If I try to import the Kernel32 functions it is working for
CreateWaitableTimer but I have a problem with the SetWaitableTimer function -
the parameters are showing the wrong signature:

[DllImport("Kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateWaitableTimer(IntPtr lpSecurityAttributes,
bool manualReset, string timerName);

Wrong signature:

[DllImport("Kernel32.dll", SetLastError = true)]
public static extern bool SetWaitableTimer(IntPtr hTimer, LARGE_INTEGER
dueTime, long lPeriod, IntPtr pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);

dueTime should be a pointer ... ?

Does anyone know what I should do?

Thanks a lot

Ralf
 
Ralf,
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern bool SetWaitableTimer(IntPtr hTimer, LARGE_INTEGER
dueTime, long lPeriod, IntPtr pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);

dueTime should be a pointer ... ?

Does anyone know what I should do?

Change to

...., ref LARGE_INTEGER dueTime, int lPeriod, ...


Mattias
 
Hi Mattias!

Thanks - now its some how obvious ... :o)

The LARGE_INTEGER is defined in DTS as I found out (but I didn't find it).

I defined it as:
public struct LARGE_INTEGER
{
public Int64 LowPart;
public long HighPart;
};

This is working - but is it OK this way or would it be better differently?
Any opinion?

Thanks a lot

Ralf


Mattias Sjögren said:
Ralf,
[DllImport("Kernel32.dll", SetLastError = true)]
public static extern bool SetWaitableTimer(IntPtr hTimer, LARGE_INTEGER
dueTime, long lPeriod, IntPtr pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);

dueTime should be a pointer ... ?

Does anyone know what I should do?

Change to

...., ref LARGE_INTEGER dueTime, int lPeriod, ...


Mattias
 
Ralf,
I defined it as:
public struct LARGE_INTEGER
{
public Int64 LowPart;
public long HighPart;
};

This is working - but is it OK this way or would it be better differently?
Any opinion?

That's too big, it should be

public struct LARGE_INTEGER
{
public uint LowPart;
public int HighPart;
}

You can also substitute it for a single (u)long if that's more
convenient.

But the SetWaitableTimer says it actually represents a FILETIME in
this case so I'd use that. It's provided in the
System.Runtime.InteropServices namespace.


Mattias
 
Hi Mattias!

In Windows API it is defined as

typedef union _LARGE_INTEGER
{
struct
{
DWORD LowPart;
LONG HighPart;
}
....
LONGLONG QuadPart;
}

Wouldn't this be allright than?

struct LARGE_INTEGER
{
public ulong LowPart;
public long HighPart;
}

I didn't find it in InteropServices? How did you get it?

Thanks a lot

Ralf
 
Hi Mattias!

Sorry - you are right. I got confused. long is too long ;-)

Would it be OK to define it like this:

public class LARGE_INTEGER
{
public uint LowPart
{
get { return (uint)(quadPart & 0xFFFFFFFF);}
}
public int HighPart
{
get {return (int)(quadPart >> 32);}
}
private Int64 quadPart;
public Int64 QuadPart
{
set { quadPart = value;}
get { return quadPart;}
}
}

In this case (as a class is supplied as a ref) the signature of
SetWaitableTimer would be:

public static extern bool SetWaitableTimer(IntPtr hTimer, /*no ref because
of class:*/LARGE_INTEGER dueTime, int lPeriod, IntPtr pfnCompletionRoutine,
IntPtr lpArgToCompletionRoutine, bool fResume);

Is this right? Or am I mistaken again ...?

Thanks a lot

Ralf
 
Back
Top