C
C#5mo ago
Shoey

How can I use a precompiled C/C++ dll in a C# project?

Someone kindly showed me how to utilize the dll in C and while I understand the structure I don't have any idea how hooking into a dll from C# works and only the barest for C, only really knowing the basic attribute in C# such as DllImport["example.dll", CallingConvention = (I don't really know how to use CC)].
15 Replies
kianyuen
kianyuen5mo ago
What do you have about those dll files? Any document or any kind of declaration?
Shoey
ShoeyOP5mo ago
They are projects on github. Sadly they don't have much documentation.
Shoey
ShoeyOP5mo ago
GitHub
GitHub - ModOrganizer2/usvfs at v0.5.6.2
library using api hooking to implement process-local filesystem-independent file links. - GitHub - ModOrganizer2/usvfs at v0.5.6.2
Shoey
ShoeyOP5mo ago
GitHub
GitHub - Holt59/usvfs-example
Contribute to Holt59/usvfs-example development by creating an account on GitHub.
Shoey
ShoeyOP5mo ago
I understand the .h files in the includes folder in the example are setting up functions using the dll to be used in the main code but I'm not sure how I could translate what that shows over to C#.
kianyuen
kianyuen5mo ago
Maybe take a look at this. https://learn.microsoft.com/en-us/dotnet/framework/interop/marshalling-data-with-platform-invoke I am also importing some dll for a project, just matching the data types.
Marshalling Data with Platform Invoke - .NET Framework
Marshal data with platform invoke in .NET. See a list of data types used in Windows APIs and C-style functions, and find their .NET managed type equivalents.
Shoey
ShoeyOP5mo ago
I'm not too sure how applicable that is for me My hangup is I can see that in C a specific function is accessed in the main "class" by including "__declspec(dllimport) void WINAPI usvfsClearVirtualMappings();" under the include folder I don't know how to be sure I'm accessing the same function by using DllImport in C#
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
kianyuen
kianyuen5mo ago
Ah sorry, I didn't take a deep look into the repo
saucecontrol
saucecontrol5mo ago
WINAPI means StdCall calling convention in C#, which is the default in Windows, so you can leave out calling convention if that's what your C DLL defines and you only care about Windows. all you need for a proper DllImport declaration is to use compatible C# types for all args to the function. the easiest way to generate compatible declarations these days is to use https://www.nuget.org/packages/ClangSharpPInvokeGenerator you point the generator app at your library's .h file, and it will create C# bindings for you.
ClangSharpPInvokeGenerator 18.1.0
ClangSharp are strongly-typed safe Clang bindings written in C# for .NET and Mono, tested on Linux and Windows.
saucecontrol
saucecontrol5mo ago
if you're shaky on the C side as well as the C# side, it can be tricky business, but if you know C reasonably well, it's pretty easy to make sure the bindings are compatible
Shoey
ShoeyOP5mo ago
Thank you! I have a decent understanding of it and seem to be able to link some methods to their functions but it is extremely reliant on pointers in a way that C# doesn't seem to support. I am familiar enough with C to manipulate the example executable and get it to set up the VFS and launch a desired executable. Maybe I could write the GUI in C# and have it save the file system structure to a text file, then use a C executable as an in-between that can have the text file and the executable to launch with the VFS passed as arguments?
saucecontrol
saucecontrol5mo ago
reliant on pointers in a way that C# doesn't seem to support
this is unlikely. there may be some opaque pointer typedefs that don't translate cleanly, but you can just declare those as void* in C#. a pointer is a pointer at the ABI level
Shoey
ShoeyOP5mo ago
Maybe. I'm not familiar at all with pointers other than what I've read doing this. The debug info printout is reliant on was a string pointer and I couldn't find info on getting that working. I don't think they were used as pointers but there were also LPSECURITY_ATTRIBUTES, LPSTARTUPINFOW, and LPPROCESS_INFORMATION variables. I tried replicating them with struct and using the table linked above but that didn't really work either, just giving me memory access errors all around. Once I got into the memory allocation management part of trying to replicate string pointers I just gave up and decided on the interop exe way.
Want results from more Discord servers?
Add your server