0

I have a capture card from Black Magic Design company. In related document it is described that the GetBytes method, from IDeckLinkVideoInputFrame interface, allows direct access to the data buffer of a video frame. Here is my work:

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);
}

I also defined voidPtrToFrame in class of DeckLinkDevice:

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

In the last line an error appears related to str1:

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

I want to know:

How can I display the value of voidPtrToFrame in an Edit control? i.e. I want to present the address of buffer containing the video frame. In the following image I provided the necessary information about GetBytes method.

enter image description here

I googled a lot and tested several ways. But I could not implement them in MFC.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
IndustProg
  • 627
  • 1
  • 13
  • 33
  • 1
    This is unrelated to the Windows API, MFC, or even C++. It's really just about C, and pointers, uninitialized data and undefined behavior. You should read an introduction to C first, to understand, how it is different from managed execution environments (like .NET). – IInspectable May 04 '16 at 13:11
  • @IInspectable the `SetDlgItemText` part of the question *is* somewhat Windows related. – Jabberwocky May 04 '16 at 13:12
  • @MichaelWalz: That call is unrelated to the issue at hand: Writing to memory pointed to by an uninitialized pointer. – IInspectable May 04 '16 at 13:15
  • @IInspectable there are actually two issues in this question, the second one is dealing with a compilation error in `SetDlgItemText`. – Jabberwocky May 04 '16 at 13:16
  • @IInspectable please consider that I did same work on win32 application successfully many times. I am new to MFC enviroment and its methods. – IndustProg May 04 '16 at 13:19
  • 2
    Please don't change the question in a way that invalidates already published answers. Besides, you aren't even using anything MFC-specific here (with the exception of querying for a window handle). In this particular case, an application using the Windows API and an application using MFC are pretty much identical. You probably need to study the Windows API more to understand, how MFC fits into this. This question, however, isn't related to MFC at all. – IInspectable May 04 '16 at 13:22
  • 1
    Possible duplicate of [Convert char\[\] to LPCWSTR](http://stackoverflow.com/questions/3225682/convert-char-to-lpcwstr) – IInspectable May 04 '16 at 13:26
  • @IInspectable I did not know there is `SetDlgItemText` in win32 application. I forgot typing `[100]` when declaring `str1`. Do NoT judge when you do NOT know me. – IndustProg May 04 '16 at 13:38
  • 1
    *"I did not know there is SetDlgItemText in win32 application."* - [SetDlgItemText](https://msdn.microsoft.com/en-us/library/windows/desktop/ms645521.aspx) is exported from *user32.dll*. This is part of the Win32 subsystem (not MFC). *"I forgot typing `[100]` when declaring `str1`."* That's not a declaration but a definition. And defining an array still leaves its contents uninitialized. *"Do NoT judge when you do NOT know me."* - I do not have to know the author to judge the quality and accuracy of a question. – IInspectable May 04 '16 at 13:50
  • @IInspectable I learned from you. Thanks. – IndustProg May 04 '16 at 14:01

1 Answers1

2

You have two problems:

1. You get a crash or at least undefined behaviour

The variable str1 is never initialized. It's a classic beginner's error.

The problem is here:

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)

You need this:

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

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

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

2. You must use wide characters

You are compiling for unicode, therefore you need this (previous other corrections are included here):

wchar_t str1[20];

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

wsprintf(str1, L"%p", voidPtrToFrame);
SetDlgItemText(m_uiDelegate->GetSafeHwnd(), IDC_handytxtBox, str1);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • It works fine. Many thanks for the reply. The value of `voidPtrToFrame` is varying continuously. Can I make it constant by `static` keyword? if yes, how to do? – IndustProg May 04 '16 at 13:57
  • No you can't because `voidPtrToFrame` is updated by the `GetBytes` function which is not under your control. – Jabberwocky May 04 '16 at 14:08