-1

I was reading about Marshaling. and im confused because what does mean this in unmanaged code. HRESULT, DWORD, and HANDLE. The original text is:

You already know that there is no such compatibility between managed and unmanaged environments. In other words, .NET does not contain such the types HRESULT, DWORD, and HANDLE that exist in the realm of unmanaged code. Therefore, you need to find a .NET substitute or create your own if needed. That is what called marshaling.

Jon B
  • 51,025
  • 31
  • 133
  • 161
user1495049
  • 43
  • 1
  • 2
  • 5
    you can use google for this... – Mitch Wheat Jan 04 '13 at 04:16
  • 3
    An inexperienced programmer might not understand how to interpret Google results for a search like this. An easy question deserves a quick answer to assist this user and others in the future, in my opinion. – Dan Nissenbaum Jan 04 '13 at 04:45
  • Which is what I gave. I feel the Wikipedia descriptions are easy enough to understand and your answer as well. Just not entirely sure this is a question for SO, might be wrong – TimothyP Jan 04 '13 at 05:02
  • Sorry guys but i prefer to read a programmer help directly.. – user1495049 Jan 04 '13 at 05:12
  • @MitchWheat, no Mitch, the purpose of this site is to ask questions. Telling the poster to search, is not productive. – AMissico Jan 04 '13 at 05:15
  • 1
    @AMissico: questions should at least be attempted by the poster. No attempt is shown. – Mitch Wheat Jan 04 '13 at 05:44
  • @MitchWheat, I disagree. The question was the interpretation of a statement. No amount of searching and reviewing the definition of a `DWORD` is not going to help. Notice he states "read a programmer help directly". The poster is asking us experienced professionals, for clarification. – AMissico Jan 04 '13 at 06:41
  • You can disagree. I've spent alot of time here and I expect (as I believe, do others) that posters should show some effort. How did you find the links you reference? By searching of course... – Mitch Wheat Jan 04 '13 at 07:02
  • @MitchWheat, Yes, spending days searching back in 2003 when I was trying to figure out how to make Windows CE API calls on .NET Compact Framework 1.1. If there was a site like this back then, it would have taken me a few hours to get an answer, instead of days. You been on this site long enough to know that it is okay to ask questions like this and it is not okay to tell the poster to "search". We are here to help, not be critical. – AMissico Jan 04 '13 at 14:19
  • @MitchWheat, it depends on how you read the question. In your case, the specific definitions, but you know want DWORD means and you know how to select the correct result from the garbage that is returned when you search for DWORD. I mean, http://en.wikipedia.org/wiki/DWORD#Size_families is just meaningless. Take TimothyP search results as an example, he didn't even find the correct definition for HANDLE, and he has a good idea what his is searching for. – AMissico Jan 04 '13 at 14:44
  • 1
    @MitchWheat I agree with AMissico here. Please give posters the benefit of the doubt unless you are absolutely certain. StackOverflow's greatest flaw, in my opinion, is that high-reputation members feel encouraged to improperly judge others. I witness this frequently, and it makes me concerned that the quality of the site will eventually degrade because of it. – Dan Nissenbaum Jan 05 '13 at 01:42
  • @Dan Nissenbaum: the site will eventually degrade due to the high number of questions that show no effort, research or attempt. SO seems to have recognised this fact in the last few months as they have changed how negative voted questions appear (i.e. not appear) in the unanswered questions list. There are 827K questions with no upvoted answers, in part due to the lack of 'buy-in' from posters who want instant answers/code/work done for them for free by some 'sucker'. – Mitch Wheat Jan 05 '13 at 01:50
  • @MitchWheat Yes, but you don't know that this question is an example of it. Also, even in those cases where no research is evidenced - but the question is a valid one, and not a duplicate - it will help others in the future to provide an answer on this site. – Dan Nissenbaum Jan 05 '13 at 01:53
  • @Dan: I'm going with my experience of answering quite a few questions and reading many times more. – Mitch Wheat Jan 05 '13 at 02:10
  • @MitchWheat, I do not want to be difficult, but you are wrong. I say this because I understand your point of view perfectly. Yet, your attitude about it is wrong. I am sorry, but this site is meant to help your peers. If you do not want to help because a question is not properly qualified, then keep silent. That is all I am saying, and that is what all the FAQs mention. If you do not know for sure, do not answer. – AMissico Jan 05 '13 at 07:45
  • @MitchWheat - "How did you find the links you reference?" Actually, I took out of my development OneNote notebook from the "General 2005 Part 2" section. :O) – AMissico Jan 05 '13 at 07:56
  • Thanks guy my problem is resolved. ;-) – user1495049 Jan 09 '13 at 20:41

3 Answers3

8

short answer:

it is just telling you that you must "map" one data type used in one programming language to another data type used in a different programming language, and the data types must match.

quick answer:

For this one, the details may not be correct, but the concept is.

These are a few of the data types defined in the Windows header files for C/C++. They are "macros" which "abstract" the primitive data types of C/C++ into more meaningful data types used in Windows programming. For instance, DWORD is really an 32-bit unsigned integer in C/C++, but on 64-bit processors, it is defined in the header files as a 64-bit unsigned integer. The idea is to provide an abstraction layer between the data type needed by the processor and the data types used by the language.

During marshalling, this "dword" will be converted to the CLR data type you specify in the DllImport declaration. This is an important point.

Let's say you want to call a Windows API method that takes a DWORD parameter. When declaring this call in C# using DllImport, you must specify the parameter data type as System.UInt32. If you don't, "bad things will happen".

For example, if you mistakenly specify the parameter data type as System.UInt64. When the actual call is made, the stack will become corrupt because more bytes are being placed on the stack then the API call expects. Which can lead to completely unexpected behavior, such as crashing the application, crashing Windows, invalid return values, or whatever.

That is why it is important to specific the correct data type.

data types in question:

  • DWORD is defined as 32-bit unsigned integer or the CLR type System.UInt32.
  • HANDLE is the CLR types IntPtr, UintPtr, or HandleRef
  • HRESULT is System.Int32 or System.UInt32

References:

Community
  • 1
  • 1
AMissico
  • 21,470
  • 7
  • 78
  • 106
3

HRESULT: http://en.wikipedia.org/wiki/HRESULT

In the field of computer programming, the HRESULT is a data type used in Windows operating systems, and the earlier IBM/Microsoft OS/2 Operating system, used to represent error conditions, and warning conditions. The original purpose of HRESULTs was to formally lay out ranges of error codes for both public and Microsoft internal use in order to prevent collisions between error codes in different subsystems of the OS/2 Operating System. HRESULTs are numerical error codes. Various bits within an HRESULT encode information about the nature of the error code, and where it came from. HRESULT error codes are most commonly encountered in COM programming, where they form the basis for a standardized COM error handling convention.

DWORD: http://en.wikipedia.org/wiki/DWORD#Size_families

HANDLE: http://en.wikipedia.org/wiki/Handle_(computing)

In computer programming, a handle is an abstract reference to a resource. Handles are used when application software references blocks of memory or objects managed by another system, such as a database or an operating system. While a pointer literally contains the address of the item to which it refers, a handle is an abstraction of a reference which is managed externally; its opacity allows the referent to be relocated in memory by the system without invalidating the handle, which is impossible with pointers. The extra layer of indirection also increases the control the managing system has over operations performed on the referent. Typically the handle is an index or a pointer into a global array of tombstones.

TimothyP
  • 21,178
  • 26
  • 94
  • 142
  • @AMissico Please leave it to others to decide whether it is really answering the question. Your first comment, *"HANDLE is wrong"*, is useful and valid. The second - not so much. I note that you have provided an excellent answer yourself, and then proceeded to downvote both of the other answers. Upvoting another's better answer, as TimothyP has upvoted your answer, is another matter. – Dan Nissenbaum Jan 05 '13 at 01:45
  • @DanNissenbaum, you are correct. When people put a question in the title and do not restate the question in the body text, I sometimes miss their "title question". Later on I noticed that TimothyP did answer the question addressed in the title. Yet, I did not change my downvote because overall I felt his answer didn't provide any value because it is just links and quotes. – AMissico Jan 05 '13 at 07:36
0

HRESULT, DWORD, and HANDLE are typedef's (i.e., they represent plain data types) defined by Microsoft for use by programmers compiling *un*managed code in a Windows environment. They are defined in a C (or C++) header file that is provided by Microsoft that is, typically, automatically included in unmanaged Windows projects created within Microsoft Visual Studio.

Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
  • -1, too much believe, not positive, if you do not know, don't answer, see faq. – AMissico Jan 04 '13 at 06:47
  • @AMissico - The items I say "I believe, am not positive" about are all in the second paragraph, *secondary* to the purpose of the question, and not central to the answer. Should I not answer the poster's question if I do not have time to research secondary matters? I make explicitly clear when I am not certain about something and in every case it is in regards to a secondary issue. For the primary points, I am absolutely correct, and certain. Your downvote is an example of a quick reaction without consideration of these important details. – Dan Nissenbaum Jan 05 '13 at 01:33
  • 1
    Providing an answer yourself (a good answer - thank you for taking the time), and then downvoting someone else's more rapid response, is crude. – Dan Nissenbaum Jan 05 '13 at 01:38
  • On another note, @AMissico, I agree with your comments in response to MitchWheat in the above discussion; thank you. – Dan Nissenbaum Jan 05 '13 at 01:43
  • (Second paragraph is now removed) – Dan Nissenbaum Jan 05 '13 at 02:16
  • I removed the downvote. The answer reads much better. Yes, you are correct, but I feel an answer must be stated confidently, regardless if it is correct or not. Otherwise, it is not an answer. Now that you removed the "passive" part of the answer, it reads much better. Therefore, I removed the downvote. Moreover, if you check my profile, you will notice that I do not upvote or downvote much. So I disagree with the "quick reaction" comment. – AMissico Jan 05 '13 at 07:29
  • regarding MitchWheat, thank you. I hate to argue that point, but I will do it. I know how hard it is to search for an answer when you don't know what to search for. – AMissico Jan 05 '13 at 07:33
  • I will remove my comments from this answer in a couple days, since they do not provide any additional value to your answer. Just letting you know, so you can do the same, if you want. – AMissico Jan 05 '13 at 07:34
  • I would remove my comments, but I don't have a high enough reputation to do that. – Dan Nissenbaum Jan 05 '13 at 11:55