IndexOutOfRangeException Problem!

Page 1 of 1

7 Replies - 3608 Views - Last Post: 28 December 2012 - 08:58 AM Rate Topic: -----

#1 krazyshank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-December 12

IndexOutOfRangeException Problem!

Posted 26 December 2012 - 10:35 PM

So i'm writing a DLL injector using a DLL injection library I found. It works well except for this:

Heres all of the injection code:
//inject dll here
            var injector = InjectionMethod.Create(InjectionMethodType.ManualMap);
            var processId = Process.GetProcessesByName(proc)[0].Id;
            if (processId == 0)
            {
                info.Text = "Failed to find game. Closing.";
                Thread.Sleep(2000);
                this.Close();
            }
            var hModule = IntPtr.Zero;
            using (var img = new PortableExecutable(loc))
                hModule = injector.Inject(img, processId);
            Thread.Sleep(1000);
            countdown.Enabled = true;



When I run my program, and I click the button that activates this code, it crashes and I get an unhandled exception (IndexOutOfRangeException) for this line:
Process.GetProcessesByName(proc)[0].Id;



I cant seem to get around this! What is causing it?
The process IS running.

Is This A Good Question/Topic? 0
  • +

Replies To: IndexOutOfRangeException Problem!

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: IndexOutOfRangeException Problem!

Posted 26 December 2012 - 10:48 PM

You aren't getting anything back from GetProcessesByName() which means your 'proc' variable has the wrong value in it.

This post has been edited by Momerath: 26 December 2012 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 krazyshank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-December 12

Re: IndexOutOfRangeException Problem!

Posted 26 December 2012 - 10:58 PM

say I want to inject into explorer.exe, would I set proc as "explorer" or "explorer.exe"?
And is it case sensitive?
Was This Post Helpful? 0
  • +
  • -

#4 krazyshank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-December 12

Re: IndexOutOfRangeException Problem!

Posted 26 December 2012 - 11:25 PM

OH i got it to work and fully inject!!
One question, I dont know how to handle errors soo,
how could I catch that error and then write some code if it happens?
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: IndexOutOfRangeException Problem!

Posted 27 December 2012 - 12:12 AM

The best way would be to check for null first:
    //inject dll here
    var injector = InjectionMethod.Create(InjectionMethodType.ManualMap);
    var processes = Process.GetProcessesByName(proc);
    if (processes != null && processes.Length > 0) {
        var processId = processes[0].Id;
        if (processId == 0) {
            info.Text = "Failed to find game. Closing.";
            ... etc ...


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3467
  • View blog
  • Posts: 10,687
  • Joined: 05-May 12

Re: IndexOutOfRangeException Problem!

Posted 27 December 2012 - 10:31 AM

Why are you injecting a DLL into Explorer.exe? Shouldn't you be registering a shell extension instead? Or are you trying to get around some security measures that are in place?
Was This Post Helpful? 0
  • +
  • -

#7 krazyshank  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 21-December 12

Re: IndexOutOfRangeException Problem!

Posted 27 December 2012 - 03:14 PM

View PostSkydiver, on 27 December 2012 - 10:31 AM, said:

Why are you injecting a DLL into Explorer.exe? Shouldn't you be registering a shell extension instead? Or are you trying to get around some security measures that are in place?

Im not injecting into explorer, I said for just an example. Im actually injecting into games :)
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4432
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: IndexOutOfRangeException Problem!

Posted 28 December 2012 - 08:58 AM

You're probably making a trainer or a cheat engine. That's fine, but we don't assist with that kind of work here because of the potential for malicious use. It's too easy to take that code and turn it into something worse. Please don't open new topics on this subject.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1