diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index ce7d8863..30e9bbcf 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -716,23 +716,8 @@ namespace CppSharp.Generators.CLI var insideClass = typedef.Namespace is Class; - string callingConvention = null; - switch (function.CallingConvention) - { - case CallingConvention.C: - callingConvention = "Cdecl"; - break; - case CallingConvention.StdCall: - callingConvention = "StdCall"; - break; - case CallingConvention.ThisCall: - callingConvention = "ThisCall"; - break; - case CallingConvention.FastCall: - callingConvention = "FastCall"; - break; - } - if (callingConvention != null) + var callingConvention = function.CallingConvention.ToInteropCallConv(); + if (callingConvention != System.Runtime.InteropServices.CallingConvention.Winapi) { WriteLine("[{0}({1}::{2})] ", "System::Runtime::InteropServices::UnmanagedFunctionPointer", diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index ecb3bd9c..02443502 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -43,25 +43,6 @@ namespace CppSharp.Generators.CSharp return new string(id.Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray()); } - public static string ToCSharpCallConv(CallingConvention convention) - { - switch (convention) - { - case CallingConvention.Default: - return "Winapi"; - case CallingConvention.C: - return "Cdecl"; - case CallingConvention.StdCall: - return "StdCall"; - case CallingConvention.ThisCall: - return "ThisCall"; - case CallingConvention.FastCall: - return "FastCall"; - } - - return "Winapi"; - } - public const string InstanceIdentifier = "__Instance"; public static string GetAccess(AccessSpecifier accessSpecifier) @@ -1608,7 +1589,7 @@ namespace CppSharp.Generators.CSharp } WriteLine("[SuppressUnmanagedCodeSecurity]"); WriteLine("[UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.{0})]", - Helpers.ToCSharpCallConv(method.CallingConvention)); + method.CallingConvention.ToInteropCallConv()); CSharpTypePrinterResult retType; var @params = GatherInternalParams(method, out retType); @@ -2562,8 +2543,8 @@ namespace CppSharp.Generators.CSharp else if (typedef.Type.IsPointerTo(out functionType)) { PushBlock(CSharpBlockKind.Typedef); - WriteLine("[UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.{0})]", - Helpers.ToCSharpCallConv(functionType.CallingConvention)); + WriteLine("[UnmanagedFunctionPointerAttribute(global::System.Runtime.InteropServices.CallingConvention.{0})]", + functionType.CallingConvention.ToInteropCallConv()); TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); WriteLine("{0}unsafe {1};", Helpers.GetAccess(typedef.Access), @@ -2697,9 +2678,9 @@ namespace CppSharp.Generators.CSharp if (Options.GenerateInternalImports) libName = "__Internal"; - Write("[DllImport(\"{0}\", ", libName); - - var callConv = Helpers.ToCSharpCallConv(function.CallingConvention); + Write("[DllImport(\"{0}\", ", libName); + + var callConv = function.CallingConvention.ToInteropCallConv(); WriteLine("CallingConvention = global::System.Runtime.InteropServices.CallingConvention.{0},", callConv); diff --git a/src/Generator/Generators/ExtensionMethods.cs b/src/Generator/Generators/ExtensionMethods.cs new file mode 100644 index 00000000..c48da7a9 --- /dev/null +++ b/src/Generator/Generators/ExtensionMethods.cs @@ -0,0 +1,27 @@ +using CppSharp.AST; +using Interop = System.Runtime.InteropServices; + +namespace CppSharp.Generators +{ + public static class ExtensionMethods + { + public static Interop.CallingConvention ToInteropCallConv(this CallingConvention convention) + { + switch (convention) + { + case CallingConvention.Default: + return Interop.CallingConvention.Winapi; + case CallingConvention.C: + return Interop.CallingConvention.Cdecl; + case CallingConvention.StdCall: + return Interop.CallingConvention.StdCall; + case CallingConvention.ThisCall: + return Interop.CallingConvention.ThisCall; + case CallingConvention.FastCall: + return Interop.CallingConvention.FastCall; + } + + return Interop.CallingConvention.Winapi; + } + } +}