Announcement

Collapse
No announcement yet.

Sizeof for Unicode

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sizeof for Unicode

    Hi All

    Code below GPF's if nSize is > 260 odd characters. %MAX_PATH is 260 but SIZEOF unicode string returns x 2
    Is there another command to return the correct "size" in wchar count?



    Code:
    #COMPILE EXE
    #DIM ALL
    %UNICODE =1
    #INCLUDE "win32api.inc"          'Windows API
    FUNCTION PBMAIN () AS LONG
    
        LOCAL TempPath AS WSTRINGZ * %MAX_PATH
        LOCAL TempFileName AS WSTRINGZ * %MAX_PATH
    
        LOCAL lresult, nSize AS LONG
        LOCAL sTmp AS STRING
    
        nSize =  SIZEOF(TempPath)
        lresult= GetTempPath(nSize, TempPath) ' <-- GPF here if nSize > 260 So instead of SIZEOF what should be used
    
        FUNCTION = GetTempFileName(TempPath, "", 0, TempFileName) 'Creates Zero byte file
        sTmp = TempFileName
        KILL sTmp
        MSGBOX sTmp
    
    END FUNCTION

  • #2
    nSize = SIZEOF(TempPath) \ 2
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3

      AS WSTRINGZ * %MAX_PATH should be AS WSTRINGZ * (%MAX_PATH + 1) and according to https://docs.microsoft.com/en-us/win...i-gettemppathw the max length is 260 (+1 for the final NUL)
      <b>George W. Bleck</b>
      <img src='http://www.blecktech.com/myemail.gif'>

      Comment


      • #4
        SIZEOF() returns size in bytes, The other statements refer to WSTRINGZ characters which are two bytes wide each.

        So, as said, divide SIZEOF's return by 2.

        Dale

        Comment


        • #5
          Originally posted by José Roca View Post
          nSize = SIZEOF(TempPath) \ 2
          Sure, but that defeats the %UNICODE for same function to be used ANSI/Unicode

          Dale / George

          According to Microsoft For Unicode platforms, TCHAR is defined as synonymous with the WCHAR type. I am using GetTempPathW

          I was looking for a NoOfChar() for Unicode in case I missed something

          Comment


          • #6
            Right each WCHAR character is 2 bytes. To get NoOfChar() divide SIZEOF by 2. In ANSI version do not divide by 2. Nothing defeated because the functions needed to know which kind of string is being passed.
            Dale

            Comment


            • #7
              Ok, I guess the point I wanted to make is that SIZEOF should be unicode aware.

              Take this example
              Code:
              LOCAL MyWstringz AS WSTRINGZ * 10
              LOCAL MyString AS WSTRING
              MyString = STRING$$(SIZEOF(MyWstringz), "X")
              So bug or no bug?
              Can certainly lead to buffer overflows.

              Comment


              • #8
                Code:
                #Compile Exe
                #Include "Win32API.inc"
                
                '----------------------------------------------------------------------------(')
                
                Function PBMain( )
                    Local v_strzANSI As STRINGZ * ( %MAX_PATH + 1 )
                    Local v_wstrzUnicode As WSTRINGZ * ( %MAX_PATH + 1 )
                    MsgBox "ANSI String len=" & Format$( SizeOf( v_strzANSI ))
                    MsgBox "Unicode String len=" & Format$( SizeOf( v_wstrzUnicode ))
                    MsgBox "Therefore you need to use ( SizeOf( v_wstrzUnicode ) - 2 ) / 2 to get... " & Format$(( SizeOf( v_wstrzUnicode ) - 2 ) / 2 )
                End Function
                <b>George W. Bleck</b>
                <img src='http://www.blecktech.com/myemail.gif'>

                Comment


                • #9
                  use LEN(). Your code modified. MSGBOX shows 15 which is count of used wide characters. (vs string's set size)
                  Code:
                  #compile exe
                  #dim all
                  %UNICODE =1
                  #include "win32api.inc"          'Windows API
                  function pbmain () as long
                  
                      local TempPath as wstringz * %max_path
                     ' LOCAL TempFileName AS WSTRINGZ * %MAX_PATH
                  
                      local lresult, nSize, nLen as long
                      local sTmp as string
                      TempPath = "123456789012345"
                      nSize =  sizeof(TempPath)
                      nLen  = len(TempPath)
                      ? str$(nSize) + " " + str$(nLen)
                   '  lresult= GetTempPath(nSize, TempPath) ' <-- GPF here if nSize > 260 So instead of SIZEOF what should be used
                  
                    '  FUNCTION = GetTempFileName(TempPath, "", 0, TempFileName) 'Creates Zero byte file
                   '  sTmp = TempFileName
                    '  KILL sTmp
                   '   MSGBOX sTmp
                  
                  end function
                  Dale

                  Comment


                  • #10
                    Dale, LEN() works if the string is populated only. My question has been answered thanks.
                    There is no function to return the length of a fixed length unicode string in wide character count.

                    Comment


                    • #11
                      SIZEOF tells you the size of reserved block of memory, it is not a specific string based function.

                      Examples: SIZEOF on a DWORD variable gives 4 bytes, SIZEOF a UDT gives you the size of the structure. Therefore SIZEOF a WSTRINGZ should give you the memory length of the reservation, not the TCHAR count. If it gave you the TCHAR count it would not follow it's intention.

                      PowerBASIC uses UTF-16 which uses 2 bytes (unsigned) for each character, so a division by 2 of the SIZEOF works as expected.
                      <b>George W. Bleck</b>
                      <img src='http://www.blecktech.com/myemail.gif'>

                      Comment


                      • #12
                        Originally posted by Carlo Pagani View Post
                        Ok, I guess the point I wanted to make is that SIZEOF should be unicode aware.

                        Take this example
                        Code:
                        LOCAL MyWstringz AS WSTRINGZ * 10
                        LOCAL MyString AS WSTRING
                        MyString = STRING$$(SIZEOF(MyWstringz), "X")
                        So bug or no bug?
                        Can certainly lead to buffer overflows.
                        NO bug. You just need to be aware that SIZEOF returns the length in bytes, not characters,



                        Comment

                        Working...
                        X