C
C#8mo ago
VerpinZal

Attempting to retrieve a Windows process name causes Access Violation Exception.

The code snippet below is supposed to retrieve the parent that launched my application and display it. It can be explorer or any other Process through which my application is launched. Actually, this code works. Problem is, while Release works, Debug can't get past the searchById line.
var processId = Process.GetCurrentProcess().Id;
var queryFromId = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", processId);
var searchById = new ManagementObjectSearcher("root\\CIMV2", queryFromId);
var idResults = searchById.Get().GetEnumerator();
idResults.MoveNext();
var queryObject = idResults.Current;
var parentId = (uint)queryObject["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);
string parentFinal = parent.ProcessName;
var processId = Process.GetCurrentProcess().Id;
var queryFromId = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", processId);
var searchById = new ManagementObjectSearcher("root\\CIMV2", queryFromId);
var idResults = searchById.Get().GetEnumerator();
idResults.MoveNext();
var queryObject = idResults.Current;
var parentId = (uint)queryObject["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);
string parentFinal = parent.ProcessName;
This used to work in debug too. I made some additions to my application's code (completely irrelevant as to the code above) and one day, suddenly, I can no longer debug my code. An image of the error is also attached. Someone, please save my sanity.
No description
10 Replies
Omnissiah
Omnissiah8mo ago
when you get to that point is searchById null or not?
VerpinZal
VerpinZalOP8mo ago
No description
VerpinZal
VerpinZalOP8mo ago
it's null
Omnissiah
Omnissiah8mo ago
and so then did you check processId?
VerpinZal
VerpinZalOP8mo ago
process id is not null
VerpinZal
VerpinZalOP8mo ago
No description
VerpinZal
VerpinZalOP8mo ago
checked task manager, processId is my app's id, trying to launch through debug
Omnissiah
Omnissiah8mo ago
so at this point i would use wmi explorer to look at the wmi data and process explorer to double check that there is a parent
VerpinZal
VerpinZalOP8mo ago
since it's debug, shouldn't it be devenv.exe?
Omnissiah
Omnissiah8mo ago
hmm i wouldn't be that sure, there are two command for launching a process, launch with debug and without attaching, and also maybe it differs with project type (like if you have a console or something else)
Want results from more Discord servers?
Add your server