User Tools

Site Tools


calling_20dll_20functions_20that_20return_20strings

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

calling_20dll_20functions_20that_20return_20strings [2018/03/31 13:19]
127.0.0.1 external edit
calling_20dll_20functions_20that_20return_20strings [2018/04/14 17:08] (current)
richardrussell Added syntax highlighting
Line 5: Line 5:
   * The string must be long enough to receive the returned data   * The string must be long enough to receive the returned data
   * The string must have an explicit NUL (**CHR$0**) termination   * The string must have an explicit NUL (**CHR$0**) termination
-\\  For example suppose you want to call the [[https://​msdn.microsoft.com/​en-us/​library/​windows/​desktop/​ms724295.aspx|GetComputerName]] API. MSDN states that the maximum length of the returned string is MAX_COMPUTERNAME_LENGTH characters so the required code is as follows:\\ +\\  For example suppose you want to call the [[https://​msdn.microsoft.com/​en-us/​library/​windows/​desktop/​ms724295.aspx|GetComputerName]] API. MSDN states that the maximum length of the returned string is MAX_COMPUTERNAME_LENGTH characters so the required code is as follows: 
 + 
 +<code bb4w>
         MAX_COMPUTERNAME_LENGTH = 15         MAX_COMPUTERNAME_LENGTH = 15
         computer$ = STRING$(MAX_COMPUTERNAME_LENGTH + 1, CHR$0)         computer$ = STRING$(MAX_COMPUTERNAME_LENGTH + 1, CHR$0)
Line 12: Line 14:
         computer$ = LEFT$(computer$,​ size%)         computer$ = LEFT$(computer$,​ size%)
         PRINT computer$         PRINT computer$
-Note particularly the initialisation of the string **computer$** to be long enough to contain the maximum length of returned string, and to have an explicit NUL termination.\\ \\  Here is another example, the conversion of a string from **UTF-8** encoding to **UTF-16** encoding. This time the maximum length of the returned string isn't known in advance, so it is first necessary to find out what that length is in order to initialise the string to a sufficient length:\\ +</​code>​ 
 + 
 +Note particularly the initialisation of the string **computer$** to be long enough to contain the maximum length of returned string, and to have an explicit NUL termination.\\ \\  Here is another example, the conversion of a string from **UTF-8** encoding to **UTF-16** encoding. This time the maximum length of the returned string isn't known in advance, so it is first necessary to find out what that length is in order to initialise the string to a sufficient length: 
 + 
 +<code bb4w>
         CP_UTF8 = &FDE9         CP_UTF8 = &FDE9
         utf8$ = "​демонстрация"​         utf8$ = "​демонстрация"​
Line 19: Line 25:
         SYS "​MultiByteToWideChar",​ CP_UTF8, 0, utf8$, -1, utf16$, size% TO ret%         SYS "​MultiByteToWideChar",​ CP_UTF8, 0, utf8$, -1, utf16$, size% TO ret%
         utf16$ = LEFT$(utf16$,​ 2*ret%)         utf16$ = LEFT$(utf16$,​ 2*ret%)
 +</​code>​
 +
 Note the multiplications-by-2 to take account of the fact that UTF-16 strings require two bytes per character. Note the multiplications-by-2 to take account of the fact that UTF-16 strings require two bytes per character.
calling_20dll_20functions_20that_20return_20strings.txt · Last modified: 2018/04/14 17:08 by richardrussell