- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have this code
ret = SetWindowLongPtr(hwnd, GWLP_USERDATA, lData)
if (ret == 0) then
err = GetLastError()
write(errMsg, '(I0)') err ! Convert integer to string
errMsg = trim(adjustl(errMsg))
errMsg = errMsg // c_null_char
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // errMsg, "Error"C, MB_OK)
end if
I am getting an error '6' here which I can see when doing a line-by line debug. That error is another problem, but regardless of what I do I cannot get that '6' to be displayed in the MessageBox. I have tried multiple different combinations of trying to make it into a C string, but all I get in the end is the text followed by nothing.
Have tried // char(0) in place of c_null_char
I have use iso_c_binding
errMsg is declared as char(64) - I have tried different sizes
Anyone know how to do what should be such a simple task??
I
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You should use
write(errMsg, '(I0)') err ! Convert integer to string
errMsg = trim(errMsg) // c_null_char
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // errMsg, "Error"C, MB_OK)
or
write(errMsg, '(I0,a)') err,c_null_char ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // errMsg, "Error"C, MB_OK)
or
write(errMsg, '(I0)') err ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // trim(errMsg)//c_null_char, "Error"C, MB_OK)
With your code the null character can't be appended to errMsg because errMsg//c_null_char has a length of 65 characters so it is truncated to 64 when assigned back to errMsg.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You should use
write(errMsg, '(I0)') err ! Convert integer to string
errMsg = trim(errMsg) // c_null_char
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // errMsg, "Error"C, MB_OK)
or
write(errMsg, '(I0,a)') err,c_null_char ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // errMsg, "Error"C, MB_OK)
or
write(errMsg, '(I0)') err ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // trim(errMsg)//c_null_char, "Error"C, MB_OK)
With your code the null character can't be appended to errMsg because errMsg//c_null_char has a length of 65 characters so it is truncated to 64 when assigned back to errMsg.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I did this
integer*4 err
character(len = 16) :: errStr
character(len = 16, kind = c_char) :: errMsg
.... (some more code)
ret = SetWindowLongPtr(hwnd, 0, lData)
if (ret == 0) then
err = GetLastError()
write(errStr, '(I0)') err ! Converts 'err' integer to string
! WRITE won’t work directly on a c_char character variable so
! need two variable, errStr and errMsg. The latter is declared
! as a c-type character, errStr is not.
! Note that Fortran requires a separate buffer for integer-to-string conversion.
errMsg = trim(adjustl(errStr)) // C_NULL_CHAR ! Trim off extra and Null-terminate
ret = MessageBox(ghwndMain,"SetWindowLongPtr failed with error " // errMsg, &
"Error"C, MB_OK)
end if
for the commented reasons and it worked. Your solution looks a lot simpler but it also looks like something I once tried. I have not tried it again but its relative simplicity makes it worth a retry.
I tried your middle solution
write(errMsg, '(I0,a)') err,c_null_char ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "C // errMsg, "Error"C, MB_OK)
and it did not work. errMsg was declared as
'character(16) errMsg'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
write(errMsg, '(I0)') err ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "// trim(errMsg)//c_null_char, "Error"C, MB_OK)
I would expect the null in the middle of the string after ...Error Code: " would cause an issue giving a shortened string (not tested)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The null character is at the end of the message string
"SetWindowLongPtr in WM_CREATE failed! Error Code: "// trim(errMsg)//c_null_char
The second argument "Error"c is the messagebox window caption
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think you should read the previous posts again. My post had the spurious C removed but several of the previous posts have it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
OK I missed that other error in code. The error number will never be displayed.
write(errMsg, '(I0,a)') err,c_null_char ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: " // errMsg, "Error"C, MB_OK)
I would have written this portion of code like that
write(errMsg, '(a,I0)') "SetWindowLongPtr in WM_CREATE failed! Error Code: ",err ! Convert integer to string
ret = MessageBox(hwnd, trim(errMsg)//c_null_char, "Error"C, MB_OK)
I always convert to C string only where it is necessary.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I never use that "C" it is not standard Fortran. For MessageBox and some similar functions I tend to make a simple wrapper routine that internal handles string termination so the caller can work with Fortran things not C things and you can minimise the SDK clutter in the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So do I, I even create a cstring() function that returns the fortran string appended with the char(0). So on Fortran side, I have only Fortran strings.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@andrew_4619 I have kept to my solution and have removed the "C in the MessageBox in favor of c_null_char to make it more standardized.
But the real issue I had with this process was converting the fortran integer to a C string for MessageBox. The final solution to that headache was that I needed to declare two different types of strings, one for fortran and one for C. Printing strings into MessageBox was never a problem.
Who decides what is the accepted solution? I did not check that option. I had tried all those options of the accepted solution and none of them worked before I even posted this issue. The prints worked fine but no number was displayed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This code doesn't work?
write(errMsg, '(I0)') err ! Convert integer to string
ret = MessageBox(hwnd, "SetWindowLongPtr in WM_CREATE failed! Error Code: "//trim(errMsg)//c_null_char, "Error"C, MB_OK)

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page