# Inject .Net Code Error

**URL:** <https://forum.uipath.com/t/inject-net-code-error/394408>\
**Category:** Activities\
**Tags:** system-error, question, uiautomation, activities\
**Created:** [January 27, 2022, 12:34am UTC](https://forum.uipath.com/t/inject-net-code-error/394408 "2022-01-27T00:34:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin.moore](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kevin.moore/32/263396_2.png) [@kevin.moore](https://forum.uipath.com/u/kevin.moore)\
**Post date:** [January 27, 2022, 12:34am UTC](https://forum.uipath.com/t/inject-net-code-error/394408/1 "2022-01-27T00:34:37Z")

</div>

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:

```auto
public class Class1
    {

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

```

---

<div class="post-metadata">

**Author:** ![StefanSchnell](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/stefanschnell/32/173622_2.png) [@StefanSchnell](https://forum.uipath.com/u/StefanSchnell)\
**Post date:** [January 27, 2022, 5:01am UTC](https://forum.uipath.com/t/inject-net-code-error/394408/2 "2022-01-27T05:01:16Z")

</div>

@kevin.moore

Hello Kevin,  
welcome in the UiPath Community.

I assume you mean the [Invoke Code activity](https://docs.uipath.com/activities/docs/invoke-code). This is the easiest way to execute C# code.

![image](https://global.discourse-cdn.com/uipath/original/4X/8/5/e/85e0fd77775ddc6cfc930b888c184e1081f48d06.png)

Try this in the Invoke Code activity:

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

![image](https://global.discourse-cdn.com/uipath/original/4X/b/d/e/bde963562f599c8a386929bc3e8871c5e0594a33.png)

![image](https://global.discourse-cdn.com/uipath/original/4X/3/0/a/30a021d342a803e9280ba44259661fd766c705bf.png)

If you want to use functions from DLLs, you can use [this procedure](https://forum.uipath.com/t/how-to-use-dotnet-assemblies/343211).

Best regards  
Stefan

---

<div class="post-metadata">

**Author:** ![kevin.moore](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kevin.moore/32/263396_2.png) [@kevin.moore](https://forum.uipath.com/u/kevin.moore)\
**Post date:** [January 27, 2022, 11:54am UTC](https://forum.uipath.com/t/inject-net-code-error/394408/3 "2022-01-27T11:54:19Z")

</div>

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](https://global.discourse-cdn.com/uipath/original/4X/3/1/3/313fb7324c4be0d861573ba65529b95cadc6ffa9.png)

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

---

<div class="post-metadata">

**Author:** ![StefanSchnell](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/stefanschnell/32/173622_2.png) [@StefanSchnell](https://forum.uipath.com/u/StefanSchnell)\
**Post date:** [January 27, 2022, 3:12pm UTC](https://forum.uipath.com/t/inject-net-code-error/394408/4 "2022-01-27T15:12:41Z")

</div>

@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](https://docs.uipath.com/activities/docs/inject-dot-net-code) says. In my opinion really only for special use cases to apply.

I changed your code a little bit:

```auto
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](https://global.discourse-cdn.com/uipath/original/4X/d/7/b/d7b649714099da30b1bc43435abefd89d2362a82.png)

![image](https://global.discourse-cdn.com/uipath/original/4X/c/8/3/c83586e0ffb85a9cc5ca313353b64e77d3b495f7.png)

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

---

<div class="post-metadata">

**Author:** ![kevin.moore](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/kevin.moore/32/263396_2.png) [@kevin.moore](https://forum.uipath.com/u/kevin.moore)\
**Post date:** [January 27, 2022, 4:38pm UTC](https://forum.uipath.com/t/inject-net-code-error/394408/5 "2022-01-27T16:38:27Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [January 30, 2022, 4:38pm UTC](https://forum.uipath.com/t/inject-net-code-error/394408/6 "2022-01-30T16:38:49Z")

</div>

Closed on January 30, 2022, 4:38pm UTC

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