GetPrivateProfileString

G

Guest

I am trying to get the GetPrivateProfileString to work, but am having
difficulties. here is my setup

Module1.bas General Declaration
Declare Function GetPrivateProfileString% Lib "kernel32.DLL" (ByVal
lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$, ByVal
nSize%, ByVal lpFileName$)


Code in Form (always errors out and goes to FileError.)

on error go to FileError:

Valid% = GetPrivateProfileString("AutoPrint", "database", lpDefault$,
lpReturnString$, Size%, "c:\winnt\autoprint.ini")

FileError:
MsgBox "Can't find startup ini file", 16, "Error lpReturnString"
Resume Ne

autoprint.ini contents
[AutoPrint]
database = biometric

This always goes to FileError, Is there a Reference that I need to add to my
project or am I not call it correct?
 
G

Graham Mandeno

There are several problems with your code:
1. You have not declared the API function correctly.
2. You have not allocated space in your result string to accept the returned
value.
3. You are implicitly declaring your variables (which is dangerous) and one
of these is the lenght of the buffer you are providing. In other words:
"please put the result into this space of length zero"
4. You have no Exit Function before FileError, so your code will always get
to FileError whether there is an error or not.

I suggest you do the following:

1. ALWAYS declare ALL your variables, and put "Option Explicit" at the top
of EVERY module to ensure that you must do so.

2. Avoid using $, %, etc as implicit data type specifiers - they are only
useful for implicit declarations anyway.

3. Write a "jacket function" to enclose your call to the API function. This
means you need to do the stuff like preallocation of buffer space and
trimming the result in only one place in your code.

Here is some code you can copy and paste into a module:
====================
Option Explicit

Private Declare Function GetPrivateProfileString _
Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) _
As Long

Public Function GetIniString _
(sSection As String, _
sKey As String, _
sIniFile As String, _
Optional sDefault As String = "") _
As String
Const cBufSize As Long = 1024
Dim sBuffer As String, lRetLen As Long
sBuffer = String(cBufSize, vbNull)
lRetLen = GetPrivateProfileString(sSection, sKey, _
sDefault, sBuffer, cBufSize, sIniFile)
GetIniString = Left(sBuffer, lRetLen)
End Function
===========================

Then, from anywhere in your code, you can call your function thus:

sDBName = GetIniString( "AutoPrint", "database", "c:\winnt\autoprint.ini" )
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand



molonede said:
I am trying to get the GetPrivateProfileString to work, but am having
difficulties. here is my setup

Module1.bas General Declaration
Declare Function GetPrivateProfileString% Lib "kernel32.DLL" (ByVal
lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$,
ByVal
nSize%, ByVal lpFileName$)


Code in Form (always errors out and goes to FileError.)

on error go to FileError:

Valid% = GetPrivateProfileString("AutoPrint", "database", lpDefault$,
lpReturnString$, Size%, "c:\winnt\autoprint.ini")

FileError:
MsgBox "Can't find startup ini file", 16, "Error lpReturnString"
Resume Ne

autoprint.ini contents
[AutoPrint]
database = biometric

This always goes to FileError, Is there a Reference that I need to add to
my
project or am I not call it correct?
 
G

Guest

Thanks, got it working.

Graham Mandeno said:
There are several problems with your code:
1. You have not declared the API function correctly.
2. You have not allocated space in your result string to accept the returned
value.
3. You are implicitly declaring your variables (which is dangerous) and one
of these is the lenght of the buffer you are providing. In other words:
"please put the result into this space of length zero"
4. You have no Exit Function before FileError, so your code will always get
to FileError whether there is an error or not.

I suggest you do the following:

1. ALWAYS declare ALL your variables, and put "Option Explicit" at the top
of EVERY module to ensure that you must do so.

2. Avoid using $, %, etc as implicit data type specifiers - they are only
useful for implicit declarations anyway.

3. Write a "jacket function" to enclose your call to the API function. This
means you need to do the stuff like preallocation of buffer space and
trimming the result in only one place in your code.

Here is some code you can copy and paste into a module:
====================
Option Explicit

Private Declare Function GetPrivateProfileString _
Lib "kernel32" _
Alias "GetPrivateProfileStringA" _
(ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) _
As Long

Public Function GetIniString _
(sSection As String, _
sKey As String, _
sIniFile As String, _
Optional sDefault As String = "") _
As String
Const cBufSize As Long = 1024
Dim sBuffer As String, lRetLen As Long
sBuffer = String(cBufSize, vbNull)
lRetLen = GetPrivateProfileString(sSection, sKey, _
sDefault, sBuffer, cBufSize, sIniFile)
GetIniString = Left(sBuffer, lRetLen)
End Function
===========================

Then, from anywhere in your code, you can call your function thus:

sDBName = GetIniString( "AutoPrint", "database", "c:\winnt\autoprint.ini" )
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand



molonede said:
I am trying to get the GetPrivateProfileString to work, but am having
difficulties. here is my setup

Module1.bas General Declaration
Declare Function GetPrivateProfileString% Lib "kernel32.DLL" (ByVal
lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$,
ByVal
nSize%, ByVal lpFileName$)


Code in Form (always errors out and goes to FileError.)

on error go to FileError:

Valid% = GetPrivateProfileString("AutoPrint", "database", lpDefault$,
lpReturnString$, Size%, "c:\winnt\autoprint.ini")

FileError:
MsgBox "Can't find startup ini file", 16, "Error lpReturnString"
Resume Ne

autoprint.ini contents
[AutoPrint]
database = biometric

This always goes to FileError, Is there a Reference that I need to add to
my
project or am I not call it correct?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

GetProfileItem. 7
GetPrivateProfileString 1
use GetPrivateProfileString 2
How is it work? 4
Reading an INI file 18
GetPrivateProfileString problem! 1
GetPrivateProfileString 2
ini file 2 2

Top