From 96eaa26d1dd87e9015b440e68a4cbd12e6832e72 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Wed, 28 Oct 2020 19:54:30 +0300 Subject: [PATCH] Add System.Runtime.InteropServices.UnmanagedFunctionPointer --- src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs | 7 ++++--- src/tools/MicroComGenerator/CSharpGen.cs | 7 +------ src/tools/MicroComGenerator/Extensions.cs | 7 +++++++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs b/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs index d6848dc48e..b4a0770e73 100644 --- a/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs +++ b/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs @@ -278,11 +278,12 @@ namespace MicroComGenerator // Generate VTable method - - var shadowDelegate = DelegateDeclaration(ParseTypeName(returnArg.NativeType), member.Name+"Delegate") + var shadowDelegate = DelegateDeclaration(ParseTypeName(returnArg.NativeType), member.Name + "Delegate") .AddParameterListParameters(Parameter(Identifier("@this")).WithType(ParseTypeName("IntPtr"))) .AddParameterListParameters(args.Select(x => - Parameter(Identifier(x.Name)).WithType(ParseTypeName(x.NativeType))).ToArray()); + Parameter(Identifier(x.Name)).WithType(ParseTypeName(x.NativeType))).ToArray()) + .AddAttribute("System.Runtime.InteropServices.UnmanagedFunctionPointer", + "System.Runtime.InteropServices.CallingConvention.StdCall"); var shadowMethod = MethodDeclaration(shadowDelegate.ReturnType, member.Name) .WithParameterList(shadowDelegate.ParameterList) diff --git a/src/tools/MicroComGenerator/CSharpGen.cs b/src/tools/MicroComGenerator/CSharpGen.cs index 93fce16dfc..8806531b19 100644 --- a/src/tools/MicroComGenerator/CSharpGen.cs +++ b/src/tools/MicroComGenerator/CSharpGen.cs @@ -109,13 +109,8 @@ namespace MicroComGenerator return ns.AddMembers(_idl.Structs.Select(e => StructDeclaration(e.Name) .WithModifiers(TokenList(Token(_visibility))) + .AddAttribute("System.Runtime.InteropServices.StructLayout", "System.Runtime.InteropServices.LayoutKind.Sequential") .AddModifiers(Token(SyntaxKind.UnsafeKeyword)) - .AddAttributeLists(AttributeList(SingletonSeparatedList( - Attribute(ParseName("System.Runtime.InteropServices.StructLayout"), - AttributeArgumentList(SingletonSeparatedList( - AttributeArgument( - ParseExpression("System.Runtime.InteropServices.LayoutKind.Sequential")))) - )))) .WithMembers(new SyntaxList(SeparatedList(e.Select(m => DeclareField(m.Type.ToString(), m.Name, SyntaxKind.PublicKeyword))))) ).ToArray()); diff --git a/src/tools/MicroComGenerator/Extensions.cs b/src/tools/MicroComGenerator/Extensions.cs index 4942442d1b..c8a4c8f45c 100644 --- a/src/tools/MicroComGenerator/Extensions.cs +++ b/src/tools/MicroComGenerator/Extensions.cs @@ -81,6 +81,13 @@ namespace MicroComGenerator return cl.AddBaseListTypes(SimpleBaseType(SyntaxFactory.ParseTypeName(bt))); } + public static T AddAttribute(this T member, string attribute, params string[] args) where T : MemberDeclarationSyntax + { + return (T)member.AddAttributeLists(AttributeList(SingletonSeparatedList( + Attribute(ParseName(attribute), AttributeArgumentList( + SeparatedList(args.Select(a => AttributeArgument(ParseExpression(a))))))))); + } + public static string StripPrefix(this string s, string prefix) => string.IsNullOrEmpty(s) ? s : s.StartsWith(prefix)