c++ - MFC中如何显示指针的值?

标签 c++ winapi mfc

我有一张 Black Magic Design 公司的采集卡。在相关文档中描述了 IDeckLinkVideoInputFrame 接口(interface)中的 GetBytes 方法允许直接访问视频帧的数据缓冲区。这是我的作品:

HRESULT     DeckLinkDevice::VideoInputFrameArrived (/* in */ IDeckLinkVideoInputFrame* videoFrame, /* in */ IDeckLinkAudioInputPacket* audioPacket)
{
    char* str1;
    voidPtrToFrame = NULL;
    videoFrame->GetBytes(&voidPtrToFrame);
    sprintf(str1, "%p", voidPtrToFrame);
 // the below line does not work. 
    SetDlgItemText(m_uiDelegate->GetSafeHwnd(), IDC_handytxtBox, str1);
}

我还在 DeckLinkDevice 类中定义了 voidPtrToFrame:

class DeckLinkDevice::IDeckLinkInputCallback
{
...
void* voidPtrToFrame;
...
}

最后一行出现与str1相关的错误:

argument of type "char*" is incompatible with parameter of type LPCWSTR

我想知道:

如何在编辑控件中显示 voidPtrToFrame 的值?即我想显示包含视频帧的缓冲区地址。在下图中,我提供了有关 GetBytes 方法的必要信息。

enter image description here

我在谷歌上搜索了很多并测试了几种方法。但是我无法在 MFC 中实现它们。

最佳答案

你有两个问题:

<强>1。你遇到崩溃或至少是未定义的行为

变量 str1 从未被初始化。这是一个典型的初学者错误。

问题出在这里:

char* str1;
voidPtrToFrame = NULL;
videoFrame->GetBytes(&voidPtrToFrame);

// here str1 points to an interterminate location, it has never been
// initialized !! Therefore your program most likely will crash
sprintf(str1, "%p", voidPtrToFrame)

你需要这个:

char str1[20]; //<<< changement here

voidPtrToFrame = NULL;
videoFrame->GetBytes(&voidPtrToFrame);

// now str1 points to a 20 byte buffer
sprintf(str1, "%p", voidPtrToFrame);

<强>2。必须使用宽字符

您正在为 unicode 编译,因此您需要这个(此处包含之前的其他更正):

wchar_t str1[20];

voidPtrToFrame = NULL;
videoFrame->GetBytes(&voidPtrToFrame);

wsprintf(str1, L"%p", voidPtrToFrame);
SetDlgItemText(m_uiDelegate->GetSafeHwnd(), IDC_handytxtBox, str1);

关于c++ - MFC中如何显示指针的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37028086/

相关文章:

c++ - QSqlRelationalTableModel QTableView 着色行

c++ - "/MD/MT "和 "dll lib"之间的概念歧义

c - 调用 CreateWindowEx 函数时出现访问冲突错误

c++ - 调整鼠标形状的大小在 MFC 中发生了变化

c++ - 如何检测无模式 CDialog 是否已关闭?

c++ - 输入特征以检查类是否具有成员函数

c++ - 如何为模板化类的模板化函数定义模板特化

c++ - Listview 中的第一次机会异常设置检查状态

c - 我如何使用 Win32 API 启动带参数的进程?

c++ - Windows API 如何接受字符串