PInvoke.FindFirstFileExFromApp does not set last error code
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.5k
- Forks
- 124
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 9
Description
Actual behavior
PInvoke.FindFirstFileExFromApp does not set last error code when return value is HANDLE.INVALID_HANDLE_VALUE. According to official document:
If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
Expected behavior
Marshal.GetLastWin32Error() returns correct error code after PInvoke.FindFirstFileExFromApp().
Repro steps
NativeMethods.txtcontent:
FindClose
FindFirstFileExFromApp
FindNextFile
INVALID_HANDLE_VALUE
- Any of your own code that should be shared?
private static unsafe HANDLE FindFirstFileExFromApp(
string lpFileName, FINDEX_INFO_LEVELS fInfoLevelId,
out WIN32_FIND_DATAW findData, FINDEX_SEARCH_OPS fSearchOp,
uint dwAdditionalFlags)
{
fixed (char* lpFileNameLocal = lpFileName)
{
WIN32_FIND_DATAW findDataLocal;
HANDLE handle = PInvoke.FindFirstFileExFromApp(lpFileNameLocal, fInfoLevelId, &findDataLocal, fSearchOp, default, dwAdditionalFlags);
if (handle == HANDLE.INVALID_HANDLE_VALUE)
{
int errorCode = Marshal.GetLastWin32Error(); // returns 0
Logger.I(TAG, $"Unable to access '{lpFileName}' ({errorCode})");
}
findData = findDataLocal;
return handle;
}
}
Context
- CsWin32 version: 0.3.264
- Win32Metadata version (if explicitly set by project):
- Target Framework: net8.0-windows10.0.22621.0
LangVersion(if explicitly set by project): [e.g.9]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the NativeMethods.txt entries and the PInvoke.FindFirstFileExFromApp call shown in the reproduction, then reproduce the failure on net8.0-windows10.0.22621.0. Done means a failed call makes Marshal.GetLastWin32Error() return the corresponding Windows error code instead of 0.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- devtools, operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100