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

haryngod: <p>I tried to use libtorch in c#.<br>
I made P/Invoke dll which is wrapped libtorch.</p>
<p>But when I use it in c# application, it cannot load my custom dll.</p>
<p>I wonder is there anyone make c++ dll with libtorch and run it in c# application?</p>

Matej_Kompanek: <p>I use libtorch this way.<br>
I suggest using  (<a href="https://www.dependencywalker.com/" rel="noopener nofollow ugc">Dependency Walker</a>) to find which DLL is missing.<br>
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.</p>

haryngod: <p>Thanks for your reply!</p>
<p>I used a dependency walker, is it any abnormal point?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/0/00574b92156b2c16514ce8010ddc1ab70a96ec22.png" data-download-href="https://discuss.pytorch.org/uploads/default/00574b92156b2c16514ce8010ddc1ab70a96ec22" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/0/00574b92156b2c16514ce8010ddc1ab70a96ec22.png" alt="image" data-base62-sha1="311NR2JRmG88dJnU1Oc8JFTsuS" width="608" height="500" data-dominant-color="F3F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">649×533 10 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I set x64 both and I copied my dll and pytorch dlls in the c# output directory.</p>
<p>When I made c++ dll with include torch header only, it worked.<br>
But when I just created a tensor, it was saying ‘cannot load dll’.</p>

Matej_Kompanek: <p>That red icon of ‘TORCH_CPU.DLL’ probably indicates there is something wrong.<br>
Mine looks like this:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/c/ac42f4d543cd52f3abb6219eb8d4b6162924c8e3.png" alt="obrázok" data-base62-sha1="ozTzQYBtFXFP4V5xCnE07YaMSI3" width="328" height="362"></p>
<p>Here is a list of Dlls I copy to output, maybe you’re missing some … <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=12" title=":confused:" class="emoji" alt=":confused:" loading="lazy" width="20" height="20"><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/2/f2c66dd329e9c478e98ea33d79b7a87a31e7a6ba.png" data-download-href="https://discuss.pytorch.org/uploads/default/f2c66dd329e9c478e98ea33d79b7a87a31e7a6ba" title="obrázok"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/2/f2c66dd329e9c478e98ea33d79b7a87a31e7a6ba.png" alt="obrázok" data-base62-sha1="yDGGa1DgWLPfGqB7b0saGvnOgJk" width="210" height="500" data-dominant-color="F1F2F2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">obrázok</span><span class="informations">247×587 5.93 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

haryngod: <p>I appreciate your support.</p>
<p>I followed your last comment, so I copied all dll from pytorch libraries again.<br>
Then it works!!!<br>
Thanks!!</p>

kuolung: <p>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.</p>