Question

I am currently using the WriteFile function to prohibit the watchdog from elapsing in the embedded system. The function actually works, but at the same time it returns an error code of 87. I tried debugging the parameters in the function, yet i still get the error code.

Marshalling part:    
[DllImport("coredll.dll", SetLastError = true)]
private static extern int WriteFile(IntPtr hwnd, byte[] lpInBuffer, uint nNumberOfBytesToWrite,  ref uint lpNumberOfBytesWritten, IntPtr lpOverlapped);

Usage:
byte[] Arg = new byte[1];
Arg[0] = 0x87;
uint WrittenByte = 0;
int returnWatchDogTouch = 0;

returnWatchDogTouch = WriteFile(InitDevices.DeviceHandleWDT, Arg, 1,ref WrittenByte, IntPtr.Zero);
int error = Marshal.GetLastWin32Error(); //Getting error value 87 in this line

the return value of returnWatchDogTouch is 1, and the error code of 87 is returned in the subsequently line. Any help is appreciated, thanks.

Was it helpful?

Solution

GetLastError is not necessarily set by functions when they succeed. Some functions do set the value to zero in that case, some do not.

You should only call GetLastError to get more specific error information after a function you call indicates failure. In your case, WriteFile returns 1 for success.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top