API Functions

Posted on 1997-04-30
Last Modified: 2006-11-17
I have a program with “FILE” on the menu bar, and under “FILE” I have the following:

The “FILENAME” is suppose to show the LAST USED FILE NAME to the menu bar.
I know that I need to use the Windows API functions for READING and WRITING an INI file to do this but I am not sure how to use the WritePrivateProfileString and ReadPrivateProfileString to Read and Write the filename to XXXXXX.ini file to accomplish this task.

At LOAD the code should read the XXXXX.ini file for the LAST USED FILE NAME and display that file name on the MENU BAR (Shown above) as “FILENAME”

At EXIT while closing out of the program, it will first write the current file name to the XXXXX.ini file.

Can someone provide code to get me going on this???
Question by:mapper
  • 7
  • 6

Accepted Solution

deathpaw earned 35 total points
ID: 1425831
Okay, this isn't too awefull difficult to do once you've done it before. But if this is your first time then it will seem a little awkward..
 On your form load you will want to do this:
returnval = GetPrivateProfileString("Previous Files","lastfile","",bufferstr,50,filename.ini)
if returnval = true then menuitem.caption = bufferstr

that will set the menu item caption to the last file item. Bufferstr is a string value where the entry will be placed, and 50 is the size of the string. Make sure you init the string first with spaces.

On the exit procedure, do this:

returnval = WritePrivateProfileString("Previous Files","lastfile",lastfile,filename.ini)

And that should be it. I hope this is enough to get you started.
Once you do this once or twice, it's a peice of cake..

Author Comment

ID: 1425832
Do I use the API Calls for Read and Write profile strings on a Module of on the main form??

Author Comment

ID: 1425833
Do I use the API Calls for Read and Write profile strings on a Module of on the main form??  I also, need to know do I need to creat an INI file to write this stuff to??

Expert Comment

ID: 1425834
First of all, make sure you have windows api calls referenced in your project, you can do that by going to tools and clicking on references under custom controls (Assuming your in vb 4.0). Once that is done, you can use the API functions anywhere in your program, on a form or in a module..
As far as creating an INI file, you can just use notepad to create the file and let the APIs handle everything else.

Author Comment

ID: 1425835
Thanks, deathpaw!

I would like to upgrade my previous grade to an "A+"  that was the info that made it all clear!


Expert Comment

ID: 1425836
No problem, Any time!
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.


Author Comment

ID: 1425837

I have added the API declarations to my module, and I have changed the code to fit my program:

I used this in the LOAD:

Private Sub Form_Load()

lpFileName = "Myfile.ini"
Dim LastFile As Long

returnval = GetPrivateProfileString("Previous Files", "lastfile", "", bufferstr, 50, Myfile.ini)
If returnval = True Then LastFile.Caption = bufferstr

and I keep getting LASTFILE is and invalid qualifier!

LastFile is the name is used under the MENU EDITOR to create the place for the file name to be displayed.

I also am getting the Invalid qualifier for the "Myfile.ini" too!
I guess I am a little slow when it comes to CAKE! :-)


Expert Comment

ID: 1425838
If LastFile is the name of the menu item, then you can't dim it to a long. It's already defined. So after the call to the function, you would do this:
LastFile.caption = bufferstr
Also, before the call to the GetPrivateProfileFunction, make sure you add this:
bufferstr = string$(50, " ")
Now, in the GetPrivateProfileString function, make sure the name of the ini file is in quotes.. ex.: "Myfile.ini"
Also, if your calling the Get function first, make sure the ini file is there..
And, if there is no previous filename there, this function is set up to default to nothing in the string, so if you want a default value, replace the "" after "lastfile" with some string valuee in quotes...

Try this, and let me know what happens.


Author Comment

ID: 1425839

Duh, I feel soooooooo stupid, I'm an eeeedddioooot!!

thanks, I will crank on that and let you know.



Expert Comment

ID: 1425840
Don't worry about. It takes me a while to catch things like that too. :)

Author Comment

ID: 1425841

I figured out another problem, in the MENU EDITOR the spot for the LASTFILE is declared as nmuLastFile (DUH) and that is why it was telling me it wasn't declared, I can't see the forrest for the trees!!  The first run through I got garbage as the file name but hey at least it is a start!!  Thanks again for all of the pointers, I should be able to doink around with it and get it fine tuned!



PS:  if eye can't you'll be hearing from me!! :)

Author Comment

ID: 1425842

I am still getting crap in the mnuLastFile.caption.  I didn't find the api calls referenced in the project under tools and clicking references.  So I put the private strings on a module as follows:

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

Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal _
lpFileName As String) As Long

In the Load event I have the following:

Dim bufferstr As String
Dim returnval As String
bufferstr = String$(50, " ")

returnval = GetPrivateProfileString("Previous Files", "lastfile", "", bufferstr, 50, "weather.ini")

If returnval = True Then
    mnuLastFile.Caption = bufferstr
End If

And, in the EXIT event I have the following:

Dim returnval As String
returnval = WritePrivateProfileString("Previous Files", "lastfile", mnuLastFile, "weather.ini")

The problem I am having is that I run the program and creat a calendar, I save the file and then Exit the program.

When I exit the program and then run it again, I get ".mÓ" in the mnuLastFile.caption.

I don't know or can't figure out what I am doing wrong!!

Please help!!



Expert Comment

ID: 1425843
First try putting a msgbox in displaying what it's loading out of the INI file.. Like this:
msgbox bufferstr

Place this before your if returnval = true then statement..
Also, dim the returnval as an integer. I also found out a little problem with the returnval of that function. The get function returns the number of characters it reads from the INI. So instead of if returnval = true, put if returnval > 0 then.
That should clear up that little problem..

Make sure the write function is working by looking at the INI file after the program exits. It's quite possible that the read is working, and the write is not.

Also, on the write function, instead of placing mnuLastFile in there, you should be placing mnuLastFile.caption, since mnuLastFile is a menuitem, not a string. Try that and let me know whats going on.

This is getting to be a pretty lengthy discussion, if you want to take it to e-mail, just mail me at

Take Care.


Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now