Add INI file support to VBForms...... ;)

To anyone interested:

Edit your VBP/VB.i file and add the following lines to the bottom:

-----------------------------------------------------
PROCEDURE GetPrivateProfileStringA EXTERNAL "kernel32.dll":U:
DEFINE INPUT PARAMETER lpApplicationName AS CHARACTER.
DEFINE INPUT PARAMETER lpKeyName AS CHARACTER.
DEFINE INPUT PARAMETER lpDefault AS CHARACTER.
DEFINE OUTPUT PARAMETER lpReturnedString AS CHARACTER.
DEFINE INPUT PARAMETER nSize AS LONG.
DEFINE INPUT PARAMETER lpFileName AS CHARACTER.
END PROCEDURE. /* GetPrivateProfileString */

PROCEDURE WritePrivateProfileStringA EXTERNAL "kernel32.dll":U:
DEFINE INPUT PARAMETER lpApplicationName AS CHARACTER.
DEFINE INPUT PARAMETER lpKeyName AS CHARACTER.
DEFINE INPUT PARAMETER lpString AS CHARACTER.
DEFINE INPUT PARAMETER lpFileName AS CHARACTER.
END PROCEDURE. /* WritePrivateProfileString */

FUNCTION getINIString RETURNS CHARACTER
( INPUT INI_File AS CHARACTER, INPUT INI_Section AS CHARACTER, INPUT INI_Variable AS CHARACTER ).
DEF VAR INI_Value AS CHARACTER INITIAL " ".
RUN GetPrivateProfileStringA(INI_Section, INI_Variable, "":U, OUTPUT INI_Value, 128, INI_File).
RETURN INI_Value.
END FUNCTION.

FUNCTION setINIString RETURNS LOGICAL
( INPUT INI_File AS CHARACTER, INPUT INI_Section AS CHARACTER, INPUT INI_Variable AS CHARACTER, INPUT INI_Value AS CHARACTER ).
DEF VAR Check_Value AS CHARACTER INITIAL " ".
RUN WritePrivateProfileStringA(INI_Section, INI_Variable, INI_Value, INI_File).
RUN GetPrivateProfileStringA(INI_Section, INI_Variable, "":U, OUTPUT Check_Value, 128, INI_File).
IF Check_Value = INI_Value THEN DO:
RETURN TRUE.
END.
ELSE DO:
RETURN FALSE.
END.
END FUNCTION.
------------------------------------------------------

Now from your VBForms procdures (you have to be including the VB.i file {vbp/vb.i}) use:

getINIString(Path/file, Section, Variable)
setINIString(Path/file, Section, Variable, Value)

Path/file = the INI file and path you are accessing.
Section = the section in [].
Variable = the item under that section.
Value is what you want it set to.

examples:
This would grab the name of the last Report Builder report edited from the Vantage mfgsys.ini file:
ASSIGN r = getINIString("V:\mfgsys61\mfgsys.ini", "ReportBuilderSaveList", "Report").

This would create the INI file, section and variable if it didn't exist, and then set the value to "Hello World!" and announce if successful:
MESSAGE setINIString("V:\mfgsys61\mfgsys.ini", "My Section", "Test Example", "Hello World!") VIEW-AS ALERT-BOX.

I have it set so that when trying to write to and INI, it will do a read right after and compare the values, it then returns true or false based on the fact that the INI value equals the value you tried to set it. Just in case the windows security has the directory or file rights restricted.

Other uses would be to keep a counter in an INI file of each time a specific function was run from your VBForm by reading the value, adding 1 to it and writing it back.

Keep in mind that the VB.i file could be overwritten with Vantage updates. The other option would be to create a second include file with this code like VB2.i and just include both in your .p files.

Also these are only written for strings/character values, I didn't think it was necessary to write versions for windows API Get and Write Integer functions and you can just convert the string to a number in your .p procedure.

These are two of the more useful functions I've added to my VBForms and thought I would share them with the group......

Mike