Skip to main content
guest
Join
|
Help
|
Sign In
Liberty BASIC Programmer's Encyc
Home
guest
|
Join
|
Help
|
Sign In
Wiki Home
Recent Changes
Pages and Files
Members
Home
General Tutorials
Advanced Tutorials
GUI Programming
Graphics and Games
Strings and Text
Numbers and Math
Using Files
Windows API
Communications
Programmer's Tools
Articles by Date
FAQs
Rosetta Code
General Articles
Newsletters Contents
Table of Contents
Demos
Submit Articles
TOS and License
WritePrivateProfileStructA
Edit
1
…
0
Tags
No tags
edit
Save
Cancel
Notify
RSS
Backlinks
Source
Print
Export (PDF)
<h1>WritePrivateProfileStructA Demo</h1> <em>[[user:tsh73]]</em><br /> <hr /> <h1>Demonstration Program</h1> Accompanies the article on <a class="wiki_link" href="/IniFiles" target="_blank">Initialization Files</a> <br /> <br /> [[user:tsh73]] advises, "Run it, write it, read it, break it and try to write again."<br /> <br /> <pre class="lb">print " * WritePrivateProfileStructA / GetPrivateProfileStructA demo *"<br/>'based on code by Richard Russel (80%) and by Stefan Pendl (15%)<br/>print<br/><br/>dim info$(1, 1) 'for fileExists<br/>struct config, soundon$ as char[12], fullscreen as long<br/>SoundOn$ = "sound is on"<br/>config.soundon$.struct = SoundOn$<br/>config.fullscreen.struct = 4<br/><br/>print "Initial values: [soundon$ = ";config.soundon$.struct;"] [fullscreen = ";config.fullscreen.struct;"]"<br/><br/>if right$(DefaultDir$,1)<>"\" then<br/> DefaultDir$=DefaultDir$+"\"<br/>end if<br/>inifile$ = DefaultDir$ + "testINI.ini"<br/>print inifile$ <br/>if fileExists("", inifile$) then print "File exists" else print "No such file"<br/>print<br/><br/>[mnu]<br/> print "1. Write INI"<br/> print "2. Read INI"<br/> print "3. Delete INI"<br/> print "4. Mess with INI with Notepad (don't forget to save it)"<br/> print "0. Exit"<br/> input "Enter selection: ";sel<br/> if sel = 0 then end<br/> if sel <1 or sel >4 then [mnu]<br/><br/>select case sel<br/>case 1<br/> size = len(config.struct)<br/><br/> calldll #kernel32, "WritePrivateProfileStructA", _<br/> "settings" as ptr, _ ' Section name<br/> "config" as ptr, _ ' Key name<br/> config as struct, _ ' Structure<br/> size as ulong, _ ' Size of structure<br/> inifile$ as ptr, ret as long<br/><br/> if ret=0 then 'If the function successfully copies the string to the initialization file, the return value is nonzero.<br/> print "Some error happened"<br/> call displayError<br/> else<br/> print "Write OK"<br/> end if<br/><br/>case 2<br/> size = len(config.struct)<br/><br/> calldll #kernel32, "GetPrivateProfileStructA", _<br/> "settings" as ptr, _ ' Section name<br/> "config" as ptr, _ ' Key name<br/> config as struct, _ ' Structure<br/> size as ulong, _ ' Size of structure<br/> inifile$ as ptr, ret as long<br/><br/> if ret=0 then 'If the function succeeds, the return value is nonzero.<br/> print "Some error happened"<br/> call displayError<br/> else<br/> print "Read OK"<br/> ' To retrieve the information from the struct:<br/> '<br/> SoundOn$ = config.soundon$.struct<br/> FullScreen = config.fullscreen.struct<br/> print "SoundOn$: ";SoundOn$<br/> print "FullScreen: ";FullScreen<br/> end if<br/><br/>case 3<br/> call tryToKill inifile$<br/> if fileExists("", inifile$) then print "File still exists" else print "No such file"<br/><br/>case 4<br/> run "notepad.exe "+inifile$ <br/><br/>end select<br/><br/>goto [mnu]<br/><br/><br/>end<br/><br/>sub tryToKill filename$<br/>'try to kill it<br/>'don't care much if failed<br/> on error goto [handler]<br/> kill filename$<br/>[handler]<br/>end sub<br/><br/>sub displayError<br/> ' by Stefan Pendl<br/> calldll #kernel32, "GetLastError", _<br/> ErrorCode as ulong<br/><br/> dwFlags = _FORMAT_MESSAGE_FROM_SYSTEM<br/> nSize = 1024<br/> lpBuffer$ = space$(nSize); chr$(0)<br/> dwMessageID = ErrorCode<br/><br/> calldll #kernel32, "FormatMessageA", _<br/> dwFlags as ulong, _<br/> lpSource as ulong, _<br/> dwMessageID as ulong, _<br/> dwLanguageID as ulong, _<br/> lpBuffer$ as ptr, _<br/> nSize as ulong, _<br/> Arguments as ulong, _<br/> result as ulong<br/><br/> print "Error "; ErrorCode; ": "; left$(lpBuffer$, result)<br/>end sub<br/><br/>function fileExists(path$, filename$)<br/> files path$, filename$, info$()<br/> fileExists = val(info$(0, 0)) 'non zero is true<br/>end function</pre>
Javascript Required
You need to enable Javascript in your browser to edit pages.
help on how to format text
Turn off "Getting Started"
Home
...
Loading...