What exceptions should I expect from unsafe P / Invoke code?

In my solution, I wrote the following:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern unsafe bool CopyFileEx(string lpExistingFileName,  
 string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData,  
 Boolean* pbCancel,    CopyFileFlags dwCopyFlags);

...

bool result;

unsafe{
  result = CopyFileEx(sourceFile, targetFile, null, IntPtr.Zero,  
                      null /* pointer */, 0);
}

if(!result)
    Win32Exception exc = new Win32Exception(Marshal.GetLastWin32Error()); 
// parameter could be omitted according to Win32Exception constructor 
// implementation

Assuming CopyFileEx was exported with the SetLastError = true parameter of the DllImport attribute, do I have a chance to get any undefined exception?

I'm specifically interested in non-CLR exceptions that are wrapped in an instance of RuntimeWrappedException. In C ++, "throw 1" is a valid construct. So, what exceptions should I expect from such P / Invoke calls, and where can I get such exception information (MSDN doesn't say anything about exceptions from CopyFileEx)?

+3
source share
1 answer

DLL Win32, , DLL, OutOfMemoryException, AccessViolationException, NullReferenceException () SEHException.

RuntimeWrappedException . C++ , Exception. DLL. ++ DLL, throw 1, RuntimeWrappedException.

SEHException catch-all SEH DLL. ++ throw 1. AFAIK, ++ SEHException, , , .

, DLL, Win32. Cygwin DLL. ++ , , . , DLL Windows ( CopyFileEx) Win32, DLL, Microsoft ++.

, API Win32 ( CopyFileEx) Win32; GetLastError. Win32 Win32 - (, ntdll.dll).

+6

Source: https://habr.com/ru/post/1756384/


All Articles