Finished unit tests and fixes for winbase.h profile functions

pull/83/head
David Hall 2019-07-26 09:43:19 -06:00
parent 3f68b4f114
commit e0aa2e8781
3 changed files with 262 additions and 101 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.InteropServices;
namespace Vanara.PInvoke
{
@ -9,16 +10,17 @@ namespace Vanara.PInvoke
/// <summary>Retrieves an integer associated with a key in the specified section of an initialization file.</summary>
/// <param name="lpAppName">The name of the section in the initialization file.</param>
/// <param name="lpKeyName">
/// The name of the key whose value is to be retrieved. This value is in the form of a string; the <c>GetPrivateProfileInt</c> function converts the
/// string into an integer and returns the integer.
/// The name of the key whose value is to be retrieved. This value is in the form of a string; the <c>GetPrivateProfileInt</c>
/// function converts the string into an integer and returns the integer.
/// </param>
/// <param name="nDefault">The default value to return if the key name cannot be found in the initialization file.</param>
/// <param name="lpFileName">
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file
/// in the Windows directory.
/// </param>
/// <returns>
/// The return value is the integer equivalent of the string following the specified key name in the specified initialization file. If the key is not
/// found, the return value is the specified default value.
/// The return value is the integer equivalent of the string following the specified key name in the specified initialization file.
/// If the key is not found, the return value is the specified default value.
/// </returns>
// UINT WINAPI GetPrivateProfileInt( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ INT nDefault, _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724345(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
@ -28,38 +30,41 @@ namespace Vanara.PInvoke
/// <summary>Retrieves all the keys and values for the specified section of an initialization file.</summary>
/// <param name="lpAppName">The name of the section in the initialization file.</param>
/// <param name="lpReturnedString">
/// A pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one or more
/// null-terminated strings; the last string is followed by a second null character.
/// A pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one
/// or more null-terminated strings; the last string is followed by a second null character.
/// </param>
/// <param name="nSize">
/// The size of the buffer pointed to by the lpReturnedString parameter, in characters. The maximum profile section size is 32,767 characters.
/// </param>
/// <param name="lpFileName">
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file
/// in the Windows directory.
/// </param>
/// <returns>
/// The return value specifies the number of characters copied to the buffer, not including the terminating null character. If the buffer is not large
/// enough to contain all the key name and value pairs associated with the named section, the return value is equal to nSize minus two.
/// The return value specifies the number of characters copied to the buffer, not including the terminating null character. If the
/// buffer is not large enough to contain all the key name and value pairs associated with the named section, the return value is
/// equal to nSize minus two.
/// </returns>
// DWORD WINAPI GetPrivateProfileSection( _In_ LPCTSTR lpAppName, _Out_ LPTSTR lpReturnedString, _In_ DWORD nSize, _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx
// DWORD WINAPI GetPrivateProfileSection( _In_ LPCTSTR lpAppName, _Out_ LPTSTR lpReturnedString, _In_ DWORD nSize, _In_ LPCTSTR
// lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms724348")]
public static extern uint GetPrivateProfileSection(string lpAppName, StringBuilder lpReturnedString, uint nSize, string lpFileName);
/// <summary>Retrieves the names of all sections in an initialization file.</summary>
/// <param name="lpszReturnBuffer">
/// A pointer to a buffer that receives the section names associated with the named file. The buffer is filled with one or more <c>null</c>-terminated
/// strings; the last string is followed by a second <c>null</c> character.
/// A pointer to a buffer that receives the section names associated with the named file. The buffer is filled with one or more
/// <c>null</c>-terminated strings; the last string is followed by a second <c>null</c> character.
/// </param>
/// <param name="nSize">The size of the buffer pointed to by the lpszReturnBuffer parameter, in characters.</param>
/// <param name="lpFileName">
/// The name of the initialization file. If this parameter is <c>NULL</c>, the function searches the Win.ini file. If this parameter does not contain a
/// full path to the file, the system searches for the file in the Windows directory.
/// The name of the initialization file. If this parameter is <c>NULL</c>, the function searches the Win.ini file. If this parameter
/// does not contain a full path to the file, the system searches for the file in the Windows directory.
/// </param>
/// <returns>
/// The return value specifies the number of characters copied to the specified buffer, not including the terminating <c>null</c> character. If the
/// buffer is not large enough to contain all the section names associated with the specified initialization file, the return value is equal to the size
/// specified by nSize minus two.
/// The return value specifies the number of characters copied to the specified buffer, not including the terminating <c>null</c>
/// character. If the buffer is not large enough to contain all the section names associated with the specified initialization file,
/// the return value is equal to the size specified by nSize minus two.
/// </returns>
// DWORD WINAPI GetPrivateProfileSectionNames( _Out_ LPTSTR lpszReturnBuffer, _In_ DWORD nSize, _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724352(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
@ -68,100 +73,130 @@ namespace Vanara.PInvoke
/// <summary>Retrieves a string from the specified section in an initialization file.</summary>
/// <param name="lpAppName">
/// The name of the section containing the key name. If this parameter is <c>NULL</c>, the <c>GetPrivateProfileString</c> function copies all section
/// names in the file to the supplied buffer.
/// The name of the section containing the key name. If this parameter is <c>NULL</c>, the <c>GetPrivateProfileString</c> function
/// copies all section names in the file to the supplied buffer.
/// </param>
/// <param name="lpKeyName">
/// The name of the key whose associated string is to be retrieved. If this parameter is <c>NULL</c>, all key names in the section specified by the
/// lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
/// The name of the key whose associated string is to be retrieved. If this parameter is <c>NULL</c>, all key names in the section
/// specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
/// </param>
/// <param name="lpDefault">
/// <para>
/// A default string. If the lpKeyName key cannot be found in the initialization file, <c>GetPrivateProfileString</c> copies the default string to the
/// lpReturnedString buffer. If this parameter is <c>NULL</c>, the default is an empty string, "".
/// A default string. If the lpKeyName key cannot be found in the initialization file, <c>GetPrivateProfileString</c> copies the
/// default string to the lpReturnedString buffer. If this parameter is <c>NULL</c>, the default is an empty string, "".
/// </para>
/// <para>
/// Avoid specifying a default string with trailing blank characters. The function inserts a <c>null</c> character in the lpReturnedString buffer to
/// strip any trailing blanks.
/// Avoid specifying a default string with trailing blank characters. The function inserts a <c>null</c> character in the
/// lpReturnedString buffer to strip any trailing blanks.
/// </para>
/// </param>
/// <param name="lpReturnedString">A pointer to the buffer that receives the retrieved string.</param>
/// <param name="nSize">The size of the buffer pointed to by the lpReturnedString parameter, in characters.</param>
/// <param name="lpFileName">
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file
/// in the Windows directory.
/// </param>
/// <returns>
/// <para>The return value is the number of characters copied to the buffer, not including the terminating <c>null</c> character.</para>
/// <para>
/// If neither lpAppName nor lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold the requested string, the string is
/// truncated and followed by a <c>null</c> character, and the return value is equal to nSize minus one.
/// If neither lpAppName nor lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold the requested string,
/// the string is truncated and followed by a <c>null</c> character, and the return value is equal to nSize minus one.
/// </para>
/// <para>
/// If either lpAppName or lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold all the strings, the last string is
/// truncated and followed by two <c>null</c> characters. In this case, the return value is equal to nSize minus two.
/// If either lpAppName or lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold all the strings, the
/// last string is truncated and followed by two <c>null</c> characters. In this case, the return value is equal to nSize minus two.
/// </para>
/// <para>
/// In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set <c>errorno</c> with a
/// value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.
/// In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set
/// <c>errorno</c> with a value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.
/// </para>
/// </returns>
// DWORD WINAPI GetPrivateProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpDefault, _Out_ LPTSTR lpReturnedString, _In_
// DWORD nSize, _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v=vs.85).aspx
// DWORD WINAPI GetPrivateProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpDefault, _Out_ LPTSTR
// lpReturnedString, _In_ DWORD nSize, _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724353(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms724353")]
public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName);
/// <summary>
/// Retrieves the data associated with a key in the specified section of an initialization file. As it retrieves the data, the function calculates a
/// checksum and compares it with the checksum calculated by the <c>WritePrivateProfileStruct</c> function when the data was added to the file.
/// Retrieves the data associated with a key in the specified section of an initialization file. As it retrieves the data, the
/// function calculates a checksum and compares it with the checksum calculated by the <c>WritePrivateProfileStruct</c> function when
/// the data was added to the file.
/// </summary>
/// <param name="lpszSection">The name of the section in the initialization file.</param>
/// <param name="lpszKey">The name of the key whose data is to be retrieved.</param>
/// <param name="lpStruct">A pointer to the buffer that receives the data associated with the file, section, and key names.</param>
/// <param name="uSizeStruct">The size of the buffer pointed to by the lpStruct parameter, in bytes.</param>
/// <param name="szFile">
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file
/// in the Windows directory.
/// </param>
/// <returns>
/// <para>If the function succeeds, the return value is nonzero.</para>
/// <para>If the function fails, the return value is zero.</para>
/// </returns>
// BOOL WINAPI GetPrivateProfileStruct( _In_ LPCTSTR lpszSection, _In_ LPCTSTR lpszKey, _Out_ LPVOID lpStruct, _In_ UINT uSizeStruct, _In_ LPCTSTR
// szFile); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724356(v=vs.85).aspx
// BOOL WINAPI GetPrivateProfileStruct( _In_ LPCTSTR lpszSection, _In_ LPCTSTR lpszKey, _Out_ LPVOID lpStruct, _In_ UINT uSizeStruct,
// _In_ LPCTSTR szFile); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724356(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms724356")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetPrivateProfileStruct(string lpszSection, string lpszKey, IntPtr lpStruct, uint uSizeStruct, string szFile);
/// <summary>
/// Retrieves the data associated with a key in the specified section of an initialization file. As it retrieves the data, the
/// function calculates a checksum and compares it with the checksum calculated by the <c>WritePrivateProfileStruct</c> function when
/// the data was added to the file.
/// </summary>
/// <typeparam name="T">The type of the structure to retrieve.</typeparam>
/// <param name="lpszSection">The name of the section in the initialization file.</param>
/// <param name="lpszKey">The name of the key whose data is to be retrieved.</param>
/// <param name="lpStruct">Receives the data associated with the file, section, and key names.</param>
/// <param name="szFile">
/// The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file
/// in the Windows directory.
/// </param>
/// <returns>
/// <para>If the function succeeds, the return value is nonzero.</para>
/// <para>If the function fails, the return value is zero.</para>
/// </returns>
public static bool GetPrivateProfileStruct<T>(string lpszSection, string lpszKey, out T lpStruct, string szFile) where T : unmanaged
{
using (var mem = SafeHGlobalHandle.CreateFromStructure<T>())
{
var b = GetPrivateProfileStruct(lpszSection, lpszKey, mem, mem.Size, szFile);
lpStruct = mem.ToStructure<T>();
return b;
}
}
/// <summary>Retrieves an integer from a key in the specified section of the Win.ini file.</summary>
/// <param name="lpAppName">The name of the section containing the key name.</param>
/// <param name="lpKeyName">
/// The name of the key whose value is to be retrieved. This value is in the form of a string; the <c>GetProfileInt</c> function converts the string into
/// an integer and returns the integer.
/// The name of the key whose value is to be retrieved. This value is in the form of a string; the <c>GetProfileInt</c> function
/// converts the string into an integer and returns the integer.
/// </param>
/// <param name="nDefault">The default value to return if the key name cannot be found in the initialization file.</param>
/// <returns>
/// The return value is the integer equivalent of the string following the key name in Win.ini. If the function cannot find the key, the return value is
/// the default value. If the value of the key is less than zero, the return value is zero.
/// The return value is the integer equivalent of the string following the key name in Win.ini. If the function cannot find the key,
/// the return value is the default value. If the value of the key is less than zero, the return value is zero.
/// </returns>
// UINT WINAPI GetProfileInt( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ INT nDefault); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724360(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms724360")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern uint GetProfileInt(string lpAppName, string lpKeyName, int nDefault);
/// <summary>Retrieves all the keys and values for the specified section of the Win.ini file.</summary>
/// <param name="lpAppName">The name of the section in the Win.ini file.</param>
/// <param name="lpReturnedString">
/// A pointer to a buffer that receives the keys and values associated with the named section. The buffer is filled with one or more null-terminated
/// strings; the last string is followed by a second null character.
/// A pointer to a buffer that receives the keys and values associated with the named section. The buffer is filled with one or more
/// null-terminated strings; the last string is followed by a second null character.
/// </param>
/// <param name="nSize">
/// The size of the buffer pointed to by the lpReturnedString parameter, in characters. The maximum profile section size is 32,767 characters.
/// </param>
/// <returns>
/// The return value specifies the number of characters copied to the specified buffer, not including the terminating null character. If the buffer is
/// not large enough to contain all the keys and values associated with the named section, the return value is equal to the size specified by nSize minus two.
/// The return value specifies the number of characters copied to the specified buffer, not including the terminating null character.
/// If the buffer is not large enough to contain all the keys and values associated with the named section, the return value is equal
/// to the size specified by nSize minus two.
/// </returns>
// DWORD WINAPI GetProfileSection( _In_ LPCTSTR lpAppName, _Out_ LPTSTR lpReturnedString, _In_ DWORD nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724363(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
@ -170,20 +205,22 @@ namespace Vanara.PInvoke
/// <summary>Retrieves the string associated with a key in the specified section of the Win.ini file.</summary>
/// <param name="lpAppName">
/// The name of the section containing the key. If this parameter is <c>NULL</c>, the function copies all section names in the file to the supplied buffer.
/// The name of the section containing the key. If this parameter is <c>NULL</c>, the function copies all section names in the file
/// to the supplied buffer.
/// </param>
/// <param name="lpKeyName">
/// The name of the key whose associated string is to be retrieved. If this parameter is <c>NULL</c>, the function copies all keys in the given section
/// to the supplied buffer. Each string is followed by a <c>null</c> character, and the final string is followed by a second <c>null</c> character.
/// The name of the key whose associated string is to be retrieved. If this parameter is <c>NULL</c>, the function copies all keys in
/// the given section to the supplied buffer. Each string is followed by a <c>null</c> character, and the final string is followed by
/// a second <c>null</c> character.
/// </param>
/// <param name="lpDefault">
/// <para>
/// A default string. If the lpKeyName key cannot be found in the initialization file, <c>GetProfileString</c> copies the default string to the
/// lpReturnedString buffer. If this parameter is <c>NULL</c>, the default is an empty string, "".
/// A default string. If the lpKeyName key cannot be found in the initialization file, <c>GetProfileString</c> copies the default
/// string to the lpReturnedString buffer. If this parameter is <c>NULL</c>, the default is an empty string, "".
/// </para>
/// <para>
/// Avoid specifying a default string with trailing blank characters. The function inserts a <c>null</c> character in the lpReturnedString buffer to
/// strip any trailing blanks.
/// Avoid specifying a default string with trailing blank characters. The function inserts a <c>null</c> character in the
/// lpReturnedString buffer to strip any trailing blanks.
/// </para>
/// </param>
/// <param name="lpReturnedString">A pointer to a buffer that receives the character string.</param>
@ -191,31 +228,36 @@ namespace Vanara.PInvoke
/// <returns>
/// <para>The return value is the number of characters copied to the buffer, not including the <c>null</c>-terminating character.</para>
/// <para>
/// If neither lpAppName nor lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold the requested string, the string is
/// truncated and followed by a <c>null</c> character, and the return value is equal to nSize minus one.
/// If neither lpAppName nor lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold the requested string,
/// the string is truncated and followed by a <c>null</c> character, and the return value is equal to nSize minus one.
/// </para>
/// <para>
/// If either lpAppName or lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold all the strings, the last string is
/// truncated and followed by two <c>null</c> characters. In this case, the return value is equal to nSize minus two.
/// If either lpAppName or lpKeyName is <c>NULL</c> and the supplied destination buffer is too small to hold all the strings, the
/// last string is truncated and followed by two <c>null</c> characters. In this case, the return value is equal to nSize minus two.
/// </para>
/// </returns>
// DWORD WINAPI GetProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpDefault, _Out_ LPTSTR lpReturnedString, _In_ DWORD
// nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724366(v=vs.85).aspx
// DWORD WINAPI GetProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpDefault, _Out_ LPTSTR
// lpReturnedString, _In_ DWORD nSize); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724366(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms724366")]
public static extern uint GetProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize);
/// <summary>Replaces the keys and values for the specified section in an initialization file.</summary>
/// <param name="lpAppName">The name of the section in which data is written. This section name is typically the name of the calling application.</param>
/// <param name="lpString">The new key names and associated values that are to be written to the named section. This string is limited to 65,535 bytes.</param>
/// <param name="lpAppName">
/// The name of the section in which data is written. This section name is typically the name of the calling application.
/// </param>
/// <param name="lpString">
/// The new key names and associated values that are to be written to the named section. This string is limited to 65,535 bytes.
/// </param>
/// <param name="lpFileName">
/// <para>
/// The name of the initialization file. If this parameter does not contain a full path for the file, the function searches the Windows directory for the
/// file. If the file does not exist and lpFileName does not contain a full path, the function creates the file in the Windows directory.
/// The name of the initialization file. If this parameter does not contain a full path for the file, the function searches the
/// Windows directory for the file. If the file does not exist and lpFileName does not contain a full path, the function creates the
/// file in the Windows directory.
/// </para>
/// <para>
/// If the file exists and was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function creates a
/// file using ANSI characters.
/// If the file exists and was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the
/// function creates a file using ANSI characters.
/// </para>
/// </param>
/// <returns>
@ -230,79 +272,117 @@ namespace Vanara.PInvoke
/// <summary>Copies a string into the specified section of an initialization file.</summary>
/// <param name="lpAppName">
/// The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is
/// case-independent; the string can be any combination of uppercase and lowercase letters.
/// The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section
/// is case-independent; the string can be any combination of uppercase and lowercase letters.
/// </param>
/// <param name="lpKeyName">
/// The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is
/// <c>NULL</c>, the entire section, including all entries within the section, is deleted.
/// The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this
/// parameter is <c>NULL</c>, the entire section, including all entries within the section, is deleted.
/// </param>
/// <param name="lpString">
/// A <c>null</c>-terminated string to be written to the file. If this parameter is <c>NULL</c>, the key pointed to by the lpKeyName parameter is deleted.
/// A <c>null</c>-terminated string to be written to the file. If this parameter is <c>NULL</c>, the key pointed to by the lpKeyName
/// parameter is deleted.
/// </param>
/// <param name="lpFileName">
/// <para>The name of the initialization file.</para>
/// <para>
/// If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
/// If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function
/// writes ANSI characters.
/// </para>
/// </param>
/// <returns>
/// <para>If the function successfully copies the string to the initialization file, the return value is nonzero.</para>
/// <para>
/// If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is zero. To get
/// extended error information, call <c>GetLastError</c>.
/// If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is
/// zero. To get extended error information, call <c>GetLastError</c>.
/// </para>
/// </returns>
// BOOL WINAPI WritePrivateProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpString, _In_ LPCTSTR lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms725501(v=vs.85).aspx
// BOOL WINAPI WritePrivateProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpString, _In_ LPCTSTR
// lpFileName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms725501(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms725501")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
/// <summary>
/// Copies data into a key in the specified section of an initialization file. As it copies the data, the function calculates a checksum and appends it
/// to the end of the data. The <c>GetPrivateProfileStruct</c> function uses the checksum to ensure the integrity of the data.
/// Copies data into a key in the specified section of an initialization file. As it copies the data, the function calculates a
/// checksum and appends it to the end of the data. The <c>GetPrivateProfileStruct</c> function uses the checksum to ensure the
/// integrity of the data.
/// </summary>
/// <param name="lpszSection">
/// The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case
/// independent, the string can be any combination of uppercase and lowercase letters.
/// The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section
/// is case independent, the string can be any combination of uppercase and lowercase letters.
/// </param>
/// <param name="lpszKey">
/// The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is
/// <c>NULL</c>, the entire section, including all keys and entries within the section, is deleted.
/// The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this
/// parameter is <c>NULL</c>, the entire section, including all keys and entries within the section, is deleted.
/// </param>
/// <param name="lpStruct">The data to be copied. If this parameter is <c>NULL</c>, the key is deleted.</param>
/// <param name="uSizeStruct">The size of the buffer pointed to by the lpStruct parameter, in bytes.</param>
/// <param name="szFile">
/// <para>The name of the initialization file. If this parameter is <c>NULL</c>, the information is copied into the Win.ini file.</para>
/// <para>
/// If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
/// If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function
/// writes ANSI characters.
/// </para>
/// </param>
/// <returns>
/// <para>If the function successfully copies the string to the initialization file, the return value is nonzero.</para>
/// <para>
/// If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is zero. To get
/// extended error information, call <c>GetLastError</c>.
/// If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is
/// zero. To get extended error information, call <c>GetLastError</c>.
/// </para>
/// </returns>
// BOOL WINAPI WritePrivateProfileStruct( _In_ LPCTSTR lpszSection, _In_ LPCTSTR lpszKey, _In_ LPVOID lpStruct, _In_ UINT uSizeStruct, _In_ LPCTSTR
// szFile); https://msdn.microsoft.com/en-us/library/windows/desktop/ms725502(v=vs.85).aspx
// BOOL WINAPI WritePrivateProfileStruct( _In_ LPCTSTR lpszSection, _In_ LPCTSTR lpszKey, _In_ LPVOID lpStruct, _In_ UINT
// uSizeStruct, _In_ LPCTSTR szFile); https://msdn.microsoft.com/en-us/library/windows/desktop/ms725502(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]
[PInvokeData("Winbase.h", MSDNShortId = "ms725502")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WritePrivateProfileStruct(string lpszSection, string lpszKey, [In] IntPtr lpStruct, uint uSizeStruct, string szFile);
/// <summary>
/// Replaces the contents of the specified section in the Win.ini file with specified keys and values. If Win.ini uses Unicode characters, the function
/// writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
/// Copies data into a key in the specified section of an initialization file. As it copies the data, the function calculates a
/// checksum and appends it to the end of the data. The <c>GetPrivateProfileStruct</c> function uses the checksum to ensure the
/// integrity of the data.
/// </summary>
/// <param name="lpszSection">
/// The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section
/// is case independent, the string can be any combination of uppercase and lowercase letters.
/// </param>
/// <param name="lpszKey">
/// The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this
/// parameter is <c>NULL</c>, the entire section, including all keys and entries within the section, is deleted.
/// </param>
/// <param name="lpStruct">The data to be copied.</param>
/// <param name="szFile">
/// <para>The name of the initialization file. If this parameter is <c>NULL</c>, the information is copied into the Win.ini file.</para>
/// <para>
/// If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function
/// writes ANSI characters.
/// </para>
/// </param>
/// <returns>
/// <para>If the function successfully copies the string to the initialization file, the return value is nonzero.</para>
/// <para>
/// If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is
/// zero. To get extended error information, call <c>GetLastError</c>.
/// </para>
/// </returns>
public static bool WritePrivateProfileStruct<T>(string lpszSection, string lpszKey, in T lpStruct, string szFile) where T : unmanaged =>
WritePrivateProfileStruct(lpszSection, lpszKey, new PinnedObject(lpStruct), (uint)Marshal.SizeOf(lpStruct), szFile);
/// <summary>
/// Replaces the contents of the specified section in the Win.ini file with specified keys and values. If Win.ini uses Unicode
/// characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
/// </summary>
/// <param name="lpAppName">The name of the section. This section name is typically the name of the calling application.</param>
/// <param name="lpString">
/// <para>The new key names and associated values that are to be written to the named section. This string is limited to 65,535 bytes.</para>
/// <para>
/// If the file exists and was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function creates a
/// file using ANSI characters.
/// The new key names and associated values that are to be written to the named section. This string is limited to 65,535 bytes.
/// </para>
/// <para>
/// If the file exists and was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the
/// function creates a file using ANSI characters.
/// </para>
/// </param>
/// <returns>
@ -316,23 +396,27 @@ namespace Vanara.PInvoke
public static extern bool WriteProfileSection(string lpAppName, string lpString);
/// <summary>
/// Copies a string into the specified section of the Win.ini file. If Win.ini uses Unicode characters, the function writes Unicode characters to the
/// file. Otherwise, the function writes ANSI characters.
/// Copies a string into the specified section of the Win.ini file. If Win.ini uses Unicode characters, the function writes Unicode
/// characters to the file. Otherwise, the function writes ANSI characters.
/// </summary>
/// <param name="lpAppName">
/// The section to which the string is to be copied. If the section does not exist, it is created. The name of the section is not case-sensitive; the
/// string can be any combination of uppercase and lowercase letters.
/// The section to which the string is to be copied. If the section does not exist, it is created. The name of the section is not
/// case-sensitive; the string can be any combination of uppercase and lowercase letters.
/// </param>
/// <param name="lpKeyName">
/// The key to be associated with the string. If the key does not exist in the specified section, it is created. If this parameter is <c>NULL</c>, the
/// entire section, including all entries in the section, is deleted.
/// The key to be associated with the string. If the key does not exist in the specified section, it is created. If this parameter is
/// <c>NULL</c>, the entire section, including all entries in the section, is deleted.
/// </param>
/// <param name="lpString">
/// A <c>null</c>-terminated string to be written to the file. If this parameter is <c>NULL</c>, the key pointed to by the lpKeyName parameter is deleted.
/// A <c>null</c>-terminated string to be written to the file. If this parameter is <c>NULL</c>, the key pointed to by the lpKeyName
/// parameter is deleted.
/// </param>
/// <returns>
/// <para>If the function successfully copies the string to the Win.ini file, the return value is nonzero.</para>
/// <para>If the function fails, or if it flushes the cached version of Win.ini, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
/// <para>
/// If the function fails, or if it flushes the cached version of Win.ini, the return value is zero. To get extended error
/// information, call <c>GetLastError</c>.
/// </para>
/// </returns>
// BOOL WINAPI WriteProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpString); https://msdn.microsoft.com/en-us/library/windows/desktop/ms725504(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)]

View File

@ -48,6 +48,7 @@
<Compile Include="AppModelTests.cs" />
<Compile Include="InterlockedApiTests.cs" />
<Compile Include="InteropServices\SafeLocalHandleTests.cs" />
<Compile Include="WinBase.ProfileTests.cs" />
<Compile Include="WinBase.ProcessThreadTests.cs" />
<Compile Include="WinBase.PowerTests.cs" />
<Compile Include="WinBase.MemMgmtTests.cs" />

View File

@ -0,0 +1,76 @@
using NUnit.Framework;
using System.Text;
using static Vanara.PInvoke.Kernel32;
namespace Vanara.PInvoke.Tests
{
[TestFixture]
public partial class WinBaseTests_Profile
{
[Test]
public void PrivateProfileTest()
{
const string sec = "Section";
using (var tmp = new TempFile(""))
{
Assert.That(WritePrivateProfileSection(sec, "Key0=10\0", tmp.FullName), ResultIs.Successful);
Assert.That(WritePrivateProfileString(sec, "Key1", "Value1", tmp.FullName), ResultIs.Successful);
Assert.That(WritePrivateProfileStruct(sec, "Key2", new RECT(1, 2, 3, 4), tmp.FullName), ResultIs.Successful);
Assert.That(WritePrivateProfileStruct(sec, "Key3", 4, tmp.FullName), ResultIs.Successful);
TestContext.WriteLine(System.IO.File.ReadAllText(tmp.FullName));
Assert.That(GetPrivateProfileInt(sec, "Key0", 0, tmp.FullName), Is.EqualTo(10));
Assert.That(GetPrivateProfileInt(sec, "Key3", 0, tmp.FullName), Is.Not.EqualTo(4));
Assert.That(GetPrivateProfileInt(sec, "Key4", 0, tmp.FullName), Is.EqualTo(0));
var sb = new StringBuilder(1024);
Assert.That(GetPrivateProfileSection(sec, sb, (uint)sb.Capacity, tmp.FullName), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
TestContext.WriteLine(sb);
sb.Clear();
Assert.That(GetPrivateProfileSectionNames(sb, (uint)sb.Capacity, tmp.FullName), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
TestContext.WriteLine(sb);
sb.Clear();
Assert.That(GetPrivateProfileString(sec, "Key0", null, sb, (uint)sb.Capacity, tmp.FullName), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
Assert.That(sb.ToString(), Is.EqualTo("10"));
sb.Clear();
Assert.That(GetPrivateProfileString(sec, "Key1", null, sb, (uint)sb.Capacity, tmp.FullName), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
Assert.That(sb.ToString(), Is.EqualTo("Value1"));
sb.Clear();
Assert.That(GetPrivateProfileString(sec, "Key3", null, sb, (uint)sb.Capacity, tmp.FullName), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
Assert.That(sb.ToString(), Is.EqualTo("0400000004"));
Assert.That(GetPrivateProfileStruct(sec, "Key2", out RECT r, tmp.FullName), Is.True);
Assert.That(r.bottom, Is.EqualTo(4));
Assert.That(GetPrivateProfileStruct(sec, "Key3", out int i, tmp.FullName), Is.True);
Assert.That(i, Is.EqualTo(4));
Assert.That(GetPrivateProfileStruct(sec, "Key0", out i, tmp.FullName), Is.False);
}
}
[Test]
public void ProfileTest()
{
const string sec = "Section";
Assert.That(WriteProfileSection(sec, "Key0=10\0"), ResultIs.Successful);
Assert.That(WriteProfileString(sec, "Key1", "Value1"), ResultIs.Successful);
Assert.That(GetProfileInt(sec, "Key0", 0), Is.EqualTo(10));
Assert.That(GetProfileInt(sec, "Key4", 0), Is.EqualTo(0));
var sb = new StringBuilder(1024);
Assert.That(GetProfileSection(sec, sb, (uint)sb.Capacity), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
TestContext.WriteLine(sb);
sb.Clear();
Assert.That(GetProfileString(sec, "Key0", null, sb, (uint)sb.Capacity), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
Assert.That(sb.ToString(), Is.EqualTo("10"));
sb.Clear();
Assert.That(GetProfileString(sec, "Key1", null, sb, (uint)sb.Capacity), Is.GreaterThan(0).And.Not.EqualTo(sb.Capacity - 2));
Assert.That(sb.ToString(), Is.EqualTo("Value1"));
}
}
}