Search the Community
Showing results for tags 'inidelete'.
-
These functions handle ANSI and unicode inifiles similar to IniRead, IniWrite and IniDelete. _WinAPI_WritePrivateProfileStringW _WinAPI_GetPrivateProfileStringW So you can read from unicode inifiles created from other programs or perhaps read and write to your own inifiles. I was unable to figure out how the API function of WritePrivateProfileStringW can create a unicode file initially so I instead used FileOpen to create a unicode file and write the 1st entry to achieve this. Further use uses WritePrivateProfileStringW is ok to handle the unicode entries. ANSI file creation is done by WritePrivateProfileStringW by default. Deletion of keys and sections may need use of Null which is in AutoIt 3.3.9.0 and later. I put comments with the code so hopefully understandable to you. The functions use a similar parameter syntax to IniRead and IniWrite. _WinAPI_WritePrivateProfileStringW has an additional parameter to handle the flag passed to FileOpen for the initial creation of the inifile. _WinAPI_GetPrivateProfileStringW has an additional parameter in case you want the buffer that holds the return value within the UDF to be larger. The example has some russian text in it so you need to save it in a unicode script for correct testing. Example ; show message if script is not unicode. Unicode text in this script requires it to be UTF encoded. ; if needed in Scite, use menu bar, File -> Encoding -> UTF (any UTF type that suits you) and save the script. If Not FileGetEncoding(@ScriptFullPath) Then MsgBox(0, @ScriptName, 'Script is not unicode') ; write to ini file $return = _WinAPI_WritePrivateProfileStringW("test.ini", 'russian', 'Open', 'Открыть', 0x21) _Notify('Write', $return) ; read from ini file $return = _WinAPI_GetPrivateProfileStringW("test.ini", 'russian', 'Open', 'Default') _Notify('Read', $return) #cs AutoIt 3.3.9.0 or later using Null keyword ; setting $sValue parameter with Null keyword will delete a key using AutoIt 3.3.9.x or later. $return = _WinAPI_WritePrivateProfileStringW("test.ini", 'russian', 'Open', Null) ; setting $sKey parameter with Null keyword will delete a section using AutoIt 3.3.9.x or later. $return = _WinAPI_WritePrivateProfileStringW("test.ini", 'russian', Null, Null) ; setting $sSection parameter with Null keyword will flush using AutoIt 3.3.9.x or later. $return = _WinAPI_WritePrivateProfileStringW("test.ini", Null, Null, Null) #ce ; More info of an API that mimics these functions and explains use of Null at http://code.google.com/p/privateprofilestring/ Func _Notify($title, $return = 0, $error = @error, $extended = @extended) ; notify results from other function calls MsgBox(StringRegExpReplace($error, '-{0,1}([1-9])[0-9]*', '0x30'), $title, _ '@error = ' & $error & @CRLF & _ '@extended = ' & $extended & @CRLF & _ '$return = ' & $return _ ) EndFunc User defined functions ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_WritePrivateProfileStringW ; Description ...: Write to ANSI and unicode encoded ini files ; Syntax.........: _WinAPI_WritePrivateProfileStringW($sFileName, $sSection, $sKey, $sValue[, $iMode = 1]) ; Parameters ....: $sFileName - The filename of the .ini file ; $sSection - The section name in the .ini file ; $sKey - The key name in the in the .ini file ; $sValue - The value to write/change ; $iMode - Refer to FileOpen mode parameter ; Return values .: Success - Not 0 ; Failure - 0 ; @error 1 to 5 - Refer to DllCall ; @error 6 - FileOpen failed to open a handle to create the file ; Author ........: MHz ; Modified.......: ; Remarks .......: Similar to IniWrite but uses unicode API calls. If $sValue is Null then $sKey is deleted. If $sKey is Null, ; then $sSection is deleted. If $sSection, $sKey and $sValue are all Null, then $sFileName is ; flushed. *** Null is a keyword that only exists in AutoIt3 versions 3.3.9.x and later *** ; Related .......: DllCall, FileOpen, IniWrite ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms725501%28v=vs.85%29.aspx ; http://msdn.microsoft.com/en-us/library/windows/desktop/bb773660%28v=vs.85%29.aspx ; Example .......: Yes ; =============================================================================================================================== Func _WinAPI_WritePrivateProfileStringW($sFileName, $sSection, $sKey, $sValue, $iMode = 1) Local $handle_write, $ret ; check if path is relative and make it absolute if it is relative $ret = DllCall('Shlwapi.dll', 'bool', 'PathIsRelativeW', 'wstr', $sFileName); lpszPath If Not @error And $ret[0] Then $sFileName = @WorkingDir & '\' & $sFileName ; create a unicode encoded file if needed and write the entry If Not FileExists($sFileName) And $iMode > 0x20 Then $handle_write = FileOpen($sFileName, $iMode) If $handle_write = -1 Then Return SetError(6, 0, 0) FileWrite($handle_write, '[' & $sSection & ']' & @CRLF & $sKey & '=' & $sValue & @CRLF) FileClose($handle_write) Return 1 EndIf ; write to the ini file. $ret[0] will contain nonzero if successful $ret = DllCall('Kernel32.dll', 'bool', 'WritePrivateProfileStringW', _ 'wstr', $sSection, _ 'wstr', $sKey, _ 'wstr', $sValue, _ 'wstr', $sFileName _ ); lpAppName, lpKeyName, lpString, lpFileName If @error Then Return SetError(@error, @extended, 0) Return SetExtended(0, $ret[0]) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_GetPrivateProfileStringW ; Description ...: Read from ANSI and unicode encoded ini files ; Syntax.........: _WinAPI_GetPrivateProfileStringW($sFileName, $sSection, $sKey[, $sDefault = ''[, $iBufferSize = 65536]]) ; Parameters ....: $sFileName - The filename of the .ini file ; $sSection - The section name in the .ini file ; $sKey - The key name in the in the .ini file ; $sDefault - The default value to return if the requested key is not found ; $iBufferSize - Adjustable buffer size which contains the chars from the API call ; Return values .: Success - Returns the requested key value ; Failure - Returns the default string if requested key not found ; @error 1 to 5 - Refer to DllCall ; Author ........: MHz ; Modified.......: ; Remarks .......: Similar to IniRead but uses unicode API calls ; Related .......: IniRead ; Link ..........: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx ; http://msdn.microsoft.com/en-us/library/windows/desktop/bb773660%28v=vs.85%29.aspx ; Example .......: Yes ; =============================================================================================================================== Func _WinAPI_GetPrivateProfileStringW($sFileName, $sSection, $sKey, $sDefault = '', $iBufferSize = 65536) Local $buffer, $ret ; check if path is relative and make it absolute if it is relative $ret = DllCall('Shlwapi.dll', 'bool', 'PathIsRelativeW', 'wstr', $sFileName); lpszPath If Not @error And $ret[0] Then $sFileName = @WorkingDir & '\' & $sFileName ; create a buffer to hold the returned string $buffer = DllStructCreate('wchar[' & $iBufferSize & ']') ; read from the ini file. $ret[0] will contain number of chars returned $ret = DllCall('Kernel32.dll', 'dword', 'GetPrivateProfileStringW', _ 'wstr', $sSection, _ 'wstr', $sKey, _ 'wstr', $sDefault, _ 'ptr', DllStructGetPtr($buffer), _ 'dword', DllStructGetSize($buffer), _ 'wstr', $sFileName _ ); lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName If @error Then SetError(@error, @extended, $sDefault) Return SetExtended($ret[0], DllStructGetData($buffer, 1)) EndFunc Thanks to AZJIO for some russian text for use in the example. Edit: Updated with summary correction by guiness in post #2