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

How to call a function that requries a unicode string

michael_green
Beginner
1,450 Views

Hi All,

I have been trying to write a Fortran program that will communicate with a PDA running Windows CE. The PDA is synchronised with my desktop running Windows XP. For this to work I need to use a number of the CE* functions, such as CeFindFirstFile, CeFindNextFile, etc. While I can get these functions to work using some bad C++ programming, I really need them to work in Fortran, but they will not because I do not know how to pass to them the Unicode character strings they need as arguments. (At least I think that's what the problem is.)

This does not work in Fortran:

character(512)   wildpath

wildpath = '\\Windows\\Programs\\WQA\\*.*'//char(0)
hFindFile = CeFindFirstFile(wildpath,loc(WFDFile))

But this does work in C++

wchar_t   wildpath[256]

wcscat(wildpath,L"\\Windows\\Programs\\WQA\\*.*");
hFindFile = CeFindFirstFile(wildpath,(LPCE_FIND_DATA) &WFDFile);

I am reluctant to believe there is something that C++ can do that Fortran 90/95 cannot. Please can someone enlighten me.

With many thanks

Mike

0 Kudos
5 Replies
Anthony_Richards
New Contributor I
1,450 Views
Unicode strings require the use of WIDE characters defined using 16 bits rather than the usual 8 bits. That is what "wchar_t " defines in your C++ code. So each character is coded using two bytes rather than the usual one. You just need to use the MultiByteToWideChar API function to translate from an ANSI string to the UNICODE representation of the same string. What I think you end up with is the original set of bytes interleaved with "00" and terminated with a double "00"
0 Kudos
michael_green
Beginner
1,450 Views
Thanks for the reply. Unfortunately I can't get the program to compile when using MultiByteToWideChar because its 5th argument must be a wide character string, and that's just what I don't seem to have access to in Fortran. The question therefore boils down to: how does one declare a wide character string in Fortran? Many thanks Mike
0 Kudos
IanH
Honored Contributor III
1,450 Views
On current ifort on Windows use [fortran]USE IFWINTY INTEGER(WCHAR) :: wide_character_string(number_of_characters)[/fortran]
0 Kudos
Anthony_Richards
New Contributor I
1,450 Views
michaelgreen wrote:

Thanks for the reply.

Unfortunately I can't get the program to compile when using MultiByteToWideChar because its 5th argument must be a wide character string, and that's just what I don't seem to have access to in Fortran. The question therefore boils down to: how does one declare a wide character string in Fortran?

Many thanks
Mike

If your character string MYSTRING is, say, 20 characters long, then define an INTEGER(2) buffer to receive the wide string and make it more than 2*(20+1) bytes long: CHARACTER*20 MYSTRING INTEGER(2) MYWIDESTRING(50) Then use the location of MYWIDESTRING as the buffer to receive the wide character string. Here is an example program that compiles and works to produce an array of bytes containing the wide-character version of an input character string. !**************************************************************************** ! ! PROGRAM: multibytecharacters ! ! PURPOSE: Convert a character string to an array of wide characters ! represented as 2-byte integers. ! !**************************************************************************** program multibytecharacters use dfwin implicit none character*40 mystring integer(2), allocatable :: mywidestring(:) integer iret, isize, i mystring="this is my null-terminated string"//char(0) ! Find out how big the receiving buffer should be, in wide characters... ! by setting the buffer length isize initially to zero isize=0 iret=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, mystring, len_trim(mystring),mywidestring,isize ) ! iret contains the required buffer length in 2-byte characters, so allocate the required number allocate(mywidestring(iret)) print *,'required buffer size,iret =',iret if(size(mywidestring).ge.iret) then ! Do the conversion... isize=iret iret=MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED, mystring, len_trim(mystring),mywidestring,isize ) print *,'Number of multibyte characters written,isize=',isize print *, mystring(1:len_trim(mystring)) print *, (mywidestring(i),i=1,isize) else print *,'Buffer length required = ', iret, ', buffer size =',size(mywidestring) endif pause deallocate(mywidestring) end program multibytecharacters
0 Kudos
michael_green
Beginner
1,450 Views
Hi Guys, Many thanks for your help - all fixed now. Cheers Mike
0 Kudos
Reply