SCI_GETTEXT - Scintilla, how do I get the text ?
-
Hi, I am wondering how I can get the text from an scintilla text control ? This is my code, but the debugger says that data has a bad pointer and I have no text in it.
TCHAR \*data; UINT nLength; nLength = SendMessage(hWndEditor,SCI\_GETLENGTH,0,0); // allocate buffer; memset(&data,0,sizeof(TCHAR)\*nLength+1); // get the data data = (TCHAR\*)SendMessage(hWndEditor, SCI\_GETTEXT,nLength+1,0); // show the data MessageBox(NULL,data,L"MyApp",0); // clear the buffer free(data);
Thanks for help. bye, gabbana
gabbana wrote:
data = (TCHAR*)SendMessage(hWndEditor, SCI_GETTEXT,nLength+1,0);
shouldn't that be
nLength = SendMessage(hWndEditor, SCI_GETTEXT,nLength+1, (LPARAM)data);
? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Sunday, October 26, 2008 2:41 PM
-
gabbana wrote:
data = (TCHAR*)SendMessage(hWndEditor, SCI_GETTEXT,nLength+1,0);
shouldn't that be
nLength = SendMessage(hWndEditor, SCI_GETTEXT,nLength+1, (LPARAM)data);
? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]modified on Sunday, October 26, 2008 2:41 PM
-
Hmm, I get an error: cannot convert from TCHAR* to UINT And the last parameter i needed to cast (LPARAM)data.
In fact I forgot a cast. I fixed (there also was a wrong cast,by cut 'n' paste, actually) my previous reply, please see it again. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
In fact I forgot a cast. I fixed (there also was a wrong cast,by cut 'n' paste, actually) my previous reply, please see it again. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Okay, but without luck. Iam not sure what the error exactly is, but if I have a breakpoint at the last SendMessage and look in the Debugger &data have a value of 0x00... and Bad Ptr And the MessageBox is empty. The code looks like this:
UINT length = SendMessage(hWndEditor, SCI\_GETLENGTH, 0, 0); TCHAR \*data; memset(&data, 0, sizeof(TCHAR)\*length+1); length = SendMessage(hWndEditor, SCI\_GETTEXT,length+1,(LPARAM)data); MessageBox(NULL,data,L"Test",0); free(data);
At the end of the function I also get this error: Stack around the variable 'data' was corrupted
modified on Sunday, October 26, 2008 11:16 PM
-
Okay, but without luck. Iam not sure what the error exactly is, but if I have a breakpoint at the last SendMessage and look in the Debugger &data have a value of 0x00... and Bad Ptr And the MessageBox is empty. The code looks like this:
UINT length = SendMessage(hWndEditor, SCI\_GETLENGTH, 0, 0); TCHAR \*data; memset(&data, 0, sizeof(TCHAR)\*length+1); length = SendMessage(hWndEditor, SCI\_GETTEXT,length+1,(LPARAM)data); MessageBox(NULL,data,L"Test",0); free(data);
At the end of the function I also get this error: Stack around the variable 'data' was corrupted
modified on Sunday, October 26, 2008 11:16 PM
-
gabbana wrote:
TCHAR *data; memset(&data, 0, sizeof(TCHAR)*length+1);
I think before memset you should allocate memory for data. You can insert following statement before memset.
data = new TCHAR[length+1];
I hope it helps.
Regards, Sandip.
I get the same error. What I found now is, that if i only allocate memory with your code and removing the memset and free functions I get no error, but the MessageBox and the gives me some crazy char like chinese. (I think I should mention that I use UNICODE)
-
I get the same error. What I found now is, that if i only allocate memory with your code and removing the memset and free functions I get no error, but the MessageBox and the gives me some crazy char like chinese. (I think I should mention that I use UNICODE)
one thing i missed do not use free when you are allocating memory with new. use delete instead of free.
gabbana wrote:
(I think I should mention that I use UNICODE)
Have you set the corresponding properties for using unicode. In the documentation i see following messages. SCI_SETKEYSUNICODE(bool keysUnicode) SCI_GETKEYSUNICODE You can try setting keysUnicode to TRUE in message SCI_SETKEYSUNICODE I hope it helps.
Regards, Sandip.
modified on Monday, October 27, 2008 4:48 AM
-
one thing i missed do not use free when you are allocating memory with new. use delete instead of free.
gabbana wrote:
(I think I should mention that I use UNICODE)
Have you set the corresponding properties for using unicode. In the documentation i see following messages. SCI_SETKEYSUNICODE(bool keysUnicode) SCI_GETKEYSUNICODE You can try setting keysUnicode to TRUE in message SCI_SETKEYSUNICODE I hope it helps.
Regards, Sandip.
modified on Monday, October 27, 2008 4:48 AM
Okay, now I am using delete. The stack corrupted message already exists. The error must be the memset, because if i delete that line I am not getting an error. The problem with the chinse chars already exists! hWndEditor is the handle to sintilla text control. I declared it in my header file HWND hWndEditor and created the control in the WM_CREATE message:
hWndEditor = CreateWindowEx(0,
_T("Scintilla"),
_T(""),
WS_CHILD | WS_VISIBLE,
0,
0,
500,
500,
hWnd,
(HMENU)1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);EDIT: Yes i send the following message in WM_CREATE after the control is created: SendMessage(hWndEditor, SCI_SETKEYSUNICODE, true, 0);
-
Okay, now I am using delete. The stack corrupted message already exists. The error must be the memset, because if i delete that line I am not getting an error. The problem with the chinse chars already exists! hWndEditor is the handle to sintilla text control. I declared it in my header file HWND hWndEditor and created the control in the WM_CREATE message:
hWndEditor = CreateWindowEx(0,
_T("Scintilla"),
_T(""),
WS_CHILD | WS_VISIBLE,
0,
0,
500,
500,
hWnd,
(HMENU)1,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);EDIT: Yes i send the following message in WM_CREATE after the control is created: SendMessage(hWndEditor, SCI_SETKEYSUNICODE, true, 0);
Does nobody has an idea how to fix the problem ? By the way: Is Scintilla a good choice, or would it be better to use the rich text edit control and write own functions to extent it, because i am learning c++, and probably it would be better for learning.
-
Does nobody has an idea how to fix the problem ? By the way: Is Scintilla a good choice, or would it be better to use the rich text edit control and write own functions to extent it, because i am learning c++, and probably it would be better for learning.
I believed the reason for the "chinese" chars is because you have NOT established memory for the varable yet. Always, always zero out a varable before it is used: Example: CString str;(you've declared it and it resides in memory, but what is presently at that memory location?) just do this str = ""; or in Unicode: str = _T(""); Now that location is clean for str. I don't know anything about "Scintilla" I've always used the rich edit control methods. Codeproject has multiple examples that show how. A C++ programming language novice, but striving to learn