Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

API Functions

Posted on 1997-04-30
13
Medium Priority
?
1,058 Views
Last Modified: 2006-11-17
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???
0
Comment
Question by:mapper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 

Accepted Solution

by:
deathpaw earned 70 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..
0
 

Author Comment

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

Author Comment

by:mapper
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??
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:deathpaw
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.
0
 

Author Comment

by:mapper
ID: 1425835
Thanks, deathpaw!

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

mapper
0
 

Expert Comment

by:deathpaw
ID: 1425836
No problem, Any time!
0
 

Author Comment

by:mapper
ID: 1425837
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
0
 

Expert Comment

by:deathpaw
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.

0
 

Author Comment

by:mapper
ID: 1425839
deathpaw,

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

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

Thanks,

mapper
0
 

Expert Comment

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

Author Comment

by:mapper
ID: 1425841
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!! :)
0
 

Author Comment

by:mapper
ID: 1425842
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
0
 

Expert Comment

by:deathpaw
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 wolfman@advnet.net.

Take Care.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

704 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