private static bool TryConvertToInteropCallingConvention(NativeCallingConvention conv, ref CallingConvention kind) { switch (conv) { case NativeCallingConvention.WinApi: kind = CallingConvention.Winapi; return(true); case NativeCallingConvention.CDeclaration: kind = CallingConvention.Cdecl; return(true); case NativeCallingConvention.Standard: kind = CallingConvention.StdCall; return(true); default: return(false); } }
static internal CodeAttributeDeclaration CreateUnmanagedFunctionPointerAttribute(NativeCallingConvention conv) { CodeAttributeDeclaration attr = new CodeAttributeDeclaration(new CodeTypeReference(typeof(System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute))); CallingConvention kind = default(CallingConvention); if (TryConvertToInteropCallingConvention(conv, ref kind)) { attr.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(CallingConvention)), kind.ToString()))); } return(attr); }
/// <summary> /// Create a DllImport attribute /// </summary> /// <param name="dllName"></param> /// <param name="entryPoint"></param> /// <returns></returns> /// <remarks></remarks> static internal CodeAttributeDeclaration CreateDllImportAttribute(string dllName, string entryPoint, NativeCallingConvention conv) { CodeAttributeDeclaration attr = new CodeAttributeDeclaration(new CodeTypeReference(typeof(System.Runtime.InteropServices.DllImportAttribute))); attr.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(dllName))); attr.Arguments.Add(new CodeAttributeArgument("EntryPoint", new CodePrimitiveExpression(entryPoint))); // If the calling convention for a DLl is not the standard WinApi then output the extra information // into the attribute CallingConvention kind = default(CallingConvention); if (TryConvertToInteropCallingConvention(conv, ref kind) && kind != CallingConvention.Winapi) { attr.Arguments.Add(new CodeAttributeArgument("CallingConvention", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(CallingConvention)), kind.ToString()))); } return(attr); }