diff options
author | Simon Rozman <simon@rozman.si> | 2020-10-14 08:29:01 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-10-30 16:51:00 +0100 |
commit | 84113bd294141742fbc5f10e645e74c395e35217 (patch) | |
tree | 36d3b90ba2391290655600c675575f693dea09a3 /api/resource.c | |
parent | api: unify hardware ID string (diff) | |
download | wintun-84113bd294141742fbc5f10e645e74c395e35217.tar.xz wintun-84113bd294141742fbc5f10e645e74c395e35217.zip |
api: unify and document resource loading
Signed-off-by: Simon Rozman <simon@rozman.si>
Diffstat (limited to 'api/resource.c')
-rw-r--r-- | api/resource.c | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/api/resource.c b/api/resource.c index b25b96d..4c5be64 100644 --- a/api/resource.c +++ b/api/resource.c @@ -5,27 +5,60 @@ #include "pch.h" +/** + * Locates RT_RCDATA resource memory address and size. + * + * ResourceName Name of the RT_RCDATA resource. Use MAKEINTRESOURCEW to locate resource by ID. + * + * Address Pointer to a pointer variable to receive resource address. + * + * Size Pointer to a variable to receive resource size. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise. + */ WINTUN_STATUS -CopyResource( - _In_z_ const WCHAR *DestinationPath, - _In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes, - _In_z_ const WCHAR *ResourceName) +ResourceGetAddress(_In_z_ const WCHAR *ResourceName, _Out_ const VOID **Address, _Out_ DWORD *Size) { HRSRC FoundResource = FindResourceW(ResourceModule, ResourceName, RT_RCDATA); if (!FoundResource) return WINTUN_LOGGER_LAST_ERROR(L"Failed to find resource"); - DWORD SizeResource = SizeofResource(ResourceModule, FoundResource); - if (!SizeResource) + *Size = SizeofResource(ResourceModule, FoundResource); + if (!*Size) return WINTUN_LOGGER_LAST_ERROR(L"Failed to size resource"); HGLOBAL LoadedResource = LoadResource(ResourceModule, FoundResource); if (!LoadedResource) return WINTUN_LOGGER_LAST_ERROR(L"Failed to load resource"); - LPVOID LockedResource = LockResource(LoadedResource); - if (!LockedResource) + *Address = LockResource(LoadedResource); + if (!*Address) { WINTUN_LOGGER(WINTUN_LOG_ERR, L"Failed to lock resource"); return ERROR_LOCK_FAILED; } + return ERROR_SUCCESS; +} + +/** + * Copies resource to a file. + * + * DestinationPath File path + * + * SecurityAttributes File security attributes. May be NULL for detault. + * + * ResourceName Name of the RT_RCDATA resource. Use MAKEINTRESOURCEW to locate resource by ID. + * + * @return ERROR_SUCCESS on success; Win32 error code otherwise. + */ +WINTUN_STATUS +ResourceCopyToFile( + _In_z_ const WCHAR *DestinationPath, + _In_opt_ SECURITY_ATTRIBUTES *SecurityAttributes, + _In_z_ const WCHAR *ResourceName) +{ + const VOID *LockedResource; + DWORD SizeResource; + DWORD Result = ResourceGetAddress(ResourceName, &LockedResource, &SizeResource); + if (Result != ERROR_SUCCESS) + return WINTUN_LOGGER_ERROR("Failed to locate resource", Result); HANDLE DestinationHandle = CreateFileW( DestinationPath, GENERIC_WRITE, @@ -37,7 +70,6 @@ CopyResource( if (DestinationHandle == INVALID_HANDLE_VALUE) return WINTUN_LOGGER_LAST_ERROR(L"Failed to create file"); DWORD BytesWritten; - DWORD Result = ERROR_SUCCESS; if (!WriteFile(DestinationHandle, LockedResource, SizeResource, &BytesWritten, NULL)) Result = WINTUN_LOGGER_LAST_ERROR(L"Failed to write file"); if (BytesWritten != SizeResource) |