views:

101

answers:

1

Here is the code from the dll:

  public static bool SendCommand(string command)
    {

        KillTeraTerm();

        try
        {
            SerialPort portToUse = new SerialPort("COM2");

            portToUse.Open();
            portToUse.WriteLine(command);
            portToUse.Close();

            StartTeraTerm();

            return true;

        }
        catch
        {
            return false;
        }

    }

Here is the code I am using to reference the dll:

        Assembly loadedDLL = Assembly.LoadFile(@"G:\PRODUCT VALIDATION GROUP\SOFTWARE VALIDATION\Ranorex Support Files\RTSInterface.dll");
        Type rtsObj = loadedDLL.GetType("Oe.RTS.RTSInterface");
        Object obj = Activator.CreateInstance(rtsObj);

        try
        {
            rtsObj.InvokeMember("SendCommand", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, obj, new object[] { "startbutton" });
        }
        catch (Exception ex)
        {
                MessageBox.Show(ex.Message);
        }

I know i am not using the return value yet... just want to know why this isnt working.

EDIT!!!!

First exception: Message = "Exception has been thrown by the target of an invocation."

Inner Exception Message = "Request failed." '

DeclaringMethod = 'rtsObj.DeclaringMethod' threw an exception of type 'System.InvalidOperationException'

Thanks for your help. First time using reflection so sorry for the choppy code.

EDIT #2!!!

Stack Trace from VS: Saftey Door Simulator.exe!Safety_Door_Simulator.Form1.btnInit_Click(object sender = {Text = "Initialize"}, System.EventArgs e = {X = 56 Y = 10 Button = Left}) Line 46 C# [External Code] Saftey Door Simulator.exe!Safety_Door_Simulator.Program.Main() Line 17 + 0x1d bytes C# [External Code]

EDIT #3

Inner stack trace:

StackTrace = " at Oe.RTS.RTSInterface.KillTeraTerm()\r\n at Oe.RTS.RTSInterface.SendCommand(String command)"

Code for KillTeraTerm:

   public static void KillTeraTerm()
    {
        if (Process.GetProcessesByName("ttermpro").Length != 0)
        {
            Process[] teraTermProcess = Process.GetProcessesByName("ttermpro");

            foreach (Process p in teraTermProcess)
            {
                p.Kill();
                Thread.Sleep(1000);
            }

        }
    }
+2  A: 

Do not use Assembly.LoadFile(), it loads assemblies without a loading context. Use LoadFrom() instead.

The exception message is pretty meaningless, not the kind you'd get out the .NET framework code. You need to look at the stack trace of the InnerException to know where it got raised.

Hans Passant
This doesnt work either.
Sean P
Added inner exception stack trace.
Sean P
Well, read the trace. KillTeraTerm threw an exception. You're a heckofalot closer to the source code for that method than we are.
Hans Passant
That method works if I call it when I reference it. When I use reflection it doesnt. Is there something else im missing with reflection and a static method calling another static method?
Sean P
Did you notice my warning about LoadFile? Use Debug + Exceptions, tick the Thrown checkbox to get the debugger to stop at the exception.
Hans Passant
Yes i switched it out. Ok so It seems I am getting a "Security Exception" in KillTeraTerm. "Request failed" . The DLL is on a network drive that is mapped. If that helps any.
Sean P
Painful. You need the LoadFrom() overload that accepts Evidence. Pass AppDomain.CurrentDomain.Evidence. Something like that. Moving the DLL would be a lot easier.
Hans Passant
That worked... thank you very much.
Sean P
@Sean: wow, that was *way* too much work to not get a helpful vote.
Hans Passant
lol Sorry! Repped! I would have upped you 10 if i could.
Sean P