C
C#•11mo ago
Justdie

import every function from a C module

Recently found out about DllImport, thought about using it to bind stuff, but im rather unsure how to go about things, following this example, it looks to me like you can only add one function per DllImport, do i have to call it multiple time?
public class Tester
{
[DllImport("libtest.so", EntryPoint="print")]

static extern void print(string message);

public static void Main(string[] args)
{

print("Hello World C# => C++");
}
}
public class Tester
{
[DllImport("libtest.so", EntryPoint="print")]

static extern void print(string message);

public static void Main(string[] args)
{

print("Hello World C# => C++");
}
}
this is the example i found found an answer to my question but its ten years old so i'd assume it would be good by now https://stackoverflow.com/questions/15781627/c-one-attribute-for-multiple-declarations-dllimport
Stack Overflow
C#: One attribute for multiple declarations (DLLImport)
I'm using the [DLLImport] attribute to access a bunch of C++ functions in my .NET code. For now, I have all the functions in the following way: const string DLL_Path = "path\to\my\dll.dll"; [
4 Replies
Honza K.
Honza K.•11mo ago
No, it's still one to one relationship... One DllImport per function, is it annoying? If you have many functions, yes 😄
Jester
Jester•11mo ago
for that reason there are source generatora like CsWin32 to do this for you. also in current C# its a little better to use LibraryImport instead (basically the same thing)
arion
arion•11mo ago
By the title of the thread. There are libraries / plugins that can do winapi functions pretty good, as listed above, CsWin32 (A source geneerator) there's also nuget packages like Vanara.PInvoke
If you plan to target non-winapi libs, you could try out https://github.com/dotnet/ClangSharp
GitHub
GitHub - dotnet/ClangSharp: Clang bindings for .NET written in C#
Clang bindings for .NET written in C#. Contribute to dotnet/ClangSharp development by creating an account on GitHub.
arion
arion•11mo ago
Downsides of things like Vanara.PInvoke or ClangSharp would be that you import all functions rather than only used functions, (bigger build binary) but you do get intelli-sense Though you could remove unused generated bindings in ClangSharp
Want results from more Discord servers?
Add your server