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
Related
I have a string coming from PC through serial to a microcontroller (Arduino), e.g.:
"HDD: 55 - CPU: 12.6 - Weather: Cloudy [...] $";
by this function I found:
String inputStringPC = "";
boolean stringCompletePC = false;
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputStringPC += inChar;
if (inChar == '$') // end marker of the string
{
stringCompletePC = true;
}
}
}
I would like to extract the first number of it after the word HDD, CPU and also get the string after Weather (ie "cloudy"); my thinking is something like that:
int HDD = <function that does that>(Keyword HDD);
double CPU = <function that does that>(Keyword CPU);
char Weather[] = <function that does that>(Keyword Weather);
What is the right function to do that?
I looked into inputStringSerial.indexOf("HDD") but I am still a learner to properly understand what it does and don't know if theres a better function.
My approach yielded some syntax errors and confused me with the difference in usage between "String inputStringSerial" (class?) and "char inputStringSerial[]" (variable?). When I do 'string inputStringSerial = "";' PlatformIO complains that "string" is undefined. Any help to understand its usage here is greatly appreciated.
Thanks a bunch.
The String class provides member functions to search and copy the contents of the String. That class and all its member functions are documented in the Arduino Reference:
https://www.arduino.cc/reference/tr/language/variables/data-types/stringobject/
The other way a list of characters can be represented is a char array, confusingly also called a string or cstring. The functions to search and copy the contents of a char array are documented at
http://www.cplusplus.com/reference/cstring/
Here is a simple Sketch that copies and prints the value of the Weather field using a String object. Use this same pattern - with different head and terminator values - to copy the string values of the other fields.
Once you have the string values of HDD and CPU, you'll need to call functions to convert those string values into int and float values. See the String member functions toInt() and toFloat() at
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/toint/
or the char array functions atoi() and atof() at
http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi
String inputStringPC = "HDD: 55 - CPU: 12.6 - Weather: Cloudy [...] $";
const char headWeather[] = "Weather: "; // the prefix of the weather value
const char dashTerminator[] = " -"; // one possible suffix of a value
const char dollarTerminator[] = " $"; // the other possible suffix of a value
void setup() {
int firstIndex; // index into inputStringPC of the first char of the value
int lastIndex; // index just past the last character of the value
Serial.begin(9600);
// find the Weather field and copy its string value.
// Use similar code to copy the values of the other fields.
// NOTE: This code contains no error checking for unexpected input values.
firstIndex = inputStringPC.indexOf(headWeather);
firstIndex += strlen(headWeather); // firstIndex is now the index of the char just past the head.
lastIndex = inputStringPC.indexOf(dollarTerminator, firstIndex);
String value = inputStringPC.substring(firstIndex, lastIndex);
Serial.print("Weather value = '");
Serial.print(value);
Serial.println("'");
}
void loop() {
// put your main code here, to run repeatedly:
}
When run on an Arduio Uno, this Sketch produces:
Weather value = 'Cloudy [...]'
I created a custom CCustomCombo by extending CComboBox to implement a DrawItem() function. Here's the code for it.
void CCustomCombo::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
ASSERT( lpDrawItemStruct->CtlType == ODT_COMBOBOX );
LPCTSTR lpszText = ( LPCTSTR ) lpDrawItemStruct->itemData;
ASSERT( lpszText != NULL );
if ( lpDrawItemStruct->itemID == -1 || lpszText == NULL)
return;
CDC dc;
dc.Attach( lpDrawItemStruct->hDC );
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Erase
// the rect by filling it with the background color.
if ( ( lpDrawItemStruct->itemAction & ODA_SELECT ) &&
( lpDrawItemStruct->itemState & ODS_SELECTED ) )
{
dc.SetTextColor( ::GetSysColor( COLOR_HIGHLIGHTTEXT ) );
dc.SetBkColor( ::GetSysColor( COLOR_HIGHLIGHT ) );
dc.FillSolidRect( &lpDrawItemStruct->rcItem, ::GetSysColor( COLOR_HIGHLIGHT ) );
}
else
{
dc.FillSolidRect( &lpDrawItemStruct->rcItem, crOldBkColor );
}
// Draw the text.
dc.DrawText(
lpszText,
( int ) _tcslen( lpszText ),
&lpDrawItemStruct->rcItem,
DT_CENTER | DT_SINGLELINE | DT_VCENTER );
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor( crOldTextColor );
dc.SetBkColor( crOldBkColor );
dc.Detach();
}
creation part -
m_selectionCombo.Create( WS_VSCROLL |
CBS_DROPDOWNLIST | WS_VISIBLE | WS_TABSTOP| CBS_OWNERDRAWFIXED,
rect, &m_wndSelectionBar, ID_TEMP_BTN))
Now the problem is with the adding string items to the combobox. When I'm using string objects, it always shows some unicode gibberish.
m_selectionCombo.InsertString(0, "One"); //works
char * one = "one";
m_selectionCombo.InsertString(0, one ); //works
CString one = "one";
m_selectionCombo.InsertString(0, one ); //shows gibberish
std::string one = "one";
char *cstr = &one[0];
m_wndSelectionBar.m_selectionCombo.InsertString(0, cstr ); //shows gibberish
The same results appear for AddString. The problem is that I have a set of doubles that I have to insert to the combobox. And I have no way of converting them to string without displaying gibberish. I tried half-a-dozen conversion methods and none worked. I'm literally at my wits end!
The funny thing is it worked perfectly before when I used CComboBox and not my CCustomCombo class/CBS_OWNERDRAWFIXED. I tried using CBS_HASSTRINGS, but it displayed nothing, not even the gibberish, so somehow the strings don't even get added in with CBS_HASSTRINGS.
I need the custom Draw method since I plan to highlight some of the dropdown items. I'm using windows 32, VS 2017.
Any help would be highly appreciated.
Thanks.
LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData;
OwnerDraw function is looking at itemData. itemData is assigned using CComboBox::SetItemData. It is not assigned using InsertString or other text functions.
char * one = "one";
m_selectionCombo.InsertString(0, one ); //works
The string and item data are stored in the same memory address when CBS_HASSTRINGS is not set.
See also documentation for CB_SETITEMDATA
If the specified item is in an owner-drawn combo box created without
the CBS_HASSTRINGS style, this message replaces the value in the
lParam parameter of the CB_ADDSTRING or CB_INSERTSTRING message that
added the item to the combo box.
So basically itemData returns a pointer one, and it works fine in this case.
CString one = "one";
m_selectionCombo.InsertString(0, one ); //shows gibberish
This time the string is created on stack, it is destroyed after the function exists. itemData points to invalid address.
Solution:
If you are setting the text using InsertString/AddString then make sure CBS_HASSTRINGS is set. And read the strings using GetLBText. Example:
//LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData; <- remove this
if(lpDrawItemStruct->itemID >= GetCount())
return;
CString str;
GetLBText(lpDrawItemStruct->itemID, str);
LPCTSTR lpszText = str;
Otherwise use SetItemData to setup data, and use itemData to read.
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.
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);
How to copy a folder from one drive to other drive in VC++ ...?
I have come this far
String^ SourcePath = Directory::GetCurrentDirectory();
String^ DestinationPath = "c:\\Test";
CString s(SourcePath) ;
CString d(DestinationPath);
Directory::CreateDirectory(DestinationPath);
SHFILEOPSTRUCT* pFileStruct = new SHFILEOPSTRUCT;
ZeroMemory(pFileStruct, sizeof(SHFILEOPSTRUCT));
pFileStruct->hwnd = NULL;
pFileStruct->wFunc = FO_COPY;
pFileStruct->pFrom = (LPCWSTR)s;//"D:\test_documents\test1.doc";
pFileStruct->pTo = (LPCWSTR)d;
pFileStruct->fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR ;
bool i = pFileStruct->fAnyOperationsAborted ;
int status = SHFileOperation(pFileStruct);
if(status == 0)
{
return true;
}
return false;
the status is showing 2 instead of zero , can some one tell me why..?
Usually a String^ points to a managed string object. The SHFILOPSSTRUCT must befilled with pointers to unmanaged wchar_t. So you must pin the strings and convert. You tried to use the CString class as conversion helper.
Use the PtrToStringChars instead to get valid strings in pTo and pFrom:
http://msdn.microsoft.com/en-us/library/d1ae6tz5(VS.80).aspx
The read of the fAnyOperationsAborted member is not required for the operation.