Link to home
Start Free TrialLog in
Avatar of mapper
mapper

asked on

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???
ASKER CERTIFIED SOLUTION
Avatar of deathpaw
deathpaw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mapper
mapper

ASKER

Do I use the API Calls for Read and Write profile strings on a Module of on the main form??
Avatar of mapper

ASKER

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??
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.
Avatar of mapper

ASKER

Thanks, deathpaw!

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

mapper
No problem, Any time!
Avatar of mapper

ASKER

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

Avatar of mapper

ASKER

deathpaw,

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

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

Thanks,

mapper
Don't worry about. It takes me a while to catch things like that too. :)
Avatar of mapper

ASKER

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!! :)
Avatar of mapper

ASKER

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