L
Larry Smith
Hi there,
Does anyone know if this (example) is safe:
[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowRect(IntPtr hWnd, ref RECT
rect);
int rc = GetWindowRect(hWnd, ref rect);
if (rc == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Error");
}
My concern is the call to "Marshal.GetLastWin32Error()". Can the value
returned by this function be modified by the call to the exception
constructor itself. For instance, could construction of the "Error" string
overwrite "Marshal.GetLastWin32Error()" on failure given that it might also
call into the WinAPI. This would be a problem in C++ for instance where you
can't depend on the order of the parameters being passed (i.e., you can't
reliably pass "GetLastError()" as the first arg since the 2nd.arg might be
evaluated first - on failure it would then overwrite the first arg)
Does anyone know if this (example) is safe:
[DllImport("user32.dll", SetLastError = true)]
internal static extern int GetWindowRect(IntPtr hWnd, ref RECT
rect);
int rc = GetWindowRect(hWnd, ref rect);
if (rc == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Error");
}
My concern is the call to "Marshal.GetLastWin32Error()". Can the value
returned by this function be modified by the call to the exception
constructor itself. For instance, could construction of the "Error" string
overwrite "Marshal.GetLastWin32Error()" on failure given that it might also
call into the WinAPI. This would be a problem in C++ for instance where you
can't depend on the order of the parameters being passed (i.e., you can't
reliably pass "GetLastError()" as the first arg since the 2nd.arg might be
evaluated first - on failure it would then overwrite the first arg)