amdimauro 05-01-2001, 12:09 PM Hello All,
Can't seem to find this information anywhere on the web, on microsoft's site, so I'm turning to you.
The VBP file is pretty much an INI file except it is missing the all-important [SECTION] part of it. I am
trying to use the "GetPrivateProfileString" API call to read the "keys" from the VBP but I have been unable to come up with the correct values to pass into the call to retrieve the "keys". If I have to reteive all the keys at once that is fine...
I'm trying to get things like the Conditional Compilation String, the Version Number, et al.
Here's what I have tried (all failed)
Declaration:
Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Calls:
szBuffer = Space(255)
nSize = Len(szBuffer)
1) bResult = GetPrivateProfileString("", 0&, "Default", szBuffer, nSize , "Project1.vbp")
2) bResult = GetPrivateProfileString(0&, 0&, "Default", szBuffer, nSize , "Project1.vbp")
3) bResult = GetPrivateProfileString(Null, 0&, "Default", szBuffer, nSize , "Project1.vbp")
4) bResult = GetPrivateProfileString(vbNullString, 0&, "Default", szBuffer, nSize , "Project1.vbp")
1 & 2 return failure codes, 3 generates "Invalid Use of Null" error, 4 returns a success code but an empty buffer.
Any help would be greatly appreciated...
Thanks in Advance.
Anthony
anhmytran 05-01-2001, 03:54 PM Option Explicit
' The ini file's content is:
' There are several lines in the previous sections
' [sectionheader]
' username=AnhMyTran (or username="a name and some spaces")
' There are several lines after that
Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As Any, _
ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Sub Form_Click()
Dim lResult As Long, sBuffer As String, lSize As Long
sBuffer = Space(255)
lSize = Len(sBuffer)
lResult = GetPrivateProfileString("sectionheader", "username", _
"unknown string", sBuffer, lSize, "D:\myProject.ini")
sBuffer = Left(sBuffer, lResult)
MsgBox sBuffer, vbOKOnly, "AnhMy Tran"
' The file should have extention 'ini'
' if the header or username is not correct,
' the return string is "unknown string"
' otherwise, it is "a name and some spaces"
End Sub
AnhMy_Tran
BillSoo 05-01-2001, 06:19 PM As far as I know, specifying vbNullString for the key causes GetPrivateProfileString to return a list of keys for the given section. Likewise, specifying vbNullString for the section causes GetPrivateProfileString to return a list of sections in the file.
I don't know how you could use the INI functions to read a VBP file, other than making your own file with a section header and then appending the vbp file to yours.
You may have to use the normal file functions instead....
"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder
amdimauro 05-02-2001, 07:01 AM Thanks for the info Bill...
I need to access all this stuff, because I am writting an Add-in.
I was afaid I would have to write my own parsing routines using the normal file functions but I was hoping I could leverage the "GetPrivateProfileString" since the VBP is pretty much an INI and that there would be some magic combination of parameter values to return keys that weren't contained in a [SECTION]...we know how Microsoft loves to do those type of things.
anhmytran 05-02-2001, 09:30 AM The following is a procedure that copies the content of your file and writes it
as a Section into an ini file, then reads the information in this section into
an output string variable. After that the temporary ini file is deleted from the HD.
Private Sub Form_Click()
On Error GoTo FileErr
' Make ini file
Dim sFName As String, sTemp As String, sSection As String
Dim lLen As Long
sSection = "Single Section"
sFName = "D:\atran\chop.vbp"
Open sFName For Input As #1 ' Open source file.
lLen = FileLen(sFName)
If lLen = 0 Then
Exit Sub
Close #1
End If
sTemp = Input(lLen, #1) ' Get the entire data.
' Add a Section Header to the data string
sTemp = "[" & sSection & "]" & vbNewLine & sTemp
MsgBox sTemp
lLen = Len(sTemp)
Close #1 ' Close file.
sFName = "D:\atran\chop.ini"
Open sFName For Binary As #1 ' Open new file.
' Write the entire section into the new ini file
Put #1, , sTemp
Close #1
' Read ini file
Dim lResult As Long, sBuffer As String, lSize As Long
sBuffer = Space(255)
lSize = Len(sBuffer)
lResult = GetPrivateProfileString(sSection, "VersionCompanyName", _
"unknown string", sBuffer, lSize, sFName)
sBuffer = Left(sBuffer, lResult)
MsgBox sBuffer, vbOKOnly, "AnhMy Tran"
' Delete the ini file
Kill sFName
Exit Sub
FileErr:
MsgBox Err.Description, vbCritical, "AnhMy Tran"
End Sub
AnhMy_Tran
SATISHNAIR 05-08-2001, 01:38 PM Assuming your VBP file is a line delimited text file with a Section and a Key similar to INI files, here is a piece of code that works for us on Windows NT and 2000.
------------- begin code -------------------
i = 255
strIniVal = String(i, vbNullChar)
j = GetPrivateProfileString(strSection, strKey, "", strIniVal, i, strFile)
strIniVal = Replace(strIniVal, vbNullChar, "", 1, , vbTextCompare)
----------- end code ------------------------
strIniVal shoud have your key value
amdimauro 05-08-2001, 02:09 PM Thanks SATISHNAIR,
But you've touched on the most critical dilemma...the VBP file ia a normal VBP file without a Section Header...therefore the GetPrivateProfileString does not work. If you open a VBP file in Notepad you will notice that is exactly the same as a INI file with the KEY=VALUE lines but it does NOT contain a Section Header...
I am resolving myself to having either 1) use straight file manipulation functions or 2) initially modify the VBP to contain a SECTION and then use the GetPrivateProfileStrings
Anthony
amdimauro 05-08-2001, 02:18 PM Thanks anhmytran,
Your suggestion is actually a pretty interesting solution to this problem. I had thought about using normal file manipulation methods but it was annoying me that I had all these INI file methods already at my disposal and I couldn't use them because of the missing SECTION header.
I hadn't thought of the angle of just creating a temporary VBP INI Type file...I'm going to give this a shot...should work...and will be alot easier with the ability to use those INI functions.
Thanks again
Anthony
|