
Signature
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
Oh, my mistake, we should not pass Null for the first parameter, it is an
invalid handle for GetDeviceCaps. We should first create a screen DC with
CreateDC and then pass this screen DC to the GetDeviceCaps. Below code
snippet works well on my side:
[DllImport("gdi32.dll", EntryPoint = "CreateDC", CharSet = CharSet.Auto,
SetLastError = true)]
private static extern IntPtr CreateDC(string lpszDriver, string
lpszDeviceName, string lpszOutput, IntPtr devMode);
[DllImport("gdi32.dll", SetLastError=true)]
private static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 capindex);
private const int LOGPIXELSX = 88;
IntPtr hdc = CreateDC("DISPLAY", null, null, IntPtr.Zero);
if (hdc == IntPtr.Zero)
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
return;
}
int _dpi = GetDeviceCaps(hdc, LOGPIXELSX);
if (_dpi == 0)
{
MessageBox.Show(Marshal.GetLastWin32Error().ToString());
return;
}
Hope it helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 01 May 2007 17:30 GMT
I remember it being null from my C++ days too - I guess we both have the
wrong memory. I added a DeleteDC and posted the code at
http://www.davidthielen.info/programming/2007/05/get_screen_dpi_.html for the
next person trying to find this.
Thank you for your help.

Signature
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
> Oh, my mistake, we should not pass Null for the first parameter, it is an
> invalid handle for GetDeviceCaps. We should first create a screen DC with
[quoted text clipped - 46 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeffrey Tan[MSFT] - 02 May 2007 04:43 GMT
Hi dave,
Thank you for sharing the result on your page!
Ok, if you need further help, please feel free to post, thanks.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.