Skip to content

Commit

Permalink
45 createinparameters setpropertyvalue wont work without using wmimet…
Browse files Browse the repository at this point in the history
…hod setmethod = objgetmethodmethod name 1 (#47)

* Adjusted ExecuteMethod to support WMI methods that do not return a value.

* Added PutInstance to native lib

* implemented put method to commit changes made to WMI instances

* increased version to 6.10.0
  • Loading branch information
MartinKuschnik authored Jan 2, 2025
1 parent d5b21a2 commit 8f5e046
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 135 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,9,1,0
PRODUCTVERSION 6,9,1,0
FILEVERSION 6,10,0,0
PRODUCTVERSION 6,10,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -70,12 +70,12 @@ BEGIN
VALUE "FileDescription", "The native part of the WmiLight lib."
#endif

VALUE "FileVersion", "6.9.1.0"
VALUE "FileVersion", "6.10.0.0"
VALUE "InternalName", "WmiLight.Native"
VALUE "LegalCopyright", "Copyright 2024 Martin Kuschnik"
VALUE "OriginalFilename", "WmiLight.Native.dll"
VALUE "ProductName", "WmiLight"
VALUE "ProductVersion", "6.9.1.0"
VALUE "ProductVersion", "6.10.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
8 changes: 8 additions & 0 deletions WmiLight.Native/WmiLightNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ extern "C" { // only need to export C interface if
return pClassObject->Put(wszName, 0, pVal, type);
}

__declspec(dllexport) HRESULT _stdcall PutInstance(IWbemServices* wbemServices, IWbemClassObject* pInst, IWbemContext* ctx)
{
if (wbemServices == nullptr)
return E_POINTER;

return wbemServices->PutInstance(pInst, WBEM_FLAG_CREATE_OR_UPDATE, ctx, nullptr);
}

__declspec(dllexport) HRESULT _stdcall ExecMethod(
IWbemServices* wbemServices,
wchar_t* classNameOrPath,
Expand Down
3 changes: 3 additions & 0 deletions WmiLight/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public static extern HResult Get(

[DllImport(NATIVE_DLL_NAME, CallingConvention = CallingConvention.StdCall)]
public static extern HResult Put(IntPtr pClassObject, [MarshalAs(UnmanagedType.LPWStr)] string wszName, ref VARIANT pvar, CimType cimType);

[DllImport(NATIVE_DLL_NAME, CallingConvention = CallingConvention.StdCall)]
public static extern HResult PutInstance(IntPtr pWbemServices, IntPtr pInst, IntPtr ctx);

[DllImport(NATIVE_DLL_NAME, CallingConvention = CallingConvention.StdCall)]
public static extern HResult GetType(IntPtr pClassObject, [MarshalAs(UnmanagedType.LPWStr)] string propertyName, out CimType cimType);
Expand Down
11 changes: 11 additions & 0 deletions WmiLight/Wbem/WbemServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ internal WbemClassObjectEnumerator ExecQuery(string query, WbemClassObjectEnumer
return new WbemClassObjectEnumerator(pEnumerator);
}

internal void PutInstance(WbemClassObject wbemClassObject, IntPtr ctx)
{
if (this.Disposed)
throw new ObjectDisposedException(nameof(WbemServices));

HResult hResult = NativeMethods.PutInstance(this, wbemClassObject, ctx);

if (hResult.Failed)
throw (Exception)hResult;
}

internal void ExecNotificationQueryAsync(string query, IntPtr ctx, WbemObjectSink sink)
{
if (this.Disposed)
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.9.1</Version>
<Version>6.10.0</Version>
<PackageId>WmiLight</PackageId>
<Authors>Martin Kuschnik</Authors>
<Company>Martin Kuschnik</Company>
Expand Down
133 changes: 3 additions & 130 deletions WmiLight/WmiMethodParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using WmiLight.Wbem;

namespace WmiLight
Expand All @@ -21,136 +20,10 @@ internal WmiMethodParameters(WbemServices wbemServices, WbemClassObject signatur
this.signatur = signatur ?? throw new ArgumentNullException(nameof(signatur));
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, sbyte propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, byte propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, short propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, ushort propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, int propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, uint propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, long propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, ulong propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, bool propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, string propertyValue)
{
this.signatur.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, string[] propertyValue)
/// <inheritdoc />
public override void Put()
{
this.signatur.Put(propertyName, propertyValue);
throw new NotSupportedException();
}

#region Description
Expand Down
144 changes: 144 additions & 0 deletions WmiLight/WmiObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,150 @@ public TResult GetPropertyValue<TResult>(string propertyName)
return wbemClassObject.Get<TResult>(propertyName);
}



#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, sbyte propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, byte propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, short propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, ushort propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, int propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, uint propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, long propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, ulong propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, bool propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, string propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <param name="propertyName">The name of the property that should be changed.</param>
/// <param name="propertyValue">The new property value.</param>
#endregion
public void SetPropertyValue(string propertyName, string[] propertyValue)
{
this.wbemClassObject.Put(propertyName, propertyValue);
}

#region Description
/// <summary>
/// Commits the changes to the object.
/// </summary>
#endregion
public virtual void Put()
{
this.wbemServices.PutInstance(this.wbemClassObject, IntPtr.Zero);
}

#region Description
/// <summary>
/// Executes a static WMI method.
Expand Down

0 comments on commit 8f5e046

Please sign in to comment.