dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.79k stars 193 forks source link

ErrorHelper.GetErrorMessage throws System.OverflowException #467

Closed pdupont1 closed 4 months ago

pdupont1 commented 4 months ago

Describe the bug and how to reproduce

HRESULT.ToString() throws System.OverflowException: Value was either too large or too small for a UInt32.

I tried updating our Project references Vanara.PInvoke.CldApi (3.4.17) and Vanara.PInvoke.SearchApi (3.4.17) to 4.0.1. After updating the above mentioned exception is thrown when cloud API calls fail with a Win32 error code which was not the case before.

Stacktrace:

Convert.ThrowUInt32OverflowException()
IConvertible.ToUInt32(IFormatProvider provider)
ErrorHelper.GetErrorMessage[TType,TFieldType](TFieldType id, String lib)
HRESULT.ToString()

What code is involved

Vanara.PInvoke.Shared 4.0.1 .NET Runtime 8.0.5

Expected behavior

ToString() should succeed and return HRESULT_FROM_WIN32(ERROR_CLOUD_FILE_NOT_UNDER_SYNC_ROOT): The operation is only supported on files under a cloud sync root.

Repro is very simple by creating a HRESULT object from the error code returned by the Cloud API directly:

static void Main(string[] args)
{
    HRESULT hRESULT = HRESULT.HRESULT_FROM_WIN32(Win32Error.ERROR_CLOUD_FILE_NOT_UNDER_SYNC_ROOT);
    //Should print: HRESULT_FROM_WIN32(ERROR_CLOUD_FILE_NOT_UNDER_SYNC_ROOT): The operation is only supported on files under a cloud sync root.
    //Throws: HRESULT.ToString() throws `System.OverflowException: Value was either too large or too small for a UInt32.`
    Console.WriteLine("" + hRESULT);
    Console.Read();
}
dahall commented 4 months ago

I think this is fixed. I've tested your condition without failure with the pre-4.0.2 build. It should be available to test shortly from the AppVeyor feed (see homepage).