Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Reference from a C# App to a DLL compiled without /clr?

I'm using Visual Studio 2008 to build a Solution with two Projects: a C# Console App and a C++ DLL. I want the app to call a function from the dll using P/Invoke. Therefore I'm trying to add the dll as a Reference to the C# app. But when I try the Add Reference command, Visual Studio won't let me do it unless I set the /clr property on the dll (under Configuration Properties:General). Now, I thought that P/Invoke could handle plain-old win32 dlls. Indeed, if I build my dll without /clr and just copy it by hand to bin/Debug, then the app runs fine. So why is /clr required to add the dll as a reference? And if VS won't let me add it, is there some (clean) workaround so that my app finds the dll?

I see that someone had a similar issue here (though with a 3rd-party dll): Unable to add a DLL Reference to VS 2008 The answer he got was to build a wrapper. But this isn't really necessary, since the app can use the dll just fine; it's just the Add Reference step that doesn't work. And besides, won't the wrapper code need a reference to the dll, raising the same problem as before? I'd really like an answer that doesn't involve writing a wrapper at all.

like image 513
Paul A Jungwirth Avatar asked Jan 18 '10 17:01

Paul A Jungwirth


1 Answers

Why not just add a post-build step to copy your unmanaged DLL to your project directory? You don't need a "reference" to be able to refer to an unmanaged DLL, and it sounds like the only problem you're experiencing is due to the file not being automatically copied into the search path.

like image 158
Daniel Pryden Avatar answered Oct 17 '22 10:10

Daniel Pryden