Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29261 Discussions

SetWindowText apparent misbehavior?

richardduncan
Beginner
1,019 Views
Hello
Its entirely possible that I'm doing something goofy, but I can't understand why the following code doesn't work as one might reasonably expect.
Based on the test outputs below, the SetWindowText command would appearto bealtering a variable when it should not. Also strange is how the 'fieldText' variable seems to always retain the correct value when shown through a write statement, but not when used with SetWindowText.
Code:
  integer(2), parameter :: STR_LENGTH = 16
  character(STR_LENGTH) :: fieldText
  integer(2) :: SET_EM_MAXITS
  SET_EM_MAXITS = 1000
  write(fieldText, '(I0)') SET_EM_MAXITS
  ! the '17' isn't anything special here:
  write(17, *) "set1: ", SET_EM_MAXITS, TRIM(fieldText)  
  ! this write statement produces the following:
  ! set1:    1000 1000
  ! ?? what is wrong with this line: ??
  iret = SetWindowText(hEditMaxits, TRIM(fieldText))
  write(17, *) "set2: ", SET_EM_MAXITS, TRIM(fieldText), iret
  ! this write statement produces the following:
  ! set2:     100 1000           1
The problem is that when this set of code is called,displayed in the edit box with handle hEditMaxits is an incorrect value of 100 instead of the desired 1000.
Incidentally,the same problem occurs with/without either the TRIM() and/or any ''C string terminator. Also it occurs when compiled in both Debug and Release configurations.
Thanks in advance for any insights - Richard
Win32 application written using CVF 6.6c
0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
1,019 Views
For the start, you must char(0)-terminate any string that goes to Win32 API functions (except for few where it's explicitly said that you need not, like TextOut). So, you need either
SetWindowText(hEditMaxits, TRIM(fieldText)//char(0)
or
write(fieldText, '(I0,A)') SET_EM_MAXITS,char(0)
,whichever is more convenient. (Also, SetDlgItemInt(hWndParent, idEditControl, int4(SET_EM_MAXITS)) should also work).

Message Edited by JugoslavDujic on 04-18-200606:23 PM

0 Kudos
richardduncan
Beginner
1,019 Views
Indeed Jugoslav, thanks.

However, even with the appropriate null terminators placed on the string, this same problem exhibits itself.

Also, using SetDlgItemInt resulted in no change to the write statements, nor to the text populated in the edit box.

Richard
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,019 Views
Then, you must have a pretty hidden bug elsewhere. I assume you have array bounds checking switched on? However, it's not a panacea -- there are the cases where it cannot catch the memory overwrite.

Few hints as to how to approach the problem:

- Open "Memory" debug window and type LOC(SET_EM_MAXITS) there. Go step by step and watch which statement alters the value.
- In Debug/Breakpoints/Data you can do a similar thing (type SET_EM_MAXITS) and you'll get notified when the value changes (note that the feature is, erm, quirky).
- In "Memory" window you can also verify that you're correctly writing terminal char(0) into the string.

I doubt it has anything to do with SetWindowText. Does SendMessage(WM_SETTEXT) produce the same behavior?
0 Kudos
Reply