home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Corel / Ventura8 / Ventura / Scripts / GetPrivateProfileStringSection.csc < prev    next >
Encoding:
Text File  |  1998-07-08  |  6.0 KB  |  109 lines

  1. REM TEACHING SCRIPT - GetPrivateProfileString() API example [CorelSCRIPT 8]
  2. REM GetPrivateProfileStringSection.csc  February 5, 1998
  3. REM ⌐ 1998 Corel Corporation. All rights reserved.
  4.  
  5. REM **********************************************************************
  6. REM  GENERAL INFORMATION 
  7. REM  This script demonstrates how to declare and call a function in a 
  8. REM  Dynamic Link Library (DLL). 
  9. REM 
  10. REM  To use an API, you must know the API name, and how it is declared.
  11. REM  You can find this information by using an API text viewer such as that 
  12. REM  provided with Visual Basic (Apilod32.exe)
  13. REM ***********************************************************************
  14.  
  15. REM ***********************************************************************
  16. REM  This example uses the function GetPrivateProfileString() from the 
  17. REM  windows library: Kernel32.dll. The GetPrivateProfileString() function 
  18. REM  retrieves a string from the specified section of an initialization 
  19. REM  file. As SCRIPT does not have a character data type, the string is 
  20. REM  stored in an integer array, which is then traversed byte by byte to 
  21. REM  obtain the information.
  22. REM
  23. REM    The example provided retrieves all key names from the [config] section
  24. REM  of the Corelapp.ini file.
  25. REM ***********************************************************************
  26.  
  27. ' Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
  28. ' The above line shows the function as declared from the Visual Basic API text viewer. 
  29. ' This declaration has to be modified slightly in order to work with Corel SCRIPT.
  30. ' The SCRIPT declaration places the 'ALIAS' at the end of the declaration. 
  31.  
  32. ' BYVAL AppName AS STRING
  33.     ' The name of the section from which to retrieve information
  34.     ' To use this parameter in SCRIPT, declare it as a STRING and assign it the name of the section 
  35.     ' from which to retrieve information
  36.  
  37. ' BYVAL KeyName AS LONG
  38.     ' The name of the key whose associated string is to be retrieved
  39.     ' If this parameter is set to NULL, all key names in the specified section are copied to the buffer 
  40.     ' To obtain all key names using SCRIPT, declare this parameter as a LONG and assign it a value of 0
  41.     ' To obtain a specified string using SCRIPT, declare this parameter as a STRING and assign it the 
  42.     ' name of the key to obtain information from
  43.  
  44. ' BYVAL DefString AS STRING
  45.     ' The name of the default string; if lpKeyName cannot be found, this name is used 
  46.  
  47. ' BYREF lpReturnedString AS INTEGER
  48.     ' Pointer to the destination buffer that receives the retrieved string
  49.  
  50. ' BYVAL nSize AS LONG
  51.     ' The size of the buffer which recieves the retrieved string
  52.  
  53. ' BYVAL FileName AS STRING
  54.     ' The name of the initialization file. If the full path is not provided, Windows searches for the file 
  55.     ' in the Windows directory.
  56.  
  57. #addfol "..\..\Scripts"
  58. #include "VPConst.csi"
  59.  
  60. DECLARE FUNCTION GetPrivateProfileString Lib "kernel32"(\\
  61.     BYVAL AppName AS STRING,         \\
  62.     BYVAL KeyName AS LONG,             \\
  63.     BYVAL DefString AS STRING,        \\
  64.     BYREF lpReturnedString As INTEGER,    \\
  65.     BYVAL nSize AS LONG,            \\
  66.     BYVAL FileName AS STRING)        \\
  67. AS LONG ALIAS "GetPrivateProfileStringA" 
  68.  
  69. ' This section defines values used by the GetPrivateProfileString() function
  70. MAX_CHARS& = 256            ' Defines the size of the string to be retrieved
  71. DIM KeyName AS LONG            ' Name of the key whose associated string is to be retrieved
  72. DIM DefString AS STRING        ' Name of the default string
  73. DIM RetString AS STRING        ' The string that is created from the retrieved characters 
  74. DIM StrSize AS LONG            ' The size of the buffer which recieves the retrieved string
  75. DIM lRetValue AS LONG        ' Return value of the function: If the function succeeds, specifies the number of characters copied to the buffer
  76.                         '                        : If the function fails, returns 0
  77. DIM FileName AS STRING        ' Name of the initialization file
  78. DIM IntArray%(MAX_CHARS&/2)    ' Integer array used as a buffer for holding the retrieved string
  79. StrSize& = MAX_CHARS&        ' Initialize size of string to be retrieved
  80.  
  81. ' This section defines values used by the REGISTRYQUERY() function to obtain Ventura's root directory
  82. #DEFINE HKEY_LOCAL_MACHINE 2                ' Specifies the main registry value key to query 
  83. SubKey$ = VENTURA_REGQUERY_CONST    ' Specifies the sub registry value key to query 
  84. Value$ = "ConfigDir"                    ' Specifies the registry value key to query
  85.  
  86. ' This section initializes variables to retrieve all key names from the [config] section of the Corelapp.ini file
  87. AppName$ = "config"            ' Initialize section name
  88. KeyName& = 0                ' Initialize key name as NULL; this will retrieve all section names
  89. VenturaRoot$ = REGISTRYQUERY(HKEY_LOCAL_MACHINE,SubKey$,Value$)     ' Root directory where Ventura is installed
  90. FileName$ = VenturaRoot$ & "\corelapp.ini"                ' Initialize initialization file; need to specify complete path 
  91.                         
  92. ' This section retrieves all key names from the [config] section of the Corelapp.ini file
  93. lRetValue& =  GetPrivateProfileString(AppName$, KeyName&, DefString$, IntArray%(1), StrSize&, FileName$)
  94. ' The integer array contains two characters for every element; we need to get each character separately
  95. Limit% = lRetValue&/2 + lRetValue& MOD 2    'IF lRetValue is odd, need to add one to Limit otherwise the last character is missed
  96. FOR i% = 1 TO Limit%
  97.     ' Get the character from the low byte
  98.     Char% = IntArray%(i%) AND DEC("00FF")    ' Isolate the low byte of the integer
  99.     IF Char% = 0 THEN Char% = 13            ' Separate device names with a line return
  100.     RetString$ = RetString$ & CHR(Char%)    ' Convert integer to character
  101.     ' Get the character from the high byte
  102.     Char% = IntArray%(i%) >> 8            ' Shift right 8 bits to isolate the high byte of the integer
  103.     IF Char% = 0 THEN Char% = 13            ' Separate device names with a line return
  104.     RetString$ = RetString$ & CHR(Char%)    ' Convert integer to character
  105. NEXT i%
  106.  
  107. MESSAGE RetString$            ' Output resulting string to screen
  108.  
  109.