Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Last active July 25, 2023 09:50
Show Gist options
  • Save Aetopia/9a8b1ccac7384b9ba724112261ab4177 to your computer and use it in GitHub Desktop.
Save Aetopia/9a8b1ccac7384b9ba724112261ab4177 to your computer and use it in GitHub Desktop.
WinIni
#include <windows.h>
#include <shlwapi.h>
// (Unicode) Get the required length to store a string of section names.
DWORD GetSectionNamesLengthW(IN LPCWSTR lpFileName)
{
DWORD bufferLength = 0;
WCHAR *returnBuffer = NULL;
if (!PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName))
return 0;
do
{
returnBuffer = realloc(returnBuffer, sizeof(WCHAR) * (bufferLength += 1));
GetPrivateProfileSectionNamesW(returnBuffer, bufferLength, lpFileName);
} while (GetLastError() == ERROR_MORE_DATA);
free(returnBuffer);
return (bufferLength -= 2) != -1 ? bufferLength : 0;
}
// (ANSI) Get the required length to store a string of section names.
DWORD GetSectionNamesLengthA(IN LPCSTR lpFileName)
{
DWORD bufferLength = 0;
CHAR *returnBuffer = NULL;
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName))
return 0;
do
{
returnBuffer = realloc(returnBuffer, (bufferLength += 1));
GetPrivateProfileSectionNamesA(returnBuffer, bufferLength, lpFileName);
} while (GetLastError() == ERROR_MORE_DATA);
free(returnBuffer);
return (bufferLength -= 2) != -1 ? bufferLength : 0;
}
// (Unicode) Get the required length to store a string of key names of the specified section.
DWORD GetKeyNamesLengthW(IN LPCWSTR lpSectionName, IN LPCWSTR lpFileName)
{
DWORD size = 0;
WCHAR *returnedString = NULL;
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName) || !lpSectionName)
return 0;
do
{
returnedString = realloc(returnedString, sizeof(WCHAR) * (size += 1));
GetPrivateProfileStringW(lpSectionName, NULL, NULL, returnedString, size, lpFileName);
} while (GetLastError() == ERROR_MORE_DATA);
free(returnedString);
return (size -= 2) != -1 ? size : 0;
}
// (ANSI) Get the required length to store a string of key names of the specified section.
DWORD GetKeyNamesLengthA(IN LPCSTR lpSectionName, IN LPCSTR lpFileName)
{
DWORD size = 0;
CHAR *returnedString = NULL;
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName) || !lpSectionName)
return 0;
do
{
returnedString = realloc(returnedString, (size += 1));
GetPrivateProfileStringA(lpSectionName, NULL, NULL, returnedString, size, lpFileName);
} while (GetLastError() == ERROR_MORE_DATA);
free(returnedString);
return (size -= 2) != -1 ? size : 0;
}
// (Unicode) Get the required length to store a string of a value of the specified section and key.
DWORD GetKeyValueLengthW(IN LPCWSTR lpSectionName, IN LPCWSTR lpKeyName, IN LPCWSTR lpFileName)
{
DWORD size = 0;
WCHAR *returnedString = NULL;
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName) || !lpSectionName)
return 0;
do
{
returnedString = realloc(returnedString, sizeof(WCHAR) * (size += 1));
GetPrivateProfileStringW(lpSectionName, lpKeyName, NULL, returnedString, size, lpFileName);
} while (GetLastError() == ERROR_MORE_DATA);
free(returnedString);
return (size -= 2) != -1 ? size : 0;
}
// (ANSI) Get the required length to store a string of a value of the specified section and key.
DWORD GetKeyValueLengthA(IN LPCSTR lpSectionName, IN LPCSTR lpKeyName, IN LPCSTR lpFileName)
{
DWORD size = 0;
CHAR *returnedString = NULL;
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName) || !lpSectionName)
return 0;
do
{
returnedString = realloc(returnedString, sizeof(WCHAR) * (size += 1));
GetPrivateProfileStringA(lpSectionName, lpKeyName, NULL, returnedString, size, lpFileName);
} while (GetLastError() == ERROR_MORE_DATA);
free(returnedString);
return (size -= 2) != -1 ? size : 0;
}
// (Unicode) Get a string of section names from the specified file.
BOOL GetSectionNamesW(IN OUT LPWSTR lpszReturnBuffer, IN DWORD nSize, IN LPCWSTR lpFileName)
{
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName))
return FALSE;
return GetPrivateProfileSectionNamesW(lpszReturnBuffer, nSize, lpFileName) == nSize ? TRUE : FALSE;
}
// (ANSI) Get a string of section names from the specified file.
BOOL GetSectionNamesA(IN OUT LPSTR lpszReturnBuffer, IN DWORD nSize, IN LPCSTR lpFileName)
{
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName))
return FALSE;
return GetPrivateProfileSectionNamesA(lpszReturnBuffer, nSize, lpFileName) == nSize ? TRUE : FALSE;
}
// (Unicode) Get a string of key names from the specified file & section name.
BOOL GetKeyNamesW(IN LPCWSTR lpSectionName, IN OUT LPWSTR lpReturnedString, IN DWORD nSize, IN LPCWSTR lpFileName)
{
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName) || !lpSectionName)
return FALSE;
return GetPrivateProfileStringW(lpSectionName, NULL, NULL, lpReturnedString, nSize, lpFileName) == nSize ? TRUE : FALSE;
}
// (ANSI) Get a string of key names from the specified file & section name.
BOOL GetKeyNamesA(IN LPCSTR lpSectionName, IN OUT LPSTR lpReturnedString, IN DWORD nSize, IN LPCSTR lpFileName)
{
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName) || !lpSectionName)
return FALSE;
return GetPrivateProfileStringA(lpSectionName, NULL, NULL, lpReturnedString, nSize, lpFileName) == nSize ? TRUE : FALSE;
}
// (Unicode) Get a string of the value from the specified file, section and key name.
BOOL GetKeyValueW(IN LPCWSTR lpSectionName, IN LPCWSTR lpKeyName, IN OUT LPWSTR lpReturnedString, DWORD nSize, IN LPCWSTR lpFileName)
{
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName) || !lpSectionName || !lpKeyName)
return FALSE;
return GetPrivateProfileStringW(lpSectionName, lpKeyName, NULL, lpReturnedString, nSize, lpFileName) == nSize ? TRUE : FALSE;
}
// (ANSI) Get a string of the value from the specified file, section and key name.
BOOL GetKeyValueA(IN LPCSTR lpSectionName, IN LPCSTR lpKeyName, IN OUT LPSTR lpReturnedString, DWORD nSize, IN LPCSTR lpFileName)
{
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName) || !lpSectionName || !lpKeyName)
return FALSE;
return GetPrivateProfileStringA(lpSectionName, lpKeyName, NULL, lpReturnedString, nSize, lpFileName) == nSize ? TRUE : FALSE;
}
// (Unicode) Write the the following section name to the specified file.
BOOL WriteSectionNameW(IN LPCWSTR lpSectionName, IN LPCWSTR lpFileName)
{
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName) || !lpSectionName)
return FALSE;
return WritePrivateProfileSectionW(lpSectionName, L"", lpFileName);
}
// (ANSI) Write the following section name to the specified file.
BOOL WriteSectionNameA(IN LPCSTR lpSectionName, IN LPCSTR lpFileName)
{
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName) || !lpSectionName)
return FALSE;
return WritePrivateProfileSectionA(lpSectionName, "", lpFileName) == nSize ? TRUE : FALSE;
}
// (Unicode) Write the following key-value pair to the specified file and section.
BOOL WriteKeyValueW(IN LPCWSTR lpSectionName, IN LPCWSTR lpKeyName, IN LPCWSTR lpString, IN LPCWSTR lpFileName)
{
if (PathIsRelativeW(lpFileName) || !PathFileExistsW(lpFileName) || !lpSectionName || !lpKeyName || !lpString)
return FALSE;
return WritePrivateProfileStringW(lpSectionName, lpKeyName, lpString, lpFileName) == nSize ? TRUE : FALSE;
}
// (ANSI) Write the following key-value pair to the specified file and section.
BOOL WriteKeyValueA(IN LPCSTR lpSectionName, IN LPCSTR lpKeyName, IN LPCSTR lpString, IN LPCSTR lpFileName)
{
if (PathIsRelativeA(lpFileName) || !PathFileExistsA(lpFileName) || !lpSectionName || !lpKeyName || !lpString)
return FALSE;
return WritePrivateProfileStringA(lpSectionName, lpKeyName, lpString, lpFileName) == nSize ? TRUE : FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment