Skip to content

Commit 57179cf

Browse files
committed
Improve PhGetClassObject error handling
1 parent f7b150d commit 57179cf

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

phlib/include/phutil.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,13 +1326,15 @@ PhFileReadAllText(
13261326
_In_ BOOLEAN Unicode
13271327
);
13281328

1329+
_Success_(return == S_OK)
13291330
PHLIBAPI
1330-
PVOID
1331+
HRESULT
13311332
NTAPI
13321333
PhGetClassObject(
13331334
_In_ PWSTR DllName,
13341335
_In_ REFCLSID Rclsid,
1335-
_In_ REFIID Riid
1336+
_In_ REFIID Riid,
1337+
_Out_ PVOID* Ppv
13361338
);
13371339

13381340
#ifdef __cplusplus

phlib/util.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6755,37 +6755,44 @@ PVOID PhFileReadAllText(
67556755
return string;
67566756
}
67576757

6758-
PVOID PhGetClassObject(
6758+
_Success_(return == S_OK)
6759+
HRESULT PhGetClassObject(
67596760
_In_ PWSTR DllName,
67606761
_In_ REFCLSID Rclsid,
6761-
_In_ REFIID Riid
6762+
_In_ REFIID Riid,
6763+
_Out_ PVOID* Ppv
67626764
)
67636765
{
6764-
HRESULT (WINAPI *DllGetClassObject_I)(_In_ REFCLSID rclsid, _In_ REFIID riid, _Out_ PVOID* ppv) = NULL;
6766+
HRESULT status = S_FALSE;
6767+
HRESULT (WINAPI* DllGetClassObject_I)(_In_ REFCLSID rclsid, _In_ REFIID riid, _COM_Outptr_ PVOID* ppv);
67656768
IClassFactory* classFactory;
6766-
PVOID moduleHandle = NULL;
6767-
PVOID classInterface = NULL;
6769+
PVOID moduleHandle;
67686770

67696771
if (!(moduleHandle = PhGetLoaderEntryDllBase(DllName)))
6770-
moduleHandle = LoadLibrary(DllName);
6771-
6772-
if (!moduleHandle)
6773-
return NULL;
6772+
{
6773+
if (!(moduleHandle = LoadLibrary(DllName)))
6774+
return ERROR_MOD_NOT_FOUND;
6775+
}
67746776

67756777
if (!(DllGetClassObject_I = PhGetDllBaseProcedureAddress(moduleHandle, "DllGetClassObject", 0)))
6776-
return NULL;
6778+
return ERROR_PROC_NOT_FOUND;
67776779

6778-
if (FAILED(DllGetClassObject_I(Rclsid, &IID_IClassFactory, &classFactory)))
6779-
return NULL;
6780+
status = DllGetClassObject_I(
6781+
Rclsid,
6782+
&IID_IClassFactory,
6783+
&classFactory
6784+
);
67806785

6781-
if (FAILED(IClassFactory_CreateInstance(classFactory, NULL, Riid, &classInterface)))
6782-
{
6783-
IClassFactory_Release(classFactory);
6784-
return NULL;
6785-
}
6786-
else
6787-
{
6788-
IClassFactory_Release(classFactory);
6789-
return classInterface;
6790-
}
6786+
if (FAILED(status))
6787+
return status;
6788+
6789+
status = IClassFactory_CreateInstance(
6790+
classFactory,
6791+
NULL,
6792+
Riid,
6793+
Ppv
6794+
);
6795+
6796+
IClassFactory_Release(classFactory);
6797+
return status;
67916798
}

0 commit comments

Comments
 (0)