I want to display all modules loaded into a specified process. I used this code and it works:
Process myProcess = new Process();
myProcess.StartInfo.FileName = "";
myProcess.StartInfo.WorkingDirectory = "";
myProcess.Start();
ProcessModule myProcessModule;
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
for (int i = 0; i < myProcessModuleCollection.Count; i++)
{
myProcessModule = myProcessModuleCollection[i];
MessageBox.Show(myProcessModule.ModuleName);
}
But I want to display the modules after a button click, so I did this:
public Process myProcess;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "";
myProcess.StartInfo.WorkingDirectory = "";
myProcess.Start();
}
private void button2_Click(object sender, EventArgs e)
{
ProcessModule myProcessModule;
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
for (int i = 0; i < myProcessModuleCollection.Count; i++)
{
myProcessModule = myProcessModuleCollection[i];
MessageBox.Show(myProcessModule.ModuleName);
}
}
But it throws me an error "Object reference not set to an instance of an object" for this line:
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
I searched for an answer on the internet, tryed everything what could I think of, but it just won't work. I also searched for the error I get and I found out that the program probably thinks that the myProcess.Modules is null, but that doesn't help me either.