Workflow for converting interop definitions
mainFollow these steps to migrate interop code to CsWin32:
- Locate manual interop: Search for
DllImport,MarshalAs,StringBuilder,Win32PInvoke, orVanarausage. - Update
NativeMethods.txt: Add the function and all dependent structs, enums, or COM interfaces tosrc/Files.App.CsWin32/NativeMethods.txt. - Regenerate signatures: Build the CsWin32 project to refresh the generated code.
- Update callers: Use generated APIs directly. Prefer safe overloads like
Span<char>,SafeHandle,ComPtr<T>, and generated enums/structs. Use unsafe raw overloads only when necessary (e.g., for specific pointer requirements). - Cleanup: Remove the manual definitions only after all callers have been updated.
- Verify: Build the project and ensure no
error CS*(C# compiler) errors exist.
# 1. Locate manual interop
git grep -n "DllImport\|MarshalAs\|StringBuilder" -- src
git grep -n "Win32PInvoke\." -- src/Files.App
git grep -n "using Vanara\|Vanara\.PInvoke\|Kernel32\.|\|Shell32\.|\|User32\." -- src/Files.App
# 2. Add to NativeMethods.txt (Example entries)
# RmStartSession
# RM_PROCESS_INFO
# SHBrowseForFolder
# 3. Build CsWin32 project to refresh signatures
dotnet build src/Files.App.CsWin32/Files.App.CsWin32.csproj -c Debug -p:Platform=x64
# 5. Verify callers are gone before deleting manual definitions
git grep -n "Win32PInvoke\.RmStartSession" -- src/Files.App