Chapter #1 - The Module Code

Okay, we´ll be needing these for our project:

Form1 Text1: A text box for data input
Command1: A button to puke the data on a profiled file
Command2: A button for exit
Module1.BAS Vars: Public dot_ini As String, Public cur_usr As String
Private Declare Function GetWindowsDirectory: The API to retrieve Windows´ default directory
Private Declare Function GetUserNameA: The API to retrieve the current logged-in user
Public Function GetWindows() As String
Public Function GetUserName() as String
Public Sub Load_Up()

This is the code to the Bas Module, commented line by line:

Public dot_ini As String
'This variable will hold the path to our .ini file
'within the Load_Up() sub we´ll set it to a correct path that can be currently adressed
'wether we have profile-support or not.

Public cur_usr As String
'This variable will hold the name of the current user, if there´s no profiled-user currently logged in, it will fall back to "unnamed" value.

Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long
'This API returns non-zero if it could find a UserName.
'If it succeded (returned non-zero) the actual UserName is stored on lpBuffer

Private Declare Function GetWindowsDirectory Lib "kernel32.dll" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
'This API returns non-zero if it could find the current Windows´ defualt directory
'Again, if it succeded, the result is stored on lpBuffer.



Public Function GetUserName() As String

Dim UserName As String * 255
'This creates a buffer for the username with a length of 255 characters (what a big name that´d be!)
    GetUserNameA UserName, 255
   'Calls the api, passing a buffer (UserName), and the length of that buffer (255)

    GetUserName = Left$(UserName, InStr(UserName, Chr$(0)) - 1)
   'Now the function (GetUserName) returns only the left-handed part of the string returned on UserName with the characters of the current user´s name. So, basically what it does, is look up through the whole 255 characters, until it finds a Chr$(0), a space that´d be, and pastes the result on GetUserName

End Function


Public Function GetWindows() As String
Dim pathWin As String
Dim done As Long

   pathWin = String$(144, 0)
   done = GetWindowsDirectory(pathWin, Len(pathWin))
   If done = 0 Then
     GetWindows = "not found"
   Else
     GetWindows = Left(pathWin, done)
   End If
'This is basically the same as above, the procedure is the same, but this one gives back the current windows´ default directory (i.e.:"c:\windows")
End Function


Public Sub Load_Up()

cur_usr = GetUserName
'Let´s get the current userName

winpath = GetWindows
'Let´s get the current windows´ default directory


If len(cur_usr) = 0 Then
'What? No multi-user support? okay...:
 cur_usr = "Unnamed"
 dot_ini = winpath & "\myApp.ini"
 'Our app´s data will be saved and retrieved from the default dir, and our guest will be known as "Unnamed". (i.e.:"c:\windows\myApp.ini")

Else
'Yesss! We got a profiled-user logged in! so...:
 dot_ini = winpath & "\profiles\" & cur_usr & "\application data\myApp.ini"
'Our app´s data will be saved and retrieved from his profile area:
'"c:\windows\profiles\johnny\application data\myApp.ini"
End If

End Sub

'The neatest of it all, is that if you run Load_Up() every time you start your app, you will retrieve the same data for each profile. Wich means that there is a coherence between each profiled-user and their myApp.ini file. Each one has a different one, and the guest user also has his own copy at the default windows´ directory... ;)

Upto here, you might want to start experiments on that Load_Up() sub, and that´s what I promised since the beginning... So let´s get to see this beauty in action!!!
On to Chapter #2



Introduction - Chapter #1 - Chapter #2 - Chapter #3