Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ using (WmiConnection con = new WmiConnection())
}
```

Query all partitions for a remote machine with credentials:
Query all partitions for a remote machine with credentials (UPN format recommended):
```C#

var opt = new WmiConnectionOptions() { EnablePackageEncryption = true };
var cred = new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
var cred = new NetworkCredential("USER@DOMAIN", "PASSWORD");

using (WmiConnection con = new WmiConnection(@"\\MACHINENAME\root\cimv2", cred, opt))
{
Expand Down
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 7,0,0,0
PRODUCTVERSION 7,0,0,0
FILEVERSION 7,1,0,0
PRODUCTVERSION 7,1,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", "7.0.0.0"
VALUE "FileVersion", "7.1.0.0"
VALUE "InternalName", "WmiLight.Native"
VALUE "LegalCopyright", "Copyright 2026 Martin Kuschnik"
VALUE "OriginalFilename", "WmiLight.Native.dll"
VALUE "ProductName", "WmiLight"
VALUE "ProductVersion", "7.0.0.0"
VALUE "ProductVersion", "7.1.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
13 changes: 6 additions & 7 deletions WmiLight.Native/WmiLightNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,18 @@ extern "C" { // only need to export C interface if
);
}


__declspec(dllexport) HRESULT _stdcall SetProxy(
IUnknown* pIUnknown,
wchar_t* username,
wchar_t* password,
wchar_t* authority,
wchar_t* domain,
ImpersonationLevel impersonationLevel,
AuthenticationLevel authenticationLevel)
{
if (pIUnknown == nullptr)
return E_POINTER;

if (username == nullptr && password == nullptr && authority == nullptr)
if (username == nullptr && password == nullptr && domain == nullptr)
{
return CoSetProxyBlanket(
pIUnknown,
Expand All @@ -116,9 +115,9 @@ extern "C" { // only need to export C interface if

authInfo.User = (unsigned short*)username;
authInfo.UserLength = username == nullptr ? 0 : static_cast<unsigned long>(wcslen(username));

authInfo.Domain = (unsigned short*)authority;
authInfo.DomainLength = authority == nullptr ? 0 : static_cast<unsigned long>(wcslen(authority));
authInfo.Domain = (unsigned short*)domain;
authInfo.DomainLength = domain == nullptr ? 0 : static_cast<unsigned long>(wcslen(domain));

authInfo.Password = (unsigned short*)password;
authInfo.PasswordLength = password == nullptr ? 0 : static_cast<unsigned long>(wcslen(password));
Expand All @@ -129,7 +128,7 @@ extern "C" { // only need to export C interface if
pIUnknown,
RPC_C_AUTHN_DEFAULT,
RPC_C_AUTHZ_DEFAULT,
nullptr,
COLE_DEFAULT_PRINCIPAL,
authenticationLevel,
impersonationLevel,
&authInfo,
Expand Down
5 changes: 5 additions & 0 deletions WmiLight/Exceptions/InvalidMethodException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ internal InvalidMethodException(string method, WbemStatus wbemStatus)

#endregion

#region Description
/// <summary>
/// Gets the name of the invalid Method.
/// </summary>
#endregion
public string Method { get; }
}
}
2 changes: 1 addition & 1 deletion WmiLight/Exceptions/LocalCredentialsException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
#region Description
/// <summary>
/// The Exception for the case that the Username, password, or authority are specified for a local <see cref="WmiLight.WmiConnection"/>.
/// The Exception for the case that the Username, password, or domain are specified for a local <see cref="WmiLight.WmiConnection"/>.
/// </summary>
#endregion
public class LocalCredentialsException : WmiException
Expand Down
2 changes: 1 addition & 1 deletion WmiLight/Exceptions/TransportFailureException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
#region Description
/// <summary>
/// The Exception for the case that the Username, password, or authority are specified for a local <see cref="WmiLight.WmiConnection"/>.
/// The Exception that is thrown when the WMI service returns WBEM_E_TRANSPORT_FAILURE.
/// </summary>
#endregion
public class TransportFailureException : WmiException
Expand Down
2 changes: 1 addition & 1 deletion WmiLight/Internal/HResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static implicit operator Exception(HResult hr)
return new TransportFailureException(new HResultInfo(hr, "The remote procedure call (RPC) link between the current process and WMI failed.", WbemStatus.WBEM_E_TRANSPORT_FAILURE.ToString()));

case (int)WbemStatus.WBEM_E_LOCAL_CREDENTIALS:
return new LocalCredentialsException(new HResultInfo(hr, "Username, password, or authority can only used on a remote connection.", WbemStatus.WBEM_E_LOCAL_CREDENTIALS.ToString()));
return new LocalCredentialsException(new HResultInfo(hr, "Username, password, or domain can only used on a remote connection.", WbemStatus.WBEM_E_LOCAL_CREDENTIALS.ToString()));

case (int)WbemStatus.WBEM_E_FAILED:
return new WmiException(new HResultInfo(hr, "An unspecified error occurred.", WbemStatus.WBEM_E_FAILED.ToString()));
Expand Down
10 changes: 5 additions & 5 deletions WmiLight/Internal/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static extern HResult SetProxy(
[MarshalAs(UnmanagedType.LPWStr)]
string password,
[MarshalAs(UnmanagedType.LPWStr)]
string authority,
string domain,
ImpersonationLevel impersonationLevel,
AuthenticationLevel authenticationLevel);

Expand Down Expand Up @@ -197,7 +197,7 @@ public static extern HResult SetProxy(
[MarshalAs(UnmanagedType.LPWStr)]
string password,
[MarshalAs(UnmanagedType.LPWStr)]
string authority,
string domain,
ImpersonationLevel impersonationLevel,
AuthenticationLevel authenticationLevel);

Expand Down Expand Up @@ -314,14 +314,14 @@ public static HResult SetProxy(
IntPtr pIUnknown,
string username,
string password,
string authority,
string domain,
ImpersonationLevel impersonationLevel,
AuthenticationLevel authenticationLevel)
{
if (Environment.Is64BitProcess)
return x64.SetProxy(pIUnknown, username, password, authority, impersonationLevel, authenticationLevel);
return x64.SetProxy(pIUnknown, username, password, domain, impersonationLevel, authenticationLevel);
else
return x86.SetProxy(pIUnknown, username, password, authority, impersonationLevel, authenticationLevel);
return x86.SetProxy(pIUnknown, username, password, domain, impersonationLevel, authenticationLevel);
}
public static HResult ExecQuery(
IntPtr pWbemServices,
Expand Down
2 changes: 1 addition & 1 deletion WmiLight/Internal/NativeMethods.netcore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static partial HResult SetProxy(
[MarshalAs(UnmanagedType.LPWStr)]
string password,
[MarshalAs(UnmanagedType.LPWStr)]
string authority,
string domain,
ImpersonationLevel impersonationLevel,
AuthenticationLevel authenticationLevel);

Expand Down
67 changes: 67 additions & 0 deletions WmiLight/Internal/NormalizedCredential.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace WmiLight
{
using System;
using System.Diagnostics;
using System.Net;

internal class NormalizedCredential
{
internal NormalizedCredential(NetworkCredential networkCredential)
{
if (networkCredential.UserName != null)
{
string[] usernameParts = networkCredential.UserName.Split('\\');

if (usernameParts.Length == 2)
{
this.UserNameWithoutDomain = usernameParts[1];
this.UserNameWithDomain = networkCredential.UserName;

if (string.IsNullOrEmpty(networkCredential.Domain))
this.Domain = usernameParts[0];
else
this.Domain = networkCredential.Domain;
}
else
{
usernameParts = networkCredential.UserName.Split('@');

if (usernameParts.Length == 2)
{
this.UserNameWithoutDomain = usernameParts[0];
this.UserNameWithDomain = networkCredential.UserName;

if (string.IsNullOrEmpty(networkCredential.Domain) || string.Equals(networkCredential.Domain, usernameParts[1], StringComparison.OrdinalIgnoreCase))
this.Domain = usernameParts[1];
else
this.Domain = networkCredential.Domain;

}
else
{
this.UserNameWithoutDomain = networkCredential.UserName;
this.UserNameWithDomain = string.IsNullOrEmpty(networkCredential.Domain) ? networkCredential.UserName : $"{networkCredential.Domain}\\{networkCredential.UserName}";
this.Domain = networkCredential.Domain;
}
}
}
else
{
this.UserNameWithoutDomain = null;
this.UserNameWithDomain = null;
this.Domain = networkCredential.Domain;
}

this.Password = networkCredential.Password;
}

internal string UserNameWithoutDomain { get; }

internal string UserNameWithDomain { get; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal string Password { get; }

internal string Domain { get; }
}
}
2 changes: 1 addition & 1 deletion WmiLight/Wbem/Enumerations/WbemStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal enum WbemStatus : int

#region Description
/// <summary>
/// The user specified a username, password, or authority on a
/// The user specified a username, password, or domain on a
/// local connection. The user must use an empty user name and password and rely on
/// default security.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions WmiLight/Wbem/WbemServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ internal void SetProxy(ImpersonationLevel impersonate, AuthenticationLevel authL
throw (Exception)hResult;
}

internal void SetProxy(string userName, string password, string authority, ImpersonationLevel impersonate, AuthenticationLevel authLevel)
internal void SetProxy(string userName, string password, string domain, ImpersonationLevel impersonate, AuthenticationLevel authLevel)
{
if (this.Disposed)
throw new ObjectDisposedException(nameof(WbemServices));

HResult hResult = NativeMethods.SetProxy(this, userName, password, authority, impersonate, authLevel);
HResult hResult = NativeMethods.SetProxy(this, userName, password, domain, impersonate, authLevel);

if (hResult.Failed)
throw (Exception)hResult;
Expand Down Expand Up @@ -61,7 +61,7 @@ internal WbemClassObjectEnumerator ExecQuery(string query, WbemClassObjectEnumer
return new WbemClassObjectEnumerator(pEnumerator);
}

internal WbemClassObjectEnumerator ExecQuery(string query, WbemClassObjectEnumeratorBehaviorOption behaviorOption, IntPtr ctx, string userName, string password, string authority, AuthenticationLevel authLevel, ImpersonationLevel impersonate)
internal WbemClassObjectEnumerator ExecQuery(string query, WbemClassObjectEnumeratorBehaviorOption behaviorOption, IntPtr ctx, string userName, string password, string domain, AuthenticationLevel authLevel, ImpersonationLevel impersonate)
{
if (this.Disposed)
throw new ObjectDisposedException(nameof(WbemServices));
Expand All @@ -82,7 +82,7 @@ internal WbemClassObjectEnumerator ExecQuery(string query, WbemClassObjectEnumer
}
}

hResult = NativeMethods.SetProxy(pEnumerator, userName, password, authority, impersonate, authLevel);
hResult = NativeMethods.SetProxy(pEnumerator, userName, password, domain, impersonate, authLevel);

if (hResult.Failed)
throw (Exception)hResult;
Expand Down
Loading