Sign in to follow this  

DWORD to int

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I was looking into how to get the length of a LPCTSTR (for a directX text output (C++)), and someone pointed me to a page where there was a way of getting the length as a DWORD. Could you just cast that to an int, or is there another way? And yes, I did already Google 'DWORD to int'.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
DWORD is a typedef. Typically for int.


According to this page on msdn (which looks like it's meant for 32-bit Windows), it's a typedef to unsigned long. However, I'd be hesitant to write code that relied on the underlying type of a typedef...the whole point of the typedef is to hide that information. Code that relies on the underlying type of the typedef could break on other platforms or architectures (such as moving to 64-bit Windows).

Cheers,
Matt

Share this post


Link to post
Share on other sites
Quote:
Original post by mnizol
Quote:
Original post by jpetrie
DWORD is a typedef. Typically for int.


According to this page on msdn (which looks like it's meant for 32-bit Windows), it's a typedef to unsigned long. However, I'd be hesitant to write code that relied on the underlying type of a typedef...the whole point of the typedef is to hide that information. Code that relies on the underlying type of the typedef could break on other platforms or architectures (such as moving to 64-bit Windows).


DWORD is specific to Windows (though others might define it also) and Microsoft has not changed its definition (as well as the sizes of int or long) for 64-bit Windows. The only problem you really need to worry about is that DWORD is unsigned and int is signed.

Share this post


Link to post
Share on other sites
If you are worried about the size of the data type for portability among different platforms that may have a different dword size, just #include <cstdint>, and store it in an uint32_t.

If not, on 32bit systems, a dword is usually an unsigned int.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
DWORD is specific to Windows (though others might define it also) and Microsoft has not changed its definition (as well as the sizes of int or long) for 64-bit Windows. The only problem you really need to worry about is that DWORD is unsigned and int is signed.


John,

I can't speak to the specifics that you mention; however, would you agree, given that the OP posted to a beginner forum, that it would be good to encourage the practice of *not* writing code that relies upon the underlying type of a typedef? It may be true in practice that the code will work just fine on current and future Windows platforms; but, I'd contend that encouraging safe programming habits would be better for the OP's development long-term. Just my two cents.

Cheers,
Matt

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this