PC Review


Reply
Thread Tools Rate Thread

GetPrivateProfileString

 
 
=?Utf-8?B?bW9sb25lZGU=?=
Guest
Posts: n/a
 
      24th Nov 2004
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?


 
Reply With Quote
 
 
 
 
Graham Mandeno
Guest
Posts: n/a
 
      24th Nov 2004
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" <(E-Mail Removed)> wrote in message
news:BEB8BC7E-75EA-4AA6-A274-(E-Mail Removed)...
>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?
>
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?bW9sb25lZGU=?=
Guest
Posts: n/a
 
      29th Nov 2004
Thanks, got it working.

"Graham Mandeno" wrote:

> 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" <(E-Mail Removed)> wrote in message
> news:BEB8BC7E-75EA-4AA6-A274-(E-Mail Removed)...
> >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?
> >
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GetPrivateProfileString =?Utf-8?B?a3ZyZGV2ZWxvcGVyMQ==?= Microsoft Dot NET 3 25th Mar 2005 05:53 PM
Equivalent of GetPrivateProfileString equivalent in C#? Germic Microsoft C# .NET 1 24th Jan 2005 10:22 AM
GetPrivateProfileString =?Utf-8?B?TmVpbCBH?= Microsoft Dot NET Framework 2 1st Jun 2004 10:27 PM
GetPrivateProfileString any managed method? Robains Microsoft Dot NET Framework 2 29th Dec 2003 02:11 PM
GetPrivateProfileString Susan Landgraf Microsoft Dot NET 1 27th Nov 2003 04:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:18 PM.