C
C#2y ago
maria 🌟

✅ wtf i cant import system management

i cant import it
47 Replies
maria 🌟
maria 🌟2y ago
i usualy added automatcly or if not i added like thath but now it doenst work maybie its thath it was a feature of net framework?
maria 🌟
maria 🌟2y ago
@🌈 Thinker 🌈 i invoke you
Thinker
Thinker2y ago
no idea what System.Management is
maria 🌟
maria 🌟2y ago
its built in on net but i dont now why i cant import it its like the wmic of windows
Thinker
Thinker2y ago
Looking at the docs for most of the types in that namespace, none of them list versions past .NET Framework 4.8.1 So in all likelihood it's not part of .NET Core / modern versions of .NET.
maria 🌟
maria 🌟2y ago
😭
Thinker
Thinker2y ago
What are you aiming to do exactly? There's probably some other better library for it.
maria 🌟
maria 🌟2y ago
private static Process GetParentProcessId()
{
var currentProcess = Process.GetCurrentProcess();
int parentProcessId = -1;
string queryText = string.Format("select parentprocessid from win32_process where processid = {0}", currentProcess.Id);
using (var searcher = new ManagementObjectSearcher(queryText))
{
foreach (var obj in searcher.Get())
{
parentProcessId = Convert.ToInt32(obj["parentprocessid"]);
break;
}
}

return Process.GetProcessById(parentProcessId);
}
private static Process GetParentProcessId()
{
var currentProcess = Process.GetCurrentProcess();
int parentProcessId = -1;
string queryText = string.Format("select parentprocessid from win32_process where processid = {0}", currentProcess.Id);
using (var searcher = new ManagementObjectSearcher(queryText))
{
foreach (var obj in searcher.Get())
{
parentProcessId = Convert.ToInt32(obj["parentprocessid"]);
break;
}
}

return Process.GetProcessById(parentProcessId);
}
Thinker
Thinker2y ago
... what is this why is there Convert.ToInt32 here why obj["parentprocessid"] why no string interpolation
DaVinki
DaVinki2y ago
Just use NuGet to get System.Management
Thinker
Thinker2y ago
System.Management 8.0.0-preview.2.23128.3
Provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure. Commonly Used Types: System.Management.ManagementClass System.Management.ManagementObject System.Management.SelectQuery
maria 🌟
maria 🌟2y ago
private static Process GetParentProcessId()
{
int parentProcessId = int.MinValue;

using (var searcher = new ManagementObjectSearcher(string.Format("select parentprocessid from win32_process where processid = {0}", Process.GetCurrentProcess().Id)))
{
foreach (var obj in searcher.Get())
{
parentProcessId = (int)obj["parentprocessid"];
break;
}
}

return Process.GetProcessById(parentProcessId);
}
private static Process GetParentProcessId()
{
int parentProcessId = int.MinValue;

using (var searcher = new ManagementObjectSearcher(string.Format("select parentprocessid from win32_process where processid = {0}", Process.GetCurrentProcess().Id)))
{
foreach (var obj in searcher.Get())
{
parentProcessId = (int)obj["parentprocessid"];
break;
}
}

return Process.GetProcessById(parentProcessId);
}
better now? ummm
Thinker
Thinker2y ago
What does obj[] return
maria 🌟
maria 🌟2y ago
Unhandled exception. System.PlatformNotSupportedException: System.Management currently is only supported for Windows desktop applications.
at System.Management.ManagementObjectSearcher..ctor(String queryString)
at SimpleBatchObfuscator.Program.GetParentProcessId() in C:\Users\Mrgaton\OneDrive\Programas\Programas Oscuros que cre en csharp\SimpleBatchObfuscator\SimpleBatchObfuscator\Program.cs:line 16
at SimpleBatchObfuscator.Program.Main(String[] args) in C:\Users\Mrgaton\OneDrive\Programas\Programas Oscuros que cre en csharp\SimpleBatchObfuscator\SimpleBatchObfuscator\Program.cs:line 34
Unhandled exception. System.PlatformNotSupportedException: System.Management currently is only supported for Windows desktop applications.
at System.Management.ManagementObjectSearcher..ctor(String queryString)
at SimpleBatchObfuscator.Program.GetParentProcessId() in C:\Users\Mrgaton\OneDrive\Programas\Programas Oscuros que cre en csharp\SimpleBatchObfuscator\SimpleBatchObfuscator\Program.cs:line 16
at SimpleBatchObfuscator.Program.Main(String[] args) in C:\Users\Mrgaton\OneDrive\Programas\Programas Oscuros que cre en csharp\SimpleBatchObfuscator\SimpleBatchObfuscator\Program.cs:line 34
numbers
Thinker
Thinker2y ago
right, then you don't need the (int)
maria 🌟
maria 🌟2y ago
i dont really now but halp
Thinker
Thinker2y ago
Well, apparently it's not supported :/
maria 🌟
maria 🌟2y ago
WTF im on windows
maria 🌟
maria 🌟2y ago
Thinker
Thinker2y ago
ah yes, object I honestly have no idea, sorry
maria 🌟
maria 🌟2y ago
another thing that i prefere framework
Thinker
Thinker2y ago
it's fine to be wrong You should never ever use Framework for new projects. The only reason Framework is required here is because this library System.Management is very likely very very old.
maria 🌟
maria 🌟2y ago
bruh its just esayer on framework
Thinker
Thinker2y ago
what is easier? it's just this specific library
maria 🌟
maria 🌟2y ago
yes it just querys on windows components and a lot of things more
Thinker
Thinker2y ago
like what
maria 🌟
maria 🌟2y ago
one files not exe on cpp and the dll with the actual data of proyect
Thinker
Thinker2y ago
wdym?
maria 🌟
maria 🌟2y ago
? wdym??
Thinker
Thinker2y ago
just not sure what you meant by that
maria 🌟
maria 🌟2y ago
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.UInt32' to type 'System.Int32'. at SimpleBatchObfuscator.Program.GetParentProcessId() in C:\Users\Mrgaton\OneDrive\Programas\Programas Oscuros que cre en csharp\SimpleBatchObfuscator\SimpleBatchObfuscator\Program.cs:line 20 at SimpleBatchObfuscator.Program.Main(String[] args) in C:\Users\Mrgaton\OneDrive\Programas\Programas Oscuros que cre en csharp\SimpleBatchObfuscator\SimpleBatchObfuscator\Program.cs:line 34
Aaron
Aaron2y ago
uint not int
maria 🌟
maria 🌟2y ago
GetProcessById accepts only int but whats difference in int and uint
Aaron
Aaron2y ago
why do you even want the parent process?
maria 🌟
maria 🌟2y ago
yes
Aaron
Aaron2y ago
(also you just just do (int)(uint))
maria 🌟
maria 🌟2y ago
i want to know if started from the explorer like if called from user or from automated application
Thinker
Thinker2y ago
int is signed (can be negative), uint is unsigned (cannot be negative) why do you need to know this?
maria 🌟
maria 🌟2y ago
thanks yes
Thinker
Thinker2y ago
that's not an answer
Aaron
Aaron2y ago
checking if called from explorer is not a good way to do that a user can use a command line too, after all
Matt
Matt2y ago
"Programas Oscuros que cre en csharp".... are you creating something malicious?
Matt
Matt2y ago
maria 🌟
maria 🌟2y ago
its just my funny folders names but believe what you want
SG97
SG972y ago
if that folder is actually in a OneDrive folder that gets synced back and forth, there's almost bound to be problems
arion
arion2y ago
@Gato have u tried pinvoke libraries instead of WMI Queries? WMI is usually slow, I've used this one before:
internal static int GetParentProcessID_SnapshotIteration(int pid)
{
if (pid == 0)
return 0;

using var hSnapshot = Kernel32.CreateToolhelp32Snapshot(Kernel32.CreateToolhelp32SnapshotFlags.TH32CS_SNAPPROCESS, 0);
if (hSnapshot is { IsInvalid: true })
return 0;

var processes = Kernel32.Process32Enumerate(hSnapshot);
return processes.First(p => p.th32ProcessID == pid).th32ParentProcessID;
}
internal static int GetParentProcessID_SnapshotIteration(int pid)
{
if (pid == 0)
return 0;

using var hSnapshot = Kernel32.CreateToolhelp32Snapshot(Kernel32.CreateToolhelp32SnapshotFlags.TH32CS_SNAPPROCESS, 0);
if (hSnapshot is { IsInvalid: true })
return 0;

var processes = Kernel32.Process32Enumerate(hSnapshot);
return processes.First(p => p.th32ProcessID == pid).th32ParentProcessID;
}
it uses the standard PInvoke lib, not vanara's sadge
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server