Error 998 on Windows API Call
-
The TrackMouseEvent function fails and returns zero. So I called (as msdn advised me) GetLastError() to get the error msg and it returns 998 which means: ERROR_NOACCESS Invalid access to memory location. Has anyone a clue what my OS is trying to tell me here? I guess the error is caused by the c++ struct which the function takes as parameter. ( TRACKMOUSEEVENT Struct on MSDN )
[DllImport("kernel32.dll", SetLastError=true)] static extern int GetLastError (); [DllImport("user32")] public static extern bool TrackMouseEvent(TRACKMOUSEEVENT lpEventTrack); [StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)] public struct TRACKMOUSEEVENT { public long cbSize; public long dwFlags; public long hwndTrack; public long dwHoverTime; } and in the C# App Constructor: TRACKMOUSEEVENT foo = new TRACKMOUSEEVENT(); foo.cbSize = Marshal.SizeOf(typeof(TRACKMOUSEEVENT)); foo.dwFlags = TME_NONCLIENT; foo.hwndTrack = (long)this.Handle; bool bar = TrackMouseEvent(foo); MessageBox.Show(""+GetLastError());
-
The TrackMouseEvent function fails and returns zero. So I called (as msdn advised me) GetLastError() to get the error msg and it returns 998 which means: ERROR_NOACCESS Invalid access to memory location. Has anyone a clue what my OS is trying to tell me here? I guess the error is caused by the c++ struct which the function takes as parameter. ( TRACKMOUSEEVENT Struct on MSDN )
[DllImport("kernel32.dll", SetLastError=true)] static extern int GetLastError (); [DllImport("user32")] public static extern bool TrackMouseEvent(TRACKMOUSEEVENT lpEventTrack); [StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)] public struct TRACKMOUSEEVENT { public long cbSize; public long dwFlags; public long hwndTrack; public long dwHoverTime; } and in the C# App Constructor: TRACKMOUSEEVENT foo = new TRACKMOUSEEVENT(); foo.cbSize = Marshal.SizeOf(typeof(TRACKMOUSEEVENT)); foo.dwFlags = TME_NONCLIENT; foo.hwndTrack = (long)this.Handle; bool bar = TrackMouseEvent(foo); MessageBox.Show(""+GetLastError());
TyronX wrote: [StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)] public struct TRACKMOUSEEVENT { public long cbSize; public long dwFlags; public long hwndTrack; public long dwHoverTime; } Well, according to the docs, these four fields should be DWORDS, or unsigned 32-bit integers. In your C# code, you're using
long
, which is a 64 bit signed integer. Change yourlong
's toint
's oruint
's and it should work. Also, change yourhwndTrack
type fromlong
toIntPtr
. Then it should work.[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct TRACKMOUSEEVENT {
public int cbSize;
public int dwFlags;
public IntPtr hwndTrack;
public int dwHoverTime;
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
TyronX wrote: [StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)] public struct TRACKMOUSEEVENT { public long cbSize; public long dwFlags; public long hwndTrack; public long dwHoverTime; } Well, according to the docs, these four fields should be DWORDS, or unsigned 32-bit integers. In your C# code, you're using
long
, which is a 64 bit signed integer. Change yourlong
's toint
's oruint
's and it should work. Also, change yourhwndTrack
type fromlong
toIntPtr
. Then it should work.[StructLayout( LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct TRACKMOUSEEVENT {
public int cbSize;
public int dwFlags;
public IntPtr hwndTrack;
public int dwHoverTime;
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome