Advertisement

03.27.2008 at 01:59PM PDT, ID: 23275637
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Type Cast conversion for x64 build
Tags: C, C++, C4312
I'm working on converting a printer driver to run on x64, and have a couple errors I'm not sure what to do with. I have a lot of type cast warnings,  but only a few errors. As this is a driver build and uses the DDK compiler, I can't really disable the portability checking.

Any help would be appreciated.

1:
2:
3:
4:
5:
6:
7:
8:
//Generates error C4312: 'type cast' : conversion from 'DWORD' to 'LPTSTR' of greater size
pOptParam->pData  = (LPTSTR) *pTemplateInfo++; // pointer to the data
 
//Generates error C4312: 'type cast' : conversion from 'DWORD' to 'BYTE *' of greater size
pb = (BYTE*)(((DWORD)pb + 3) & ~3); // DWORD align
 
//Generates error C4312: 'type cast' : conversion from 'int' to 'HBRUSH' of greater size
FillRect( lpDIS->hDC, & lpDIS->rcItem, (HBRUSH)(nIndex +1) );
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: hess_joel
Solution Provided By: evilrix
Participating Experts: 2
Solution Grade: B
Views: 0
Translate:
Loading Advertisement...
03.27.2008 at 08:07PM PDT, ID: 21227716

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 12:57AM PDT, ID: 21228581

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 04:41AM PDT, ID: 21236196

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 07:07AM PDT, ID: 21236583

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 07:23AM PDT, ID: 21236637

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 07:56AM PDT, ID: 21236776

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 08:09AM PDT, ID: 21236817

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 09:03AM PDT, ID: 21237012

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 09:12AM PDT, ID: 21237081

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.29.2008 at 09:55AM PDT, ID: 21237240

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.27.2008 at 08:07PM PDT, ID: 21227716

Rank: Master

This link may help
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win64/win64/rules_for_using_pointers.asp

I'm assuming that the code contains pointer casts.  The article also mentions that HANDLE is defined as void*, and I believe HBRUSH is a HANDLE.
 
03.28.2008 at 12:57AM PDT, ID: 21228581

Rank: Wizard

You can stop this warning by disabling the /Wp64 option in your project properties.
[Configuration Properties | C/C++ | General ===> Detect 64-bit Portability Issues]

You can also disable the warning on a per instance basis using #pragma warning, example below.

Before you disbale this warning I suggest you read the caveats documented on the MSDN page.
http://msdn2.microsoft.com/en-us/library/h97f4b9y.aspx
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
#include "windows.h"
 
int main()
{
	DWORD dw = 0;
 
// Disable warning C4312
#ifdef _WIN64
#pragma warning( push )
#pragma warning( disable : 4312 )
#endif
 
	// This line normally generates warning C4312
	LPTSTR s = (LPTSTR) dw;
 
// Reenable warning C4312
#ifdef WIN64
#pragma warning( pop )
#endif
 
	return 0;
}
Open in New Window
 
03.29.2008 at 04:41AM PDT, ID: 21236196

Rank: Wizard

I forgot to mention that you can also disable just that specific compiler warning by adding /wd4312 to the compiler command line settings in the project properties page.

/wd n
Disables the specified compiler warning where n is the compiler warning number.

http://msdn2.microsoft.com/en-us/library/thxezb7y.aspx
 
03.29.2008 at 07:07AM PDT, ID: 21236583
As I said, I can't disable the error because the is a device driver using the DDK compiler.

I'm looking for a solutiont that will not just ignore the error.
 
03.29.2008 at 07:23AM PDT, ID: 21236637

Rank: Master

I can't test to see whether the generated code actually does what you intend, but I can say that the lines marked /*fix*/ remove the compilation error when building with VS2005.

#include <windows.h>

struct OptParam { LPTSTR pData; };
struct DIS { HDC hDC; RECT rcItem; };

int main() {
   //Generates error C4312: 'type cast' : conversion from 'DWORD' to 'LPTSTR' of greater size
   OptParam* pOptParam = new OptParam;
   int* pTemplateInfo = 0;
   //pOptParam->pData  = (LPTSTR) *pTemplateInfo++; // pointer to the data
   /*fix*/ pOptParam->pData  = (LPTSTR) (INT_PTR) *pTemplateInfo++; // pointer to the data
   
   //Generates error C4312: 'type cast' : conversion from 'DWORD' to 'BYTE *' of greater size
   BYTE* pb = 0;
   //pb = (BYTE*)(((DWORD)pb + 3) & ~3); // DWORD align
   /*fix*/ pb = (BYTE*)(((INT_PTR)pb + 3) & ~3); // DWORD align
   
   //Generates error C4312: 'type cast' : conversion from 'int' to 'HBRUSH' of greater size
   int nIndex = 0;
   DIS* lpDIS = new DIS;
   //FillRect( lpDIS->hDC, & lpDIS->rcItem, (HBRUSH)(nIndex +1) );
   /*fix*/ FillRect( lpDIS->hDC, & lpDIS->rcItem, (HBRUSH)(INT_PTR)(nIndex +1) );
}
 
03.29.2008 at 07:56AM PDT, ID: 21236776

Rank: Wizard

>> As I said, I can't disable the error because the is a device driver using the DDK compiler.
It's not an error it's a warning and if you want to do this properly and remove the warning without disabling it you'll need to rework your code to use compatible types -- after all this is what the compiler is bitching about!

You may be able to introduce different levels of casting to prevent the compiler from giving the warning but that doesn't change the fundamental fact that you are casting types that are potentially incompatible. That said, if you know the values being cast are safe to cast (because the values won't cause overflow) then disabling the warning is the simplest way to do this otherwise you potentially make your code hard to follow/read just to hide a warning! If you want to ensure the cast is safe at runtime you can always use the Boost numeric cast operator, which performs runtime validation; an exception if thrown of the cast is unsafe and you'll have to handle that situation. You can then disable the warning and know your code will do the right thing at runtime time.
http://www.boost.org/libs/numeric/conversion/doc/numeric_cast.html
 
03.29.2008 at 08:09AM PDT, ID: 21236817

Rank: Master

evilrx, I'm thinking that you're on the right track here, but (using the example I posted) when I

   pOptParam->pData = boost::numeric_cast<LPTSTR>(pTemplateInfo); // pointer to the data

then I get
   error C2440: 'static_cast' : cannot convert from 'int *const ' to 'char *'

what's with the const?
 
03.29.2008 at 09:03AM PDT, ID: 21237012

Rank: Wizard

The boost::numeric_cast can't cast between numeric and pointer types as it's an unsafe cast so what you'll need to do is cast the numeric value to a suitable numeric type that will cast to your pointer type without the warning and then use reinterpret_cast to cast to the pointer type. Since the size_t type is sized specific to the platform (it is guaranteed to be an unsigned integer type that is suitable for addressing ranges of memory) you may find this is the most suitable type; however, you'll need to play a little. I don't have a 64bit compiler at home so I am not in a position to test this unfortunately. Below is an example.

This might look a bit of a kludge and, guess what, it is! The only safe way to fix this is to rewrite the code to use 64bit safe types. Also, you should note that when casting incompatible pointer types (pointer to pointer or numeric to pointer for example) the only safe thing you can do with them is to cast back to the original type and any other usage is, at best, undefined and non-portable.
1:
2:
3:
4:
5:
6:
7:
8:
9:
#include "windows.h"
#include <boost/cast.hpp>
 
int main()
{
	DWORD dw = 0;
 	LPTSTR s = reinterpret_cast<LPTSTR>(boost::numeric_cast<size_t>(dw));
 	return 0;
}
Open in New Window
Accepted Solution
 
03.29.2008 at 09:12AM PDT, ID: 21237081

Rank: Master

Thank you, evilrx.  Now I see my mistake.

>>The only safe way to fix this is to rewrite the code to use 64bit safe types
I completely agree. *Especially* for a driver!

hess_joel, that's the real answer.  Coercing the compiler into silence is risky enough with an application.  A driver, as you know, has access to all of memory, including ring 0 memory.  'nuff said.
Assisted Solution
 
03.29.2008 at 09:55AM PDT, ID: 21237240

Rank: Wizard

>> Thank you, evilrix.  Now I see my mistake.
No worries, I probably should have explained it a little better to start with :)

>> Coercing the compiler into silence is risky...
Agreed! A cast is always a potential problem, even when you are sure it's safe. Casts can hide problems, you are basically telling the compiler to shut up, you know best. In this case the compiler is fighting back, for good reason -- the code isn't 64bit safe. Consider; a cast that is safe today may not be tomorrow (code can change) and a cast will hide any potential warnings that may come out of future modifications. The boost cast does offer some protection in the form of runtime checks, but you'll then have the overhead of having to write additional exception handlers to deal with when it does go wrong.

Anyway, I don't think there's much more that can be said on this. I hope the info josgood and I have given is helpful. Sorry but there is no simple answer to this... it's either rewrite the code to make it safe, disable the warning or cast away the warning and live with the potential that in the future something may cause it to mis-behave.

-Rx.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628