I’ve imported my C++ DLL functions into C# by following some online examples. The functions have to be declared as part of a C# class. But if I want to use these functions in other C# classes how can I share the declarations?
The best solution would be to import the C++ DLL functions into a general class and use this throughout my application.
Thanks in advance.
Edit: I tried the suggestion below but now I’m getting the error “ImportSomeStuff is inaccessible due to its protection level” where I try to use the struct. Everything seems to be public, so what else can I try?
class ImportSomeStuff
{
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
public uint nData;
}
public delegate void MyCallback(ref MyStruct myStruct);
[DllImport("My.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool AddCallback(MyCallback callback);
[DllImport("My.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool RemoveCallback(MyCallback callback);
}
(different file)
class DoSomeStuff
{
public List<ImportSomeStuff.MyStruct> listStuff = new List<ImportSomeStuff.MyStruct>();
}
And then you could call this function from everywhere: