REM TEACHING SCRIPT - GetPrivateProfileString() API example [CorelSCRIPT 8]
REM GetPrivateProfileString.csc February 5, 1998
REM ⌐ 1998 Corel Corporation. All rights reserved.
REM **********************************************************************
REM GENERAL INFORMATION
REM This script demonstrates how to declare and call a function in a
REM Dynamic Link Library (DLL).
REM
REM To use an API, you must know the API name, and how it is declared.
REM You can find this information by using an API text viewer such as that
REM provided with Visual Basic (Apilod32.exe)
REM ***********************************************************************
REM ***********************************************************************
REM This example uses the function GetPrivateProfileString() from the
REM windows library: Kernel32.dll. The GetPrivateProfileString() function
REM retrieves a string from the specified section of an initialization
REM file. As SCRIPT does not have a character data type, the string is
REM stored in an integer array, which is then traversed byte by byte to
REM obtain the information.
REM
REM The example provided retrieves the name of the most recently opened
REM Ventura file obtained from the [Recent File List] section of the
REM Ventura.ini file.
REM ***********************************************************************
' 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
' The above line shows the function as declared from the Visual Basic API text viewer.
' This declaration has to be modified slightly in order to work with Corel SCRIPT.
' The SCRIPT declaration places the 'ALIAS' at the end of the declaration.
' BYVAL AppName AS STRING
' The name of the section from which to retrieve information
' To use this parameter in SCRIPT, declare it as a STRING and assign it the name of the section
' from which to retrieve information
' BYVAL KeyName AS STRING
' The name of the key whose associated string is to be retrieved
' If this parameter is set to NULL, all key names in the specified section are copied to the buffer
' To obtain all key names using SCRIPT, declare this parameter as a LONG and assign it a value of 0
' To obtain a specified string using SCRIPT, declare this parameter as a STRING and assign it the
' name of the key to obtain information from
' BYVAL DefString AS STRING
' The name of the default string; if lpKeyName cannot be found, this name is used
' BYREF lpReturnedString AS INTEGER
' Pointer to the destination buffer that receives the retrieved string
' BYVAL nSize AS LONG
' The size of the buffer which recieves the retrieved string
' BYVAL FileName AS STRING
' The name of the initialization file. If the full path is not provided, Windows searches for the file
' in the Windows directory.
#addfol "..\..\Scripts"
#include "VPConst.csi"
DECLARE FUNCTION GetPrivateProfileString Lib "kernel32"(\\
BYVAL AppName AS STRING, \\
BYVAL KeyName AS STRING, \\
BYVAL DefString AS STRING, \\
BYREF lpReturnedString As INTEGER, \\
BYVAL nSize AS LONG, \\
BYVAL FileName AS STRING) \\
AS LONG ALIAS "GetPrivateProfileStringA"
' This section defines values used by the GetPrivateProfileString() function
MAX_CHARS& = 256 ' Defines the size of the string to be retrieved
DIM KeyName AS STRING ' Name of the key whose associated string is to be retrieved
DIM DefString AS STRING ' Name of the default string
DIM RetString AS STRING ' The string that is created from the retrieved characters
DIM StrSize AS LONG ' The size of the buffer which recieves the retrieved string
DIM lRetValue AS LONG ' Return value of the function: If the function succeeds, specifies the number of characters copied to the buffer
' : If the function fails, returns 0
DIM FileName AS STRING ' Name of the initialization file
DIM IntArray%(MAX_CHARS&/2) ' Integer array used as a buffer for holding the retrieved string
StrSize& = MAX_CHARS& ' Initialize size of string to be retrieved
' This section defines values used by the REGISTRYQUERY() function to obtain Ventura's root directory
#DEFINE HKEY_LOCAL_MACHINE 2 ' Specifies the main registry value key to query
SubKey$ = VENTURA_REGQUERY_CONST ' Specifies the sub registry value key to query
Value$ = "Destination" ' Specifies the registry value key to query
' This section initializes variables to retrieve the name of the most recently opened Ventura file
AppName$ = "Recent File List" ' Initialize section name
KeyName$ = "File1" ' Initialize key name
VenturaRoot$ = REGISTRYQUERY(HKEY_LOCAL_MACHINE,SubKey$,Value$) ' Root directory where Ventura is installed
FileName$ = VenturaRoot$ & "\Workspace\Ventura8\_default\ventura.ini" ' Initialize initialization file; need to specify complete path
' This section retrieves the name of the most recently opened Ventura file from the Ventura.ini file.