C
C#16mo ago
maria 🌟

✅ wtf i cant import system management

i cant import it
47 Replies
maria 🌟
maria 🌟16mo 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 🌟16mo ago
@🌈 Thinker 🌈 i invoke you
Thinker
Thinker16mo ago
no idea what System.Management is
maria 🌟
maria 🌟16mo ago
its built in on net but i dont now why i cant import it its like the wmic of windows
Thinker
Thinker16mo 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 🌟16mo ago
😭
Thinker
Thinker16mo ago
What are you aiming to do exactly? There's probably some other better library for it.
maria 🌟
maria 🌟16mo 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
Thinker16mo ago
... what is this why is there Convert.ToInt32 here why obj["parentprocessid"] why no string interpolation
DaVinki
DaVinki16mo ago
Just use NuGet to get System.Management
Thinker
Thinker16mo 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 🌟16mo 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
Thinker16mo ago
What does obj[] return
maria 🌟
maria 🌟16mo 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
Thinker16mo ago
right, then you don't need the (int)
maria 🌟
maria 🌟16mo ago
i dont really now but halp
Thinker
Thinker16mo ago
Well, apparently it's not supported :/
maria 🌟
maria 🌟16mo ago
WTF im on windows
maria 🌟
maria 🌟16mo ago
Thinker
Thinker16mo ago
ah yes, object I honestly have no idea, sorry
maria 🌟
maria 🌟16mo ago
another thing that i prefere framework
Thinker
Thinker16mo 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 🌟16mo ago
bruh its just esayer on framework
Thinker
Thinker16mo ago
what is easier? it's just this specific library
maria 🌟
maria 🌟16mo ago
yes it just querys on windows components and a lot of things more
Thinker
Thinker16mo ago
like what
maria 🌟
maria 🌟16mo ago
one files not exe on cpp and the dll with the actual data of proyect
Thinker
Thinker16mo ago
wdym?
maria 🌟
maria 🌟16mo ago
? wdym??
Thinker
Thinker16mo ago
just not sure what you meant by that
maria 🌟
maria 🌟16mo 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
Aaron16mo ago
uint not int
maria 🌟
maria 🌟16mo ago
GetProcessById accepts only int but whats difference in int and uint
Aaron
Aaron16mo ago
why do you even want the parent process?
maria 🌟
maria 🌟16mo ago
yes
Aaron
Aaron16mo ago
(also you just just do (int)(uint))
maria 🌟
maria 🌟16mo ago
i want to know if started from the explorer like if called from user or from automated application
Thinker
Thinker16mo ago
int is signed (can be negative), uint is unsigned (cannot be negative) why do you need to know this?
maria 🌟
maria 🌟16mo ago
thanks yes
Thinker
Thinker16mo ago
that's not an answer
Aaron
Aaron16mo 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
Matt16mo ago
"Programas Oscuros que cre en csharp".... are you creating something malicious?
Matt
Matt16mo ago
maria 🌟
maria 🌟16mo ago
its just my funny folders names but believe what you want
SG97
SG9716mo ago
if that folder is actually in a OneDrive folder that gets synced back and forth, there's almost bound to be problems
arion
arion16mo 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
Accord16mo 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.