[c++][c#] Is it possible to use c++(libtorch) dll in c#

I tried to use libtorch in c#.
I made P/Invoke dll which is wrapped libtorch.

But when I use it in c# application, it cannot load my custom dll.

I wonder is there anyone make c++ dll with libtorch and run it in c# application?

I use libtorch this way.
I suggest using (Dependency Walker) to find which DLL is missing.
Make sure to have your C# assembly set to x64 and both your C++ DLL and pytorch’s DLLs have to be in the output directory of your C# assembly build.

Thanks for your reply!

I used a dependency walker, is it any abnormal point?

I set x64 both and I copied my dll and pytorch dlls in the c# output directory.

When I made c++ dll with include torch header only, it worked.
But when I just created a tensor, it was saying ‘cannot load dll’.

That red icon of ‘TORCH_CPU.DLL’ probably indicates there is something wrong.
Mine looks like this:
obrázok

Here is a list of Dlls I copy to output, maybe you’re missing some … :confused:

I appreciate your support.

I followed your last comment, so I copied all dll from pytorch libraries again.
Then it works!!!
Thanks!!

Hello, I am a new user of pytorch. Like you, I want to use c# to use pytorch + YoloV8. I would like to ask you for some experience in using c# and pytorch.