trouble with very simple Win32 API

trouble with very simple Win32 API

Hi,

i am in the process of "translating" a QuickWin fortran application into a MDI Fortran Windows Application. I have little experience with Windows applications and i am stuck on a very basic problem.
In my quickwin application i have many dialog boxes with edit fields that receive user's input and others, read-only, that get updated based on the values entered. For example, enter a number in an edit field and put its square in another:

SUBROUTINE MyDialog !---define the dialog box
...
external Update
...

call Update(dlg, ID_EDIT_1, DLG_Change)
call Update(dlg, ID_EDIT_2, DLG_Change)
retlog = DlgSetSub(dlg, ID_EDIT_1, Update)
retlog = DlgSetSub(dlg, ID_EDIT_2, Update)

retint=DlgModal(dlg)
Call DlgUninit(dlg)
...
end SUBROUTINE

SUBROUTINE Update !---------Handle changes

use DFLOGM
character(256) text
integer control_ID
real MyVariable
...

select case(control_ID)
case(ID_EDIT_1)
retlog = DlgGet(dlg, ID_EDIT_1, text)
read(text, *, iostat=retint) MyVariable
write(text,9990) MyVariable**2
retlog = DlgSet(dlg, ID_EDIT_2, text)
end select

9990 format(f9.4)
....

END SUBROUTINE

Now i am having some serious problems in doing this very simple thing in a Fortran Windows Application.
Here's what i have:

intger*4 function MyDialog(hDlg, message, uParam, lParam)

integer MaxChar
real MyVariable
...
case (WM_INITDLG)
...

case (WM_COMMAND)

if (LOWORD(uParam) .eq. ID_EDIT_1) then
iret = GetDlgItemText(hDlg, ID_EDIT_1, text, &
MaxChar)
read(text, *, iostat=retint) MyVariable
write(text, 9990) MyVariable**2
iret = SetDlgItemText(hDlg, ID_EDIT_2, text//''C)
end if
...

This does not work (the edit field ID_EDIT_2 is not updated when i enter values in ID_EDIT_1).
However, if i don't use the "write", e.g.

if (LOWORD(uParam) .eq. ID_EDIT_1) then
iret = GetDlgItemText(hDlg, ID_EDIT_1, text, &
MaxChar)
iret = SetDlgItemText(hDlg, ID_EDIT_2, text//''C)
end if

things work fine (the ID_EDIT_2 field is updated in real time with a copy of whatever i enter in ID_EDIT_1).

What am i missing ? (i know it's trivial...)

yuric

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

You're using

read(text, *, iostat=retint) MyVariable

Since text is char(0) terminated, fortran sees it as an error when reading it (you didn't check retint!) and it reads 0 into MyVariable. You should be careful when mixing C-style and Fortran-style strings. I use the following routine to convert C to Fortran strings (reverse is trivial -- TRIM()//char(0)):

MODULE STRINGS
...
!===================================
FUNCTION C2F(sStr)
!Convert C string to Fortran string

CHARACTER*(*),INTENT(IN):: sStr
CHARACTER(LEN=LEN(sStr)):: C2F

INTEGER::                  nRes

nRes=SCAN(sStr,CHAR(0))
IF (nRes.EQ.0) THEN
      C2F=sStr
ELSE
      C2F=sStr(1:nRes-1)
END IF

END FUNCTION C2F
!=================================================
INTEGER FUNCTION CLEN(szString)
!"C" length of the string
CHARACTER(*),INTENT(IN):: szString

CLEN=SCAN(szString,CHAR(0))
IF (CLEN.EQ.0) CLEN=LEN(szString)

END FUNCTION CLEN

Jugoslav

Jugoslav
www.xeffort.com

...I forgot to say what I should have said first: you don't have to port dialog handling to Windows API. DFLOGM works quite fine with Win32 applications (unless you need some advanced stuff) and the code is IMHO simpler and better maintainable. (It does produce a slight memory overhang but this is negligible compared to translation and maintenance effort).

Jugoslav

Jugoslav
www.xeffort.com

Thank you so much, Jugoslav: you're just about the best resource on CVF programming i have ever encountered!

You're diagnosis of course was right (iostat was returning 59 for list-directed I/O syntax error) and your string-manipulating function did the job.

I am learning Win32 API kind of along the way, and i don't know how educational books such as Lawrence's (which i am reading) could forget to remind the reader about the "C-ness" of strings from edit fields (given the level of the book is so introductory.) I guess i am just plain dumb...

Have you yourself thought about writing a book? The practical info and code snippets you have would beinvaluable to _a lot_ of users in the sciences who don't do windows programming for a living but do numerical analysis (and FORTRAN) for a living.

To answer your question,i am aware DFLOGM can do a lot, but my problem is not knowing exactly what it can't do. I have built a rather large (and growing...) num. an. software for certain optical systems. Recently, I made it dialog-based for speeding up my personal use. Everything was DFLOGM-based: very straightforward. Now, i am seriously thinking about making a commercial product out of it (it wouldn't be bad to cash something on my research...) At this time, I do not know yet what's the best way to make commercial-user-friendly a lot of functionalities. That's why i decided to go for raw Win32 API: i don't want to find myself in a situation where i am stuck and cannot do exactly what i have in mind (because of DFLOGM limitations and/or my ignorance)

yuric

Thank you for your compliments; I actually did thought of writing a book, but unfortunately my present occupations prevent me even from finishing my thesis :-(. I have a regular job (and I'm far too young to retire :-)).

My short-term plans include completion of my (ambitiously conceived) XFT GUI library -- that also requires writing a lots of documentation and samples. See my replies to your other recent questions.

Jugoslav

Jugoslav
www.xeffort.com

If you want to sell to users, then you will want to take advantage of the XFLOGM capability for adding F1 help and context-sensitive help to dialogs. You will no doubt be writing your WinHelp help files as you go along using Help Workshop (freely downloadable from several sites)?
(search the forum for stuff on context help etc)
HTH

Leave a Comment

Please sign in to add a comment. Not a member? Join today