Print char[] to messagebox - visual-c++

I am taking the URL value from DocumentComplete and try to copy it to testDest[256]
Here is my code:
char *p= _com_util::ConvertBSTRToString(url->bstrVal);
for (int i = 0; i <= strlen(p); i++)
{
testDest[i] = p[i];
}
My question is, how can I print the testDest value on a messagebox?

The simplest way to create a message box is this:
MessageBox(NULL, testDest, "some title", 0);
If you already have a window and you want to associate the message box with that window, just change the first parameter from NULL to the window handle.
Also, if you're using Unicode, you should convert your char [] to TCHAR [] or otherwise call the ANSI version (MessageBoxA) explicitly.

You can do this
CString cstring( testDest);
AfxMessageBox(testDest);

Related

C++ How to parse math problem in string using TinyExpr?

First I need to detect if message from player contains math problem
like I get 2 messages from him
char* message1 = "Please solve this math problem";
char* message2 = "How much is 1 + 2 ?";
These messages are not static, player can say different messages.
I need to process message1 and message2
I need to detect math problem and cut it like see message2 and I need it to be "1+2"
char* mathProblemFromMessage = "1+2"; // <- I need to get this from message also detect if message contains any math problem before
double answer = te_interp(mathProblemFromMessage, 0);
printf("Answer is %f\n", answer);
my current code
std::string msg = Message;
if (func.contains_math_operators(msg.c_str()) && func.contains_number(msg.c_str()))
{
double answer = te_interp(Message, 0);
}
I think it fails and always results 0 because msg is "Player: 1+1" I need to cut math problem from it, I dont know how to do it...a
Find a position of the first digit in that string and get a substring from that point:
std::size_t found = msg.find_first_of("0123456789");
if(found != std::string::npos)
{
std::string sub = msg.substr(found);
double answer = te_interp(sub, 0);
}
If that also fails, try to trim invalid tail.
You may want to extend that find_first_of above to include open paren (, unary minus - or some supported function names (I am not familiar with TinyExpr)

CComboBox FindString empty

The MFC CComboBox allows a person to do an AddString of an empty string. I just proved that by doing a GetCount() before and another after the AddString; Count was 0, then it became 1; and the GUI also seems to reflect it, as its list was an huge empty box, and when adding it became a one-liner.
I also proved it further by doing
int a = m_combo.GetCount();
CString sx= _T("Not empty string");
if(a == 1)
m_combo.GetLBText(0, sx);
TRACE(_T("Start<%s>End"), sx);
and the Output window displays
File.cpp(9) : atlTraceGeneral - Start<>End
so we conclude the sx variable is empty.
Then I do a FindString having a CString m_name variable which is empty:
int idx= m_combo.FindString(-1, m_name);
And it returns CB_ERR!
Is it standard behavior for empty string entries? Official documentation doesn't say anything about it!
If it is, what is the simplest way to override it? Is there some parameter or change in the resources to change the behaviour? If there is not, I am thinking about deriving or composing a class just for the case where the string is Empty!
I did it manually for the empty string and it works!
CString sItem;
int idx_aux= CB_ERR;
// need it because FindString, FindStringExact and SelectSring return CB_ERR when we provide an empty string to them!
if(m_name.IsEmpty())
{
for (int i=0; i<m_combo.GetCount(); i++)
{
m_combo.GetLBText(i, sItem);
if(sItem.IsEmpty())
{
idx_aux= i;
break;
}
}
}
else
idx_aux= m_combo.FindString(-1, m_name);

clistbox gettext error

I am trying to add items from alistbox to cstringarray. But text is null always. plz suggest the changes
int count = m_OutList.GetCount();
for ( i = 0; i <m_OutList.GetCount(); i++)
{
m_OutList.GetText( buf[i], text );
m_selcomponents->Add(text);
// getb //Add();
}
If text is a CString in your example, you need to write m_OutList.GetText(i,text) - you don't need a buffer variable if you pass a CString&. buf[i] is a part of your buffer and has a random value.

CEdit index at which line-break happens

I am getting text from CEdit control. I need to imitate the same text on image. The edit control is multiline, so when I keep on typing long string the remaining part of the word goes to the next line.
I am not getting at which text index in the CEdit the character goes on the next line.
So while trying to do this, I am able to insert "\r\n" if the width of line exceed the width of the rect. But the problem is if the alignment is center the the character after the inserted "\r\n" is treated as new word by the DrawText api and purpose of the above insertion of new-line character fails.
Can someone let me know, Is there some way I can know where the line is breaking in CEdit or alternative for the same.
CString strTempFormattedText(_T(""));
LONG tempTotalWidth = 0;
for(int i = 0; i < myText.GetLength(); i++) {
TCHAR tempChar = myText.GetAt(i);
tempTotalWidth += pDC->GetTextExtent(CString(tempChar), 1).cx;
if(tempTotalWidth >= rc.Width()) {
tempTotalWidth = 0;
strTempFormattedText.Append(_T("\n\r"));
}
strTempFormattedText.AppendChar(tempChar);
}
myText = strTempFormattedText;
switch(ALIGNMENT)
{
case RIGHT:
pDC->DrawText(myText, -1, rc, DT_WORDBREAK | DT_RIGHT );
break;
case CENTER:
pDC->DrawText(myText, -1, rc, DT_WORDBREAK | DT_CENTER);
break;
case LEFT:
pDC->DrawText(myText, -1, rc, DT_WORDBREAK | DT_LEFT);
break;
}
Thanks
I think you should combine LineIndex, LineLength and PosFromChar to solve your problem. Using the API instead of the data could be useful if you will need access the more complex CRichEditText.

How do I use the IFileDialogCustomize::GetEditBoxText() method?

WCHAR wcText[100] = {0};
WCHAR *pText = wcText;
WCHAR **ppText = &pText;
HRESULT hr = pDialogCustomize->GetEditBoxText(dwCtrlID, ppText);
The edit box contains the text "5000" but this text isn't returned by the GetEditBoxText method.
How can I get the text from the edit box?
According to http://msdn.microsoft.com/en-us/library/bb775908%28VS.85%29.aspx, GetEditBoxText allocates and returns a string for you (that you must later free with CoTaskMemFree). I'm assuming that you're checking the wcText array after the function call, and it is empty? This is because GetEditBoxText is changing the value of pText.
Try:
WCHAR *pText = NULL;
HRESULT hr = pDialogCustomize->GetEditBoxText(dwCtrlID, &pText);
if (S_OK == hr && NULL != pText)
// function succeeded and pText points to a buffer of text that must free when done using

Resources