AddDllDirectory () is the latest addition to winapi. It is guaranteed to be available only in Windows 8, to obtain it on earlier versions of Windows, update KB2533623 is required. Do not worry when you select product requirements.
This is unusual in more than one way; it does not match the normal pattern for winapi functions that accept a string. This makes the function available in two versions, the ANSI version, which has A added and the Unicode version with W. AddDllDirectory () does not have an attached letter, only the Unicode version exists. It is not clear to me whether it was intentional or supervisory, with high chances for intentional. The function declaration is missing from the Windows 8 SDK headers, which is very unusual.
Thus, your original ad failed because you called the Unicode version, but the pinvoke marker passed the ANSI string. You were probably lucky because there were an odd number of characters in the string with enough successful zeros to not cause AccessViolation.
Using the CharSet property in a [DllImport] declaration requires the pinvoke marker to pass a Unicode string.
Hans passant
source share