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.
I googled a lot and tested several ways. But I could not implement them in MFC.
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);
Related
Okay, I've been dealing with this for two days now, and I can't find a solution.
Problem: I'm trying to set a filter to a File Selection Dialog using Winapi. I'm using GetOpenFileName function to do this. This function uses a structure to set options such as file extension filters. This structure's member called lpstrFilter needs a certain string format. I'm setting that string exactly as Winapi indicates, but for some reason, this string's value changes.
I've got this static const char *:
//This contains string "JPG"
static const char * extensionFilter = v->trabajo10.C_JMV_SelectFile_FileExtension7.GetString();
//This forms a filter string which applies to OPENFILENAME structure.
string sFilter;
sFilter.append("Format: ");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
sFilter.append("*.");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
//This opens the file selection dialog
if (GetOpenFileName(&ofn)==TRUE){
...
The File Selection Dialog looks CORRECTLY, like this:
The joke comes now, I modify the code like this:
//This contains string "JPG"
static const char * extensionFilter = v->trabajo10.C_JMV_SelectFile_FileExtension7.GetString();
if(1){
//This forms a filter string which applies to OPENFILENAME structure.
string sFilter;
sFilter.append("Format: ");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
sFilter.append("*.");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
}
//This opens the file selection dialog
if (GetOpenFileName(&ofn)==TRUE){
...
And this is the result, THE PROBLEM:
Filter string was modified???
if(1){
//This forms a filter string which applies to OPENFILENAME structure.
string sFilter;
sFilter.append("Format: ");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
sFilter.append("*.");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
}
The sFilter variable has a lifetime that ends when the block in which it declared ends. The pointer returned by sFilter.c_str() is valid until sFilter is modified or destroyed.
You are using this pointer after it has become invalidated. This is the same problem as you had yesterday, which I guessed at in comments to the question. This is why you need to show a full MCVE. This question also looks to be a duplicate of the one that you asked a week ago: Winapi GetOpenFileName Extension Filter not working. I suggest that you take some time to make sure that you fully appreciate the validity of the value returned by c_str().
You must ensure that sFilter lives until after you have finished with the pointer. Declare sFilter in an outer block to ensure that.
The problem is that you have a variable that fell out of scope
if(1){
string sFilter;
// ... code
// Right here
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter;
}
After that block ends filter falls out of scope, so ofn.lpstrFilter has a dangling pointer.
Answering ProtectedVoid's concern about declaring an unused object: Imagine a std::string was an expensive object and the condition was unlikely. You don't want the object constructed unless the condition is true. But then it must last beyond the scope of the condition. So we use the fact that a default constructed unique_ptr is much cheaper than a default constructed string:
std::unique_ptr<std::string> scope_extender;
if( something unlikely ){
//This forms a filter string which applies to OPENFILENAME structure.
std::string* sFilter = new std::string;
scope_extender.reset( sFilter );
sFilter->append("Format: ");
sFilter->append(extensionFilter);
sFilter->push_back('\0');
sFilter->append("*.");
sFilter->append(extensionFilter);
sFilter->push_back('\0');
const char * filter = sFilter->c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
}
Obviously I don't want to imply std::string is expensive enough to construct to be worth all that trouble. But some object in a similar situation might be. Also, there was a comment about what if it was many minor objects in one conditional. In that case, you would want a utility struct to hold them all together and the unique_ptr conditionally owns that struct.
I have a DLL that I need to handle in C++. I'm using WxWidgets (standard compilation, but I also tried Unicode on/off) and NetBeans. I also tried dealing with this without WxWidgets (windows.h) and had same problems.
Here is how I access the DLL functions using WxWidgets:
// -------------------- POINTERS TO FUNCTIONS
typedef bool(*TYPE_DLL_SetLicense)(char*, char*);
typedef bool(*TYPE_DLL_PingConnection)(char*);
typedef char*(*TYPE_DLL_ERR_DESCRIPTION)(void);
class DLL_Library
{
public:
// pointers to functions inside dll
TYPE_DLL_SetLicense DLL_SetLicense; //initialize - will wor fine as it returns only true/false (buffer only provide data)
TYPE_DLL_PingConnection DLL_PingConnection; //ping to serwer. Will return trahs, becouse it uses buffer to provide data ang get answear back
TYPE_DLL_ERR_DESCRIPTION DLL_ERR_DESCRIPTION; //error description. No buffer, no trouble. Returns correct string.
wxDynamicLibrary dynLib2;
int initialize(void)
{
//patch to dll
wxString path = wxStandardPaths::Get().GetExecutablePath().BeforeLast('\\') + _("\\DLL_dll\\DLLMOK.dll");
if(!wxFile::Exists(path)) return -1;
//load dll
if(!dynLib2.Load(path)) return -2;
//Assign functions in dll to variable
DLL_SetLicense=(TYPE_DLL_SetLicense) dynLib2.GetSymbol(wxT("DLL_SetLicense"));
DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(wxT("DLL_PingConnection"));
DLL_ERR_DESCRIPTION=(TYPE_DLL_ERR_DESCRIPTION) dynLib2.GetSymbol(wxT("DLL_ERROR_DESCRIPTION"));
return 0;
}
};
And here is the function I run. It should return and XML content, that I try to save to the file.
//DLL_PingConnection
//result ping to be save in file
wxFile file_ping_xml;
plik_ping_xml.Open(wxT("C:\\dll\\ping.xml"),wxFile::write);
char buffor_ping_xml[2000];
//I run the function here
bool is_ping = DLL_PingConnection(buffor_ping_xml);
if(is_ping)
{
tex_box->AppendText(wxT("DLL_PingConnection True\n"));
//we save result to file
bool is_write_ping_ok = file_ping_xml.Write(buffor_ping_xml,2000);
if (is_write_ping_ok){tex_box->AppendText(wxT("Save to file is ok ok\n"));}
else {tex_box->AppendText(wxT("Save to file failed :( \n"));}
}
else
{
tex_box->AppendText(wxT("DLL_PingConnection False\n"));
}
std::cout << "Error description: " << DLL_ERR_DESCRIPTION() << "\n"; //will work fine both in saving to file, and in streaming to screen.
The problem is that inside the file instead of good content I get rubbish like this:
NOTE that this only happens in functions that use buffers like:
char buffer[2000] //buffer will contain for example file xml
function do_sth_with_xml(buffer) //buffer containing xml will (should) be overwriten with xml results of the function - in our case DLL_PingCONNECTION should save in buffer xml with connection data
Documentation say that the DLL operates on Windows-1250. File ping.xml I have set to windows ANSI, but I don't think problem lies here.
EDIT: I have written problem without WxWidgets (I load DLL using windows.h) - same problems. Here is the code: Getting trash data in char* while using it as buffer in function . Please help :(
This
DLL_PingConnection=(TYPE_DLL_PingConnection)
shouldn't it be
DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(wxT("DLL_PingConnection"));
?
seems otherwise you will not get a valid pointer to the function in the DLL.
as a general rule you should check return values, especially from a DLL
you load dynamically since it happens that you sometimes get another version
of the DLL which may have a function with same name but other signature or
where is missing entirely.
You named a function
DLL_PingConnection=(TYPE_DLL_PingConnection) dynLib2.GetSymbol(....
and call it with
OSOZ.OSOZ_PingConnection(buffor_ping_xml);
you typedef a function
typedef bool(*TYPE_DLL_PingConnection)(char*);
you create a variable
char buffor_ping_xml[2000];
in your typedef it is char* and your buffor_ping_xml is char
how can that work ?
try
char *buffor_ping_xml = new char[2000];
/* or */
wchar_t *buffor_ping_xml = new wchar_t[2000];
/* or */
wxChar *buffor_ping_xml = new wxchar[2000];
bool is_ping = DLL_PingConnection(buffor_ping_xml);
wxString mystring = wxString::FromUTF8(buffor_ping_xml);
write mystring to file.
To Do:
look in your wxwidgets\libs folder for your libs
are there libwxmsw29ud_* with a 'u' in the name (after version number here 29)?
If not You can not use unicode
If yes next steps
for all different test char *, wchar_t *, wxChar * give the files different name.
for example file_ping_xml.Open(wxT("C:\dll\ping_w_t_FromUTF8.xml"), ...
for wchar_t * in combination with
wxString mystring = wxString::FromUTF8(buffor_ping_xml);
also in combination with
wxString mystring(buffor_ping_xml);
Then check out the look like, of the files in a browser .
To test you can go to your wxWidgets sample folder . Compile in the folder C:\wxWidgets\samples\docview\docview.cpp . Open with docview.exe a unicode file . How does it look.
Unicode download file
Unicode-related compilation settings
You should define wxUSE_UNICODE to 1 to compile your program in Unicode mode. This currently works for wxMSW, wxGTK, wxMac and wxX11. If you compile your program in ANSI mode you can still define wxUSE_WCHAR_T to get some limited support for wchar_t type.
Here is answear: Getting trash data in char* while using it as buffer in function.
Thanks everyone - expecially for patience.
I have a WiFi Listener registered as a callback (pointer function) with a fixed 3rd party interface. I used a static member of my function to register the callback function and then that static function calls a nonstatic member through a static cast. The main problem is that I cannot touch the resulting char * buff with any members of my class nor can I even change an int flag that is also a member of my class. All result in runtime access violations. What can I do? Please see some of my code below. Other problems are described after the code.
void *pt2Object;
TextWiFiCommunication::TextWiFiCommunication()
{
networkDeviceListen.rawCallback = ReceiveMessage_thunkB;
/* some other initializing */
}
int TextWiFiCommunication::ReceiveMessage_thunkB(int eventType, NETWORK_DEVICE *networkDevice)
{
if (eventType == TCP_CLIENT_DATA_READY)
static_cast<TextWiFiCommunication *>(pt2Object)->ReceiveMessageB(eventType,networkDevice);
return 1;
}
int TextWiFiCommunication::ReceiveMessageB(int eventType, NETWORK_DEVICE *networkDevice)
{
unsigned char outputBuffer[8];
// function from an API that reads the WiFi socket for incoming data
TCP_readData(networkDevice, (char *)outputBuffer, 0, 8);
std::string tempString((char *)outputBuffer);
tempString.erase(tempString.size()-8,8); //funny thing happens the outputBuffer is double in size and have no idea why
if (tempString.compare("facereco") == 0)
cmdflag = 1;
return 1;
}
So I can't change the variable cmdflag without an access violation during runtime. I can't declare outputBuffer as a class member because nothing gets written to it so I have to do it within the function. I can't copy the outputBuffer to a string type member of my class. The debugger shows me strlen.asm code. No idea why. How can I get around this? I seem to be imprisoned in this function ReceiveMessageB.
Thanks in advance!
Some other bizzare issues include: Even though I call a buffer size of 8. When I take outputBuffer and initialize a string with it, the string has a size of 16.
You are likely getting an access violation because p2tObject does not point to a valid object but to garbage. When is p2tObject initialized? To what does it point?
For this to work, your code should look something like this:
...
TextWifiCommunication twc;
p2tObject = reinterpret_cast<void*>(&twc);
...
Regarding the string error, TCP_readData is not likely to null-terminate the character array you give it. A C-string ends at the first '\0' (null) character. When you convert the C-string to a std::string, the std::string copies bytes from the C-string pointer until it finds the null terminator. In your case, it happens to find it after 16 characters.
To read up to 8 character from a TCP byte stream, the buffer should be 9 characters long and all the bytes of the buffer should be initialized to '\0':
...
unsigned char outputBuffer[9] = { 0 };
// function from an API that reads the WiFi socket for incoming data
TCP_readData(networkDevice, (char *)outputBuffer, 0, 8);
std::string tempString((char *)outputBuffer);
...
Alright so I have an edit control and a static text control. When the user clicks a button, I want the program to take the text from the edit control and add it to the text in the static text control and then set that new value as the text in the static control. So I have a function that appends the buffers to one another but my program doesn't seem to be working. This is what I have:
//when button message is recieved:
SendMessage(hwndEditControl, WM_GETTEXT,255,(LPARAM)editbuffer);
GetWindowText(hwndTextControl, (LPWSTR)allText, GetWindowTextLength(hwndTextControl));
allText = appendStrings((char*)editbuffer, (char*)allText);
SetWindowText(hwndTextControl, (LPCWSTR)allText);}
// where appendStrings is defined as:
char* appendStrings (char* buffer1, char* buffer2)
{
std::string string1(buffer1), string2(buffer2);
std::string string3 = string1 + string2;
return (char*)string3.c_str();
}
//and
static char* editbuffer = new char;
static char* allText = new char; //these are defined as so
So anyway, when I push the button, I'm pretty sure that the appendStrings function is working because I think it takes what is in the editbox and adds it to the Text box. The reason I say "i think" though is because the string in the text box is always just jibberish. Its sometimes random symbols or just these "l's" (or what look like "L"s). I think it's a problem with my pointers but I'm not sure. I'm new to this so if there is an easier way, please tell me.
You're returning a pointer to a temporary that's being destroyed before the function returns.
Instead of returning a char *, return a std::string.
I am new to _bstr_t's and still trying to get the hang of it. I was trying to check whether a particular string x is contained anywhere within the bstring. Something I would normally do like;
String x = "hello";
String example = "You! hello there";
...
if (example.find(x) != string::npos) {
...
Just for the record the intended platform is windows.
There is no need to use _bstr_t. Use the BSTR type.
Next, read Eric's Complete Guide to BSTR Semantics.
Lastly, you can use the BSTR in native code the way you would a normal character array in most cases.
BSTR bstr = SysAllocString(L"FooBarBazQux");
if (wcsstr(bstr, L"Bar") != NULL) {
// Found it! Do something.
} else {
// Not there.
}
SysFreeString(bstr);
MSDN for wcsstr.
Your example appears to be trying to use string::find from STL. But you specify your variables of type "String" (capitalized). If you instead did:
using namespace std;
string x = "hello";
string example = "You! hello there";
...
your example would compile. Do you actually have a BSTR or _bstr_t that you need to work with that you haven't shown? Even if so, it's pretty easy to make an std::string from a _bstr_t, and after that you can use STL as you normally would.