简体   繁体   English

在MFC C ++中获得桌面分辨率

[英]Getting Desktop Resolution in MFC C++

I have inherited some partially developed 10 year old code that was written in MFC C++. 我继承了一些用MFC C ++编写的已开发10年的旧代码。 I've been tasked with updating and finishing the app. 我的任务是更新和完成该应用程序。

In the CDialogChild class, it looks like this in the .h file: 在CDialogChild类中,它在.h文件中看起来像这样:

private:
static const int m_iDefaultDesktopSizeX = 1024;

static const int m_iDefaultDesktopSizeY = 740;

I'm still trying to figure out the code and the logic behind it, but I decided to dynamically get the resolution. 我仍在尝试找出代码及其背后的逻辑,但我决定动态获取分辨率。 I removed the "static" keyword from the .h as well as the magic numbers, and then I attempted to define them in the initialisation list in the constructor: 我从.h中删除了“ static”关键字以及魔术数字,然后尝试在构造函数的初始化列表中定义它们:

CDialogChild::CDialogChild(CWnd* pParent /*=NULL*/)
: CDialog(CDialogChild::IDD, pParent) , //more variables,
m_iDefaultDesktopSizeX(GetSystemMetrics(SM_CXSCREEN)), m_iDefaultDesktopSizeY(GetSystemMetrics(SM_CYSCREEN))
{
//...
}

My resolution is 1366x768, the numbers I get in the Visual Studio 2013 debugger vary, from "unable to access memory" to numbers in the millions. 我的分辨率是1366x768,我在Visual Studio 2013调试器中得到的数字各不相同,从“无法访问内存”到数以百万计的数字。

I then decided to attempt to initialize them in the constructor and not the initialisation list. 然后,我决定尝试在构造函数中而不是初始化列表中对其进行初始化。 I removed the "const" keyword in the header from both variables and then figured I would print to the debug output their values. 我从两个变量中删除了标头中的“ const”关键字,然后认为我会将其值打印到调试输出。 This is my code (I get that it's not so elegant but still figuring out the MFC conventions): 这是我的代码(我觉得它不是那么优雅,但是仍然可以弄清MFC的约定):

#ifdef UNICODE
typedef std::wstringstream tstringstream;
#else
typedef std::stringstream tstringstream;
#endif

m_iDefaultDesktopSizeX = GetSystemMetrics(SM_CXSCREEN);
m_iDefaultDesktopSizeY = GetSystemMetrics(SM_CYSCREEN);    

#ifdef OUTPUT_DEBUG_STRING
    tstringstream ss1;
    tstringstream ss2;

    ss1 << m_iDefaultDesktopSizeX;
    //stream >> sizeX;

    ss2 << m_iDefaultDesktopSizeY;

    LPTSTR dest = _T("m_iDefaultDesktopSizeX = ");
    LPTSTR src1 = _T("");
    LPTSTR src2 = _T("");

    ss1 >> src1;
    ss2 >> src2;

    // 1000 is a magic number
    StringCchCat(dest, 1000, src1);
    StringCchCat(dest, 1000, _T(" and m_iDefaultDesktopSizeY = "));
    StringCchCat(dest, 1000, src2);
    OutputDebugString(dest);
#endif

When I run it, an exception is thrown on the "ss2 >> src2" saying access violation writing to [hex address] 当我运行它时,在“ ss2 >> src2”上引发异常,表明访问冲突写入[十六进制地址]

So, what is going on and what more info should I provide? 那么,怎么回事,我应该提供什么更多信息? The problem I am getting is during my troubleshooting of another problem, so help with either or both would be awesome! 我遇到的问题是在对另一个问题进行故障排除期间,因此对这两个问题之一或两者的帮助将非常棒!

EDIT 编辑

Thanks to Mark's comment, I changed the initialization of all the LPTSTR variables to be "new TCHAR()", which fixed that particular issue. 感谢Mark的评论,我将所有LPTSTR变量的初始化更改为“ new TCHAR()”,从而解决了该特定问题。 I was able to run the string in the debug output, which showed the correct resolution (even though it's wrong in the VS Debugger itself), so thanks a lot! 我能够在调试输出中运行该字符串,该字符串显示正确的分辨率(即使VS Debugger本身是错误的),所以非常感谢! This works even when I put the variables back in the Constructor initialization list and make them "const" again :) 即使将变量放回到构造函数初始化列表中并再次使它们成为“ const”,此方法也有效:)

However, I now get a new error. 但是,我现在收到一个新错误。 While this is ultimately not an issue that it's happening in this #ifdef block, the error I am now getting might pop up in other code I write. 虽然这最终不是在#ifdef块中发生的问题,但我现在遇到的错误可能会在我编写的其他代码中弹出。

The code: 编码:

tstringstream ss1;
tstringstream ss2;

ss1 << m_iDefaultDesktopSizeX;
//stream >> sizeX;

ss2 << m_iDefaultDesktopSizeY;

LPTSTR dest = new TCHAR();
StringCchCat(dest, 300, _T("\nm_iDefaultDesktopSizeX = "));
LPTSTR src1 = new TCHAR();
LPTSTR src2 = new TCHAR();

ss1 >> src1;
ss2 >> src2;


StringCchCat(dest, 300, src1);
StringCchCat(dest, 300, _T(" and m_iDefaultDesktopSizeY = "));
StringCchCat(dest, 300, src2);
OutputDebugString(dest);

//error happens on the line below this one!!!!
delete dest;
dest = 0;
delete  src1;
src1 = 0;
delete  src2;
src2 = 0;

The error occurs when the runtime reaches the delete commands and it is that the .exe file has triggered a breakpoint. 当运行时到达删除命令并且.exe​​文件触发了断点时,将发生错误。

The error is most likely caused by the buffer overruns in your code such as the following: 该错误最有可能是由于代码中的缓冲区溢出所致,例如:

LPTSTR dest = new TCHAR();
StringCchCat(dest, 300, _T("\nm_iDefaultDesktopSizeX = "));

The first line allocates a single character, then on the second line you write about 20-30 characters to it. 第一行分配一个字符,然后在第二行中为它写入大约20-30个字符。

Don't bother with raw string pointers, just use CString or std::wstring: 不用理会原始的字符串指针,只需使用CString或std :: wstring:

tstringstream ss1;
tstringstream ss2;

ss1 << m_iDefaultDesktopSizeX;
ss2 << m_iDefaultDesktopSizeY;

CString dest = _T("\nm_iDefaultDesktopSizeX = ");
dest += ss1.str().c_str();
dest += _T(" and m_iDefaultDesktopSizeY = ");
dest += ss2.str().c_str();

OutputDebugString(dest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM