Inject .Net Code Error

I am pretty new to UIPath and am wanting to use some custom C# code to process an Excel file that is rather large. I am trying to use the Inject .Net Code activity, but keep getting a generic Inject .NET Code: Dot net code failed exception.

UIPath is recognizing the class and methods contained in of the .dll file, but when it runs I keep getting a generic Inject .NET Code: Dot net code failed exception. I pared down the c# code to what amounts to a Hello World, and still get an error. Is there a particular .Net Framework that needs to be used, or anything?

The only code in the .dll is:

public class Class1
    {

        public static void testMethod()
        {
            Console.WriteLine("Hello World from the DLL!");
        }
    }

@kevin.moore

Hello Kevin,
welcome in the UiPath Community.

I assume you mean the Invoke Code activity. This is the easiest way to execute C# code.

image

Try this in the Invoke Code activity:

Console.WriteLine("Hello World from the Inovke Code activity!");

image

image

If you want to use functions from DLLs, you can use this procedure.

Best regards
Stefan

Hi Stefan,

Thanks for the reply.

I saw the invoke code activity and chose to give the inject .NET code a try instead so that I could debug the c# code separately.

image

UIPath recognizes the .dll file and the public static methods inside of it, but when it tries to invoke it, it just fails. I’ll try invoking the .dll from the invoke code activity as you outline in the linked tutorial and see what happens.

Thanks Again,

Kevin

@kevin.moore

Hello Kevin,

I have never used this activity before. This activity “Injects .NET code into the main UI thread of the target application. Intended for usage with .NET UI applications that do not expose traditional automation technologies or cannot be correctly targeted by traditional means”, as the documentation says. In my opinion really only for special use cases to apply.

I changed your code a little bit:

public class Class1
    {
        public static object testMethod()
        {
            return "Hello World from the DLL!";
        }
    }

It delivers now an object back, because I assume a console output will not really work in this context. As target I use a tiny dotNET Windows Forms application and it works as expected.

image

image

I recommend you to use the Invoke Code Activity instead of Inject dotNET DLL code into a running application. Running DLL code is much easier with Invoke Code activity. Unless you want to automate an UI that can’t be automated on another way.

Best regards
Stefan

3 Likes

Great info, I read the documentation, but did not realize it would only work if the program had a UI, I was actually going to try a Windows form app as a test today as well. I don’t need any UI functionality, so I will go with your suggestion of invoking the .dll from the invoke code activity. Thankyou much!!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.