none
InvalidProgramException: Common Language Runtime detected an invalid program RRS feed

  • Question

  • Hi All:

    "InvalidProgramException: Common Language Runtime detected an invalid program"

    We had a project using C# reflection heavily, and recently encountered above weird problem, the environment is .net framework 4.7.1 + vs2017 + windows 10 Enterprise, I tried to upgrade windows/vs2017 to latest version, also build with .net framework 4.7.2, neither works, instead it only works when I choose vs2013 + .net framework 4.5.2 (which is the setting of earlier project version)

    I spent whole day in google, and find out that most of the links refer to below address:

    https://support.microsoft.com/en-us/help/312544/you-may-receive-an-invalidprogramexception-error-message-when-you-run

    however the hotfix link returns 404 error, and the fix is for .net framework v2.0

    I found that VS2017 is using csc v2.8.0.62830, while VS2013 is using csc v12.0.40629.0, is there a patch for C# compiler?

    any reference would appreciated, thanks in advance

    kind regards


    fight for future!





    • Edited by Mulder2008 Monday, May 21, 2018 7:58 AM
    Monday, May 21, 2018 5:35 AM

Answers

  • Don't worry.

    after changing to using normal way instead of reflection, I managed to fix it.


    fight for future!

    • Marked as answer by Mulder2008 Tuesday, May 22, 2018 10:00 AM
    Tuesday, May 22, 2018 10:00 AM

All replies

  • When the crash occurs you'll get a crash dump. Load that up into the debugger and determine what line(s) of code are causing the issue. This will dramatically narrow down the possible problems.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, May 21, 2018 2:58 PM
    Moderator
  • Hi Mulder2008,

    Try setting up project properties like below and see what happens.

    anyCPU


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, May 21, 2018 8:09 PM
    Moderator
  • thanks for your help,

    but it's not a crash issue, only InvalidProgramException raised, and actually the call stack would not help even if I post here.


    fight for future!

    Tuesday, May 22, 2018 12:48 AM
  • thanks for your help, yes I've set up the compiler option like you mentioned, but the problem persists.

    fight for future!

    Tuesday, May 22, 2018 12:49 AM
  • "but it's not a crash issue, only InvalidProgramException raised"

    What? An exception, unless handled by your code will crash it. Are you handling the exception? At the point the exception occurs the stack trace is captured. The stack trace will identify what was executing at the time the error occurred. Using this information you can quickly narrow down the actual problem. If you are using exception handling then log that information to your logger of choice. If you are getting a message box or similar when the exception occurs then use Process Explorer or similar to capture the crash dump. This can then be loaded into VS and you can see a picture of what your program was doing at the time.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, May 22, 2018 1:51 AM
    Moderator
  • yes, it's an Exception captured in our code, and only our codes are involved in the call stack, and I'm sure it's not the fault of our source code, because it's running properly if the solution is built in VS2013, while this problem occurred if it's built in VS2017, it's more like a C# compiler problem.

    similar to this one:

    https://support.microsoft.com/en-us/help/312544/you-may-receive-an-invalidprogramexception-error-message-when-you-run

    it's fixed in .net framework 2.0, however comes back in c# Roslyn


    fight for future!

    Tuesday, May 22, 2018 5:09 AM
  • Hi Mulder2008,

    As we know, the codes we program using C# are implemented by the source code of the .NET Framework. There may be some change in source code of different versions of .NET Framework, which may cause that the same codes are implemented by different ways. you could check the difference .NET Framework 4.7.1 and 4.5.2 using the link: https://referencesource.microsoft.com/. If necessary, you can share the codes which trigger the exception.

    Dolen


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 22, 2018 9:53 AM
  • Don't worry.

    after changing to using normal way instead of reflection, I managed to fix it.


    fight for future!

    • Marked as answer by Mulder2008 Tuesday, May 22, 2018 10:00 AM
    Tuesday, May 22, 2018 10:00 AM
  • Glad to hear that the exception is fixed.

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 23, 2018 2:09 AM
  • that does not change anything, still present in .net core 3.1 - to reproduce try (add needed nuget package and put any existing win service process id below) simple net core console app with:        class Program
        {
            static void Main(string[] args)
            {
                string s = getServiceStartTime(true);
                Console.WriteLine("Hello World!" + s);
            }

            private static string getServiceStartTime(bool withFile)
            {
                string s = string.Empty;
                int pid;
                //Get-Process | select name, id, starttime | select-string <PID>
                try
                {
                    using (Process p = Process.GetCurrentProcess()) { pid = p.Id; };
                    pid = 44344;
                    PowerShell psinstance;
                    if (withFile)
                    {
                        string ps = $"Get-Process | select name, id, starttime | select-string {pid}";
                        var tmp = System.IO.Path.GetTempFileName();
                        System.IO.File.WriteAllText(tmp, ps);
                        psinstance = PowerShell.Create().AddScript(tmp);
                    }
                    else
                    {
                        psinstance = PowerShell.Create()
                            .AddCommand("get-process")
                            .AddCommand("select name, id, starttime")
                            .AddCommand($"select-string {pid}");
                    }
                    var results = psinstance.Invoke();
                    s += results;
                }
                catch (Exception ex)
                {
                    s += ex.Message;
                    Console.WriteLine(ex.Message);
                }

                return s;
            }
        }
    Tuesday, June 2, 2020 4:51 PM
  • Your post seems to have nothing to do with this old post related to exceptions and reflection. Please post new questions in their own thread and someone may be able to help you.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, June 2, 2020 5:37 PM
    Moderator