Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WriteFile函数生成1(成功),但返回87的错误代码_C#_Marshalling - Fatal编程技术网

C# 使用WriteFile函数生成1(成功),但返回87的错误代码

C# 使用WriteFile函数生成1(成功),但返回87的错误代码,c#,marshalling,C#,Marshalling,我目前正在使用WriteFile函数来禁止看门狗在嵌入式系统中消失。该函数实际上可以工作,但同时返回错误代码87。我尝试调试函数中的参数,但仍然得到错误代码 Marshalling part: [DllImport("coredll.dll", SetLastError = true)] private static extern int WriteFile(IntPtr hwnd, byte[] lpInBuffer, uint nNumberOfBytesToWrite, ref

我目前正在使用WriteFile函数来禁止看门狗在嵌入式系统中消失。该函数实际上可以工作,但同时返回错误代码87。我尝试调试函数中的参数,但仍然得到错误代码

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

returnWatchDogTouch的返回值为1,后续行中返回错误代码87。非常感谢您的帮助。

GetLastError
在函数成功时不一定由函数设置。在这种情况下,有些函数会将值设置为零,有些函数则不会


在您调用的函数指示失败后,您应该只调用
GetLastError
来获取更具体的错误信息。在您的情况下,
WriteFile
返回1表示成功。

如果函数成功,则不一定设置最后一个错误代码。也就是说,仅当调用的函数指示失败时,才应
GetLastError
。@jlahd添加更多描述并将其作为答案。我明白了,我一直认为GetLastError在任何情况下都会响应前面的语句。谢谢你的解释。