11

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";

[DllImport(DLL_Path, 
    CallingConvention = CallingConvention.StdCall, 
    CharSet = CharSet.Ansi)] 
public static extern int MyFunction1();

[DllImport(DLL_Path, 
    CallingConvention = CallingConvention.StdCall, 
    CharSet = CharSet.Ansi)]
public static extern ErrorCode MyFunction2(int id);

[DllImport(DLL_Path, 
    CallingConvention = CallingConvention.StdCall, 
    CharSet = CharSet.Ansi)]
public static extern ErrorCode MyFunction3(string server, byte timeout, 
    ref int connection_id, ref DeviceInfo pInfos);

[DllImport(DLL_Path, 
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi)]
public static extern ErrorCode MyFunction4([MarshalAs(UnmanagedType.LPArray)] byte[] pVersion, 
    ref int psize);

[DllImport(DLL_Path, 
    CallingConvention = CallingConvention.StdCall, 
    CharSet = CharSet.Ansi)]
public static extern ErrorCode MyFunction5(int errorcode, 
    [MarshalAs(UnmanagedType.LPTStr)] string pmsg, ref int psize);

Which is rather not pleasing to the eye: the repetition of attributes seems unefficient and destroys readability for the prototypes of the functions. Especially since I have something like 20 or 30 functions to import.

I wonder if I could have the [DllImport(DLL_Path, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] part only once somewhere and have the function definitions more clearly identified, like this pseudo code:

const string DLL_Path = "path\\to\\my\\dll.dll";
// some code defining a section which tells that the next functions are DLLImport
[DllImport(DLL_Path, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] 
{
    public static extern int MyFunction1();

    public static extern ErrorCode MyFunction2(int id);

    public static extern ErrorCode MyFunction3(string server, byte timeout, ref int connection_id, ref DeviceInfo pInfos);

    public static extern ErrorCode MyFunction4([MarshalAs(UnmanagedType.LPArray)] byte[] pVersion, ref int psize);

    public static extern ErrorCode MyFunction5(int errorcode, [MarshalAs(UnmanagedType.LPTStr)] string pmsg, ref int psize);
}

Is this possible?
I found this question in SO: Shorten amount of DllImport in C#? but it suggests dynamically loading the functions through LoadLibrary and GetProcAddress, which I find less readable.

Community
  • 1
  • 1
Gui13
  • 12,993
  • 17
  • 57
  • 104
  • The answer is no, stick with what you have, pleasing to the eye or not. – Phil Apr 03 '13 at 07:47
  • Man that's mean! Don't you want to change the order of things sometimes? Or do you stick to what you have and don't ever dream of a world where it's easier and prettier to import DLL functions? – Gui13 Sep 23 '13 at 07:37

1 Answers1

7

No, there's no way to reduce the Attributes to a single declaration. You'll need to apply the Attribute to all methods.

But you can at least shorten your Attribute declarations to [DllImport(DLL_Path)], because the values you are specifying for CallingConvention and CharSet are the same as the default values.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • That's good enough for me, I'll put them in front of the declaration so that it's easier to read. I miss an "Attribute scope" though... it would be a nice addition! – Gui13 Apr 03 '13 at 07:59