Sweden-Number/dlls/setupapi/misc.c

175 lines
4.4 KiB
C
Raw Normal View History

/*
* Setupapi miscellaneous functions
*
* Copyright 2005 Eric Kohl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winreg.h"
#include "setupapi.h"
#include "wine/unicode.h"
/**************************************************************************
* MyFree [SETUPAPI.@]
*
* Frees an allocated memory block from the process heap.
*
* PARAMS
* lpMem [I] pointer to memory block which will be freed
*
* RETURNS
* None
*/
VOID WINAPI MyFree(LPVOID lpMem)
{
HeapFree(GetProcessHeap(), 0, lpMem);
}
/**************************************************************************
* MyMalloc [SETUPAPI.@]
*
* Allocates memory block from the process heap.
*
* PARAMS
* dwSize [I] size of the allocated memory block
*
* RETURNS
* Success: pointer to allocated memory block
* Failure: NULL
*/
LPVOID WINAPI MyMalloc(DWORD dwSize)
{
return HeapAlloc(GetProcessHeap(), 0, dwSize);
}
/**************************************************************************
* MyRealloc [SETUPAPI.@]
*
* Changes the size of an allocated memory block or allocates a memory
* block from the process heap.
*
* PARAMS
* lpSrc [I] pointer to memory block which will be resized
* dwSize [I] new size of the memory block
*
* RETURNS
* Success: pointer to the resized memory block
* Failure: NULL
*
* NOTES
* If lpSrc is a NULL-pointer, then MyRealloc allocates a memory
* block like MyMalloc.
*/
LPVOID WINAPI MyRealloc(LPVOID lpSrc, DWORD dwSize)
{
if (lpSrc == NULL)
return HeapAlloc(GetProcessHeap(), 0, dwSize);
return HeapReAlloc(GetProcessHeap(), 0, lpSrc, dwSize);
}
/**************************************************************************
* DuplicateString [SETUPAPI.@]
*
* Duplicates a unicode string.
*
* PARAMS
* lpSrc [I] pointer to the unicode string that will be duplicated
*
* RETURNS
* Success: pointer to the duplicated unicode string
* Failure: NULL
*
* NOTES
* Call MyFree() to release the duplicated string.
*/
LPWSTR WINAPI DuplicateString(LPCWSTR lpSrc)
{
LPWSTR lpDst;
lpDst = MyMalloc((lstrlenW(lpSrc) + 1) * sizeof(WCHAR));
if (lpDst == NULL)
return NULL;
strcpyW(lpDst, lpSrc);
return lpDst;
}
/**************************************************************************
* QueryRegistryValue [SETUPAPI.@]
*
* Retrieves value data from the registry and allocates memory for the
* value data.
*
* PARAMS
* hKey [I] Handle of the key to query
* lpValueName [I] Name of value under hkey to query
* lpData [O] Destination for the values contents,
* lpType [O] Destination for the value type
* lpcbData [O] Destination for the size of data
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: Otherwise
*
* NOTES
* Use MyFree to release the lpData buffer.
*/
LONG WINAPI QueryRegistryValue(HKEY hKey,
LPCWSTR lpValueName,
LPBYTE *lpData,
LPDWORD lpType,
LPDWORD lpcbData)
{
LONG lError;
/* Get required buffer size */
*lpcbData = 0;
lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, NULL, lpcbData);
if (lError != ERROR_SUCCESS)
return lError;
/* Allocate buffer */
*lpData = MyMalloc(*lpcbData);
if (*lpData == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
/* Query registry value */
lError = RegQueryValueExW(hKey, lpValueName, 0, lpType, *lpData, lpcbData);
if (lError != ERROR_SUCCESS)
MyFree(*lpData);
return lError;
}