Skip to content

Commit

Permalink
Merge pull request #33 from MartinKuschnik/31-exception-thrown-in-des…
Browse files Browse the repository at this point in the history
…tructors-causing-program-crash

Migrated custom ReleaseIUnknown to Marshal.Release, custom AddRef method to Marshal.AddRef and custom QueryInterface method to Marshal.QueryInterface.
  • Loading branch information
MartinKuschnik authored Sep 4, 2024
2 parents 9fc5129 + 6823d92 commit 8549f23
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 45 deletions.
8 changes: 4 additions & 4 deletions WmiLight.Native/WmiLight.Native.rc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 6,5,1,0
PRODUCTVERSION 6,5,1,0
FILEVERSION 6,5,2,0
PRODUCTVERSION 6,5,2,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -61,12 +61,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Martin Kuschnik"
VALUE "FileDescription", "The native part of the WmiLight lib."
VALUE "FileVersion", "6.5.1.0"
VALUE "FileVersion", "6.5.2.0"
VALUE "InternalName", "WmiLight.Native"
VALUE "LegalCopyright", "Copyright 2024 Martin Kuschnik"
VALUE "OriginalFilename", "WmiLight.Native.dll"
VALUE "ProductName", "WmiLight"
VALUE "ProductVersion", "6.5.1.0"
VALUE "ProductVersion", "6.5.2.0"
END
END
BLOCK "VarFileInfo"
Expand Down
26 changes: 0 additions & 26 deletions WmiLight.Native/WmiLightNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,7 @@ extern "C" { // only need to export C interface if
return CoCreateInstance(CLSID_UnsecuredApartment, nullptr, CLSCTX_LOCAL_SERVER, IID_IUnsecuredApartment, reinterpret_cast<void**>(unsecuredApartment));
}

__declspec(dllexport) HRESULT ReleaseIUnknown(IUnknown* pIUnknown)
{
if (pIUnknown == nullptr)
return E_POINTER;

pIUnknown->Release();

return S_OK;
}

__declspec(dllexport) HRESULT AddRef(IUnknown* pIUnknown)
{
if (pIUnknown == nullptr)
return E_POINTER;

pIUnknown->AddRef();

return S_OK;
}

__declspec(dllexport) HRESULT QueryInterface(IUnknown* pIUnknown, REFIID riid, void** ppvObject)
{
if (pIUnknown == nullptr)
return E_POINTER;

return pIUnknown->QueryInterface(riid, ppvObject);
}

__declspec(dllexport) HRESULT ConnectServer(
IWbemLocator* wbemLocator,
Expand Down
9 changes: 0 additions & 9 deletions WmiLight/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ IntPtr pObjParam
[DllImport(NATIVE_DLL_NAME)]
public static extern HResult CreateWbemUnsecuredApartment(out IntPtr pUnsecuredApartment);

[DllImport(NATIVE_DLL_NAME)]
public static extern HResult ReleaseIUnknown(IntPtr pIUnknown);

[DllImport(NATIVE_DLL_NAME)]
public static extern HResult AddRef(IntPtr pIUnknown);

[DllImport(NATIVE_DLL_NAME)]
public static extern HResult QueryInterface(IntPtr pIUnknown, Guid riid, out IntPtr ppvObject);

[DllImport(NATIVE_DLL_NAME)]
public static extern HResult ConnectServer(
IntPtr pWbemLocator,
Expand Down
8 changes: 5 additions & 3 deletions WmiLight/Wbem/IUnknown.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

namespace WmiLight.Wbem
{
Expand All @@ -16,8 +17,8 @@ internal IUnknown(IntPtr nativePointer)

~IUnknown()
{
// do not throw any exception in destructor
NativeMethods.ReleaseIUnknown(this.nativePointer);
if (this.nativePointer != IntPtr.Zero)
Marshal.Release(this.nativePointer);
}

public static implicit operator IntPtr(IUnknown iUnknown) => iUnknown.nativePointer;
Expand All @@ -31,7 +32,8 @@ public void Dispose()
{
disposed = true;

NativeMethods.ReleaseIUnknown(this.nativePointer);
if (this.nativePointer != IntPtr.Zero)
Marshal.Release(this.nativePointer);

GC.SuppressFinalize(this);
}
Expand Down
4 changes: 3 additions & 1 deletion WmiLight/Wbem/WbemClassObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ private static object VariantToObject(ref VARIANT value, CimType type)

case CimType.Object:

HResult hResult = NativeMethods.QueryInterface(value.Object, InterfaceIdentifier.IWbemClassObject, out IntPtr pWbemObject);
Guid iid = InterfaceIdentifier.IWbemClassObject;

HResult hResult = Marshal.QueryInterface(value.Object, ref iid, out IntPtr pWbemObject);

if (hResult.Failed)
throw (Exception)hResult;
Expand Down
2 changes: 1 addition & 1 deletion WmiLight/Wbem/WbemObjectSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal static HResult Indicate(IntPtr pEventSink, int lObjectCount, IntPtr[] a
// in accordance with COM rules. If the objects are only used for the duration of the Indicate call,
// then you do not need to call AddRef on each object pointer.

NativeMethods.AddRef(pWbemClassObject);
Marshal.AddRef(pWbemClassObject);

objects[i] = new WbemClassObject(pWbemClassObject);
}
Expand Down
2 changes: 1 addition & 1 deletion WmiLight/WmiLight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</Target>
<PropertyGroup />
<PropertyGroup>
<Version>6.5.1</Version>
<Version>6.5.2</Version>
<PackageId>WmiLight</PackageId>
<Authors>Martin Kuschnik</Authors>
<Company>Martin Kuschnik</Company>
Expand Down

0 comments on commit 8549f23

Please sign in to comment.