Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:10:48+00:00 2026-06-01T17:10:48+00:00

Can someone help me understand why Win32 error codes don’t match method signatures and

  • 0

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?

Take for example SCardReleaseContext, as per the MSDN documentation the return type is LONG. As per this MSDN article, the c# equivalent of LONG is int. Looking at some p/invoke example signatures of SCardReleaseContext, the return type is also marked as int.

However, the definitions of WinSCard error codes, do not match the int type, because the value simply doesn’t fit into int.

Currently, in my c# program, I have had to define the return value and the error code values as uint. This way my code compiles and works. Otherwise, the compiler will complain that the value cannot be converted to int, for instance – enum ErrorCodes : int { SCARD_E_CANCELLED = 0x80100002 }.

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?

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 uint instead?

Edit:
Fixed the last link, which was sort of incorrect, in it the values were cast to DWORD, which happens to be uint. Set the link to point to Microsoft’s own site.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:10:49+00:00Added an answer on June 1, 2026 at 5:10 pm

    The winapi was designed to be used with a C compiler. A language which allows assigning an unsigned literal to a signed integer. Some compilers generate a warning for that but never an error. Your C# compiler isn’t so forgiving. You’ll need to apply two two-by-fours to keep it happy:

        enum ErrorCodes : int { 
            SCARD_F_INTERNAL_ERROR = unchecked((int)0x80100001) 
        }
    

    The cleaner solution is to have the enum inherit from uint instead.

        enum ErrorCodes : uint { 
            SCARD_F_INTERNAL_ERROR = 0x80100001 
        }
    

    And change the pinvoke declaration to return an uint instead of an int. Lying in the pinvoke declaration is a pretty common technique, especially with the ones that take a PVOID or LPARAM. Of course, you have to know the consequences. There are none for lying LONG to uint, the types have the same size, only the value interpretation is different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me understand what I'm doing wrong with this method? I'm trying
(Specifically, I'm using Backbone Model events) Can someone help me understand how javascript events
can someone help me understand this because it's working in as3 but not so
Can someone help me understand why this gives an output of 0? #include <iostream>
Can someone help me understand this topshelf project...mainly the difference between ConfigureServiceinIsolation and ConfigureService.
Eclipse crashed and won't restart. Can someone help me understand what the problem is?
Can someone help me understand how to make Ryan Bate's screencast on Backbone.js work
Can someone help me understand what SendGrid actually adds to an application architecture? Is
Can someone help me understand the WPF stretch alignment. I often run into issues
Can someone help me understand what's wrong with this query: DELETE FROM noteproject INNER

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.