You are here
Home > Programming > Exception Thrown By The Target Of An Invocation – Resolved… Sort Of

Exception Thrown By The Target Of An Invocation – Resolved… Sort Of

Exception Thrown By Target Of Invocation

The other day I truly had a dumbstruck, absolutely floored, speechless moment. I tried to run SQL Server Management Studio and was presented with an error telling me that an exception has been thrown by the target of  an invocation. Well that error message was about as helpful as the first time I ever saw the Object reference not set to an instance of an object error. And as I stared blankly at my monitor, I thought….. ‘I have nothing’.

Exception Thrown By The Target Of An Invocation

Pro Tip: to skip all the details  and go straight to the solution, see the ‘Take Away – The Gist’ section at the end of this article.

In retrospect, the error message is quite descriptive. The target of what I am trying to invoke threw an error. So I tried starting Visual Studio and saw the same error. Again, there was that moment… nothing.

Exception Thrown

Thank goodness that I tried running SYSPRO 7 after that. When I tried to log on to the company, I got an error telling me that it could not establish a SQL connection because it could not find a specific DLL at the specific file path. And that my dear friends, is what got me thinking. Why can’t SYSPRO find that DLL?

Exception Thrown – A Moment Of Clarity

So this got me thinking, if the DLL file was there (I checked) why could Windows not find it? I could also not run Notepad or Remote Desktop Connection and all their icons were missing and showing as unrecognized files.

Exception Thrown

Exception Thrown

I then decided to go and look at my Environment Variables because I saw that the shortcut to the applications made use of the environment variables. So in the Control Panel under System, I clicked on ‘Advanced system settings’ and Windows 7 displayed this gem of a message to me. Refer to the Gandalf gif above.

Exception Thrown

Ok, so I had to go the long way round to view the Environment Variables. Click on SystemPropertiesAdvanced.exe in the System32 sub-folder in Windows.

Exception Thrown

When System Properties open up, click on the Environment Variables button to view and edit these values.

Exception Thrown

First thing I checked was windir which seemed fine.

Exception Thrown

Then I noticed the Path variable. The value of this was insanely long. I was so dumbfounded, I checked this in the registry editor to be sure.

Exception Thrown

So in HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment you will see the Path value. To give you an idea of what this variable contained, look here:

That is a total of 2473 characters. So I Googled the problem and it seems as though it is a common issue. It seems as though as soon as the Path variable exceeds 2048 characters, other variables such as windir stop working. What I did was delete many of the entries from this Path variable (you will notice, the paths are separated by a semicolon), and after a reboot, everything started working fine again.

Take Away – The Gist

If you are seeing these errors, do the following:

  • Run the following program – c:windowssystem32systempropertiesadvanced.exe
  • Check the length of the Path variable by pasting it into a word document and check the character count
  • Remove some of the entries until the length is under 2048 characters
  • Reboot your PC

I am not sure if there are any other causes for the errors described above, but on my Windows 7 machine, this is what solved it. After I fixed this, my applications (Visual Studio, SQL Server and SYSPRO 7) immediately started working and my Notepad, Remote Desktop Connection and icons all returned after a reboot.

Exception Thrown

Image Reference

Dirk Strauss
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.

Similar Articles

  • ryan

    Excellent, thanks!

  • Colin

    Thanks

  • Simon Timms

    This was exactly my problem, thanks. In my case it was after installing the visual studio 2015 CTP that the problem manifested itself.

    • Dirk Strauss

      Thanks for the feedback Simon 🙂

  • ardanew

    thanks, solved my problem too!

  • Blue_Peter

    Awesome and thanks. Solved my issue.

    • Dirk Strauss

      Fantastic! Anything to help a fellow Stormers fan! 🙂

  • Matt Thomas

    Thanks Dirk… this is a great write-up and description of the problem. I knew it was environment variable related but would have never thought the PATH variable exceeded a limit.

  • Preetam

    Thanks a lot for the detailed explanation ! Helped me a lot

  • Attila

    Thanks a lot !!! It was exactly what I needed!! :-)))

  • splattne

    Thank you for solving the issue for me. Incredibly stupid bug though.

  • thanks a lot, you really saved my time. i adjust the registry path and it worked for me.

  • Jim

    Thank you my fellow South African 😀

    • You’re welcome Jim! Have a lekker weekend 🙂

  • Todd

    Fixed it for me. Huge thanks!

  • Chris

    Add another thanks from me.

  • sue

    Thank you so very much for taking the time to post this – I would have floundered for hours I am sure if I had not stumbled upon your solution. Mille grazie!

  • Tony Bolton

    And a thanks from me!

    • You’re very welcome Tony! Thanks for reading the blog 🙂

  • Ernest S Kirubakaran

    Saved my day. Thank you so much!

  • PuckRockGrrl

    Thank you for this — saved me lots of frustration! I removed one item from Path, restarted, and everything seems to be working again. Yay! 🙂

  • gigi paul

    Thanks a lot. Clean precise instructions, saved my day!
    Gigi

  • Dyrus

    Hmm didn’t work for me (path is only like 327 characters long), but thanks for the info!

Top