API Functions

I have a program with “FILE” on the menu bar, and under “FILE” I have the following:
NEW
OPEN
SAVE
SAVE AS
------
“FILENAME”
------
EXIT

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???
mapperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deathpawCommented:
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..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mapperAuthor Commented:
Do I use the API Calls for Read and Write profile strings on a Module of on the main form??
mapperAuthor Commented:
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??
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

deathpawCommented:
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.
mapperAuthor Commented:
Thanks, deathpaw!

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

mapper
deathpawCommented:
No problem, Any time!
mapperAuthor Commented:
deathpaw,

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! :-)

mapper
deathpawCommented:
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.

mapperAuthor Commented:
deathpaw,

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

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

Thanks,

mapper
deathpawCommented:
Don't worry about. It takes me a while to catch things like that too. :)
mapperAuthor Commented:
deathpaw,

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!

Thanks,

mapper

PS:  if eye can't you'll be hearing from me!! :)
mapperAuthor Commented:
deathpaw,

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!!

thanks,

mapper
deathpawCommented:
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 wolfman@advnet.net.

Take Care.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.