Browse Source

Marshalled fields with a pointer type as IntPtr.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
cpp_module_crash
Dimitar Dobrev 10 years ago
parent
commit
336dbe8ff8
  1. 29
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 2
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  3. 2
      src/Generator/Types/Std/Stdlib.cs

29
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -360,7 +360,7 @@ namespace CppSharp.Generators.CSharp
else else
{ {
WriteLine("public {0} {1} {{ get; protected set; }}", WriteLine("public {0} {1} {{ get; protected set; }}",
"global::System.IntPtr", Helpers.InstanceIdentifier); CSharpTypePrinter.IntPtrType, Helpers.InstanceIdentifier);
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -688,19 +688,22 @@ namespace CppSharp.Generators.CSharp
var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter); var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter);
TypePrinter.PopMarshalKind(); TypePrinter.PopMarshalKind();
var typeName = safeIdentifier; var fieldType = field.Type.IsAddress() ?
CSharpTypePrinter.IntPtrType : fieldTypePrinted.Type;
var fieldName = safeIdentifier;
if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix)) if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix))
typeName += fieldTypePrinted.NameSuffix; fieldName += fieldTypePrinted.NameSuffix;
var access = @class != null && !@class.IsGenerated ? "internal" : "public"; var access = @class != null && !@class.IsGenerated ? "internal" : "public";
if (field.Expression != null) if (field.Expression != null)
{ {
var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter); var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter);
Write("{0} {1} {2} = {3};", access, fieldTypePrinted.Type, typeName, fieldValuePrinted); Write("{0} {1} {2} = {3};", access, fieldType, fieldName, fieldValuePrinted);
} }
else else
{ {
Write("{0} {1} {2};", access, fieldTypePrinted.Type, typeName); Write("{0} {1} {2};", access, fieldType, fieldName);
} }
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -859,7 +862,11 @@ namespace CppSharp.Generators.CSharp
if (marshal.Context.Return.StringBuilder.Length > 0) if (marshal.Context.Return.StringBuilder.Length > 0)
{ {
WriteLine("{0} = {1};", ctx.ReturnVarName, marshal.Context.Return); WriteLine("{0} = {1}{2};", ctx.ReturnVarName,
field.Type.IsPointer() && field.Type.GetFinalPointee().IsPrimitiveType() ?
string.Format("({0}) ", CSharpTypePrinter.IntPtrType) :
string.Empty,
marshal.Context.Return);
} }
if (arrayType != null && @class.IsValueType) if (arrayType != null && @class.IsValueType)
@ -1007,7 +1014,15 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
WriteLine("return {0};", marshal.Context.Return); var @return = marshal.Context.Return.ToString();
if (field.Type.IsPointer())
{
var final = field.Type.GetFinalPointee().Desugar();
if (final.IsPrimitiveType() && !final.IsPrimitiveType(PrimitiveType.Void) &&
!final.IsPrimitiveType(PrimitiveType.Char))
@return = string.Format("({0}*) {1}", field.Type.GetPointee().Desugar(), @return);
}
WriteLine("return {0};", @return);
if (arrayType != null && @class.IsValueType) if (arrayType != null && @class.IsValueType)
WriteCloseBraceIndent(); WriteCloseBraceIndent();

2
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -276,7 +276,7 @@ namespace CppSharp.Generators.CSharp
if (MarshalKind == CSharpMarshalKind.GenericDelegate || if (MarshalKind == CSharpMarshalKind.GenericDelegate ||
pointee.IsPrimitiveType(PrimitiveType.Void)) pointee.IsPrimitiveType(PrimitiveType.Void))
return "global::System.IntPtr"; return IntPtrType;
// Do not allow strings inside primitive arrays case, else we'll get invalid types // Do not allow strings inside primitive arrays case, else we'll get invalid types
// like string* for const char **. // like string* for const char **.

2
src/Generator/Types/Std/Stdlib.cs

@ -352,7 +352,7 @@ namespace CppSharp.Types.Std
{ {
public override string CSharpSignature(CSharpTypePrinterContext ctx) public override string CSharpSignature(CSharpTypePrinterContext ctx)
{ {
return "global::System.IntPtr"; return CSharpTypePrinter.IntPtrType;
} }
public override void CSharpMarshalToNative(MarshalContext ctx) public override void CSharpMarshalToNative(MarshalContext ctx)

Loading…
Cancel
Save