Question

Suppose I have a file which imports a number of functions from dll, before every function I must put:

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]

But it is boring and code becomes longer -> harder to understand, in C I can use define like this:

#define DLL_IMPORT [DllImport(...)]

What can I do in C#?

Was it helpful?

Solution

You're out of luck. The DllImport attribute is sealed, so you can't derive an instance and default the parameters, and C# has no macro processor to allow you to work around this.

OTHER TIPS

You can use P/Invoke Interop Assistant to make your job a little easier. This tool automatically generates managed p/invoke declarations (in C# or VB) from native signatures. http://blogs.msdn.com/b/bclteam/archive/2008/06/23/p-invoke-interop-assistant-justin-van-patten.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top