Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy Win32 error codes don't match method signatures?
    primarykey
    data
    text
    <p>Can someone help me understand why Win32 error codes don't match method signatures and what is the correct way to deal with this issue?</p> <p>Take for example <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa379798%28v=vs.85%29.aspx" rel="nofollow">SCardReleaseContext</a>, as per the MSDN documentation the return type is <code>LONG</code>. As per <a href="http://msdn.microsoft.com/en-us/magazine/cc164123.aspx#S6" rel="nofollow">this MSDN article</a>, the c# equivalent of <code>LONG</code> is <code>int</code>. Looking at some p/invoke <a href="http://www.pinvoke.net/default.aspx/winscard.scardreleasecontext" rel="nofollow">example signatures</a> of SCardReleaseContext, the return type is also marked as <code>int</code>.</p> <p>However, the definitions of <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa374738%28v=vs.85%29.aspx" rel="nofollow">WinSCard error codes</a>, do not match the <code>int</code> type, because the value simply doesn't fit into <code>int</code>.</p> <p>Currently, in my c# program, I have had to define the return value and the error code values as <code>uint</code>. This way my code compiles and works. Otherwise, the compiler will complain that the value cannot be converted to int, for instance - <code>enum ErrorCodes : int { SCARD_E_CANCELLED = 0x80100002 }</code>.</p> <p>Can I correctly assume that a c++ compiler will take the value 0x80100001 (which doesn't fit into an int), let it overflow and thus turn the value into some appropriate matching negative value?</p> <p>Also, am I correct in saying that the c# compiler will not allow such a mistake/bug/flaw to be written and the correct way of handeling this (corner) case is to purposefully "brake" the p/invoke signature and use <code>uint</code> instead?</p> <p><strong>Edit:</strong><br> Fixed the last link, which was sort of incorrect, in it the values were cast to <code>DWORD</code>, which happens to be <code>uint</code>. Set the link to point to Microsoft's own site.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload