by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jun 30 14:27
    chamons edited #777
  • Jun 30 14:26
    chamons milestoned #777
  • Jun 30 14:26
    chamons labeled #777
  • Jun 30 14:26
    chamons labeled #777
  • Jun 30 08:16
    delebedev edited #777
  • Jun 30 08:16
    delebedev opened #777
  • Jun 03 18:34
    whitneyschmidt closed #774
  • May 27 21:17
    chamons labeled #776
  • May 27 21:17
    chamons closed #776
  • May 25 19:26
    ILAgent opened #776
  • May 08 16:56
    chamons closed #764
  • May 08 16:13
    chamons edited #775
  • May 08 16:13
    chamons milestoned #775
  • May 08 16:12
    chamons labeled #775
  • May 08 16:12
    chamons labeled #775
  • May 08 16:05
    chamons edited #775
  • May 04 23:38
    douglaskbell opened #775
  • Apr 27 16:24
    douglaskbell edited #774
  • Apr 27 16:23
    douglaskbell edited #774
  • Apr 27 14:42
    douglaskbell edited #774
João Matos
@tritao
As a first step, you can implement CSharpSignatureType
And return return new CILType(typeof(System.Guid));
That should get those ignored methods generating with the correct signature
Peter Dräxler
@PeterDraex
So this is my code:
    [TypeMap("GUID", GeneratorKind = GeneratorKind.CSharp)]
    public class GUID : TypeMap
    {
        public override CppSharp.AST.Type CSharpSignatureType(TypePrinterContext ctx) => new CILType(typeof(System.Guid));
    }
But I still get these errors:
Function 'WebAuthNAuthenticatorMakeCredential' was ignored due to ignored param
Function 'WebAuthNAuthenticatorGetAssertion' was ignored due to ignored param
Typedef 'HWND' was ignored due to ignored type
Typedef 'GUID' was ignored due to ignored type
João Matos
@tritao
You need to do the same for HWND
Peter Dräxler
@PeterDraex
Even though some errors have disappeared. I don't know now, why the WebAuthNAuthenticatorGetAssertion method is being ignored.
Okay, but I thought that the error with GUID should not be there now?
João Matos
@tritao
WebAuthNAuthenticatorGetAssertion( In HWND
Those are just for the typedefs, I don't think it matters since they are being mapped
I think we have support for what I called opaque pointers, where basically it would just generate an empty struct for those C typedef-to-a-pointer cases
So you could try that as well
For that you would need to add some code to your Preprocess method in CppSharp
And try something like: ctx.FindClass("HWND").IsOpaque = true;
It could help.
We should probably do it by default for incomplete classes but we don't at the moment.
Peter Dräxler
@PeterDraex
I'll need to actually pass a pointer to the method (window handle). Will I still be able to do that, if the parameter is bound to empty struct?
João Matos
@tritao
Maybe not, in that case you better map HWND to an IntPtr
Or create your own HWND class in C# and map it to that for better type safety
Peter Dräxler
@PeterDraex
worked, my method got generated 🎉
Thank you @tritao for help!
João Matos
@tritao
You're welcome
Nice that you got it working 🎉
Peter Dräxler
@PeterDraex

I get errors in the generated code, like this one:

Cannot implicitly convert type 'System.IntPtr' to 'byte*'. An explicit conversion exists (are you missing a cast?)

What can be done about it?

João Matos
@tritao
I am guessing those are related to the type maps, you need to implement the rest of the type map marshaling methods.
Peter Dräxler
@PeterDraex
@tritao I'm looking at the example type maps from Stdlib.cs you've posted earlier, but still have no idea what to add to fix missing conversions. Could you give me some more hints?
My current code:
    [TypeMap("GUID", GeneratorKind = GeneratorKind.CSharp)]
    public class GUID : TypeMap
    {
        public override CppSharp.AST.Type CSharpSignatureType(TypePrinterContext ctx) => new CILType(typeof(System.Guid));

        public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { }
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { }
    }

    [TypeMap("HWND", GeneratorKind = GeneratorKind.CSharp)]
    public class HWND : TypeMap
    {
        public override CppSharp.AST.Type CSharpSignatureType(TypePrinterContext ctx) => new CILType(typeof(System.IntPtr));
        public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { }
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { }
    }
João Matos
@tritao
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
{
    ctx.Return.Write("new Std.WString()");
}

public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
{
    ctx.Return.Write(ctx.ReturnVarName);
}
Here is an example from Stdlib, with a minimal implementation
What you write in each method depends on your specific case
In the case of GUID, you need to figure out how to convert the native GUID* into a System.Guid using C# code
HWND is probably simpler since you might be able to just cast.
Peter Dräxler
@PeterDraex
    [TypeMap("HWND", GeneratorKind = GeneratorKind.CSharp)]
    public class HWND : TypeMap
    {
        public override CppSharp.AST.Type CSharpSignatureType(TypePrinterContext ctx) => new CILType(typeof(System.IntPtr));
        public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) => ctx.Return.Write("(IntPtr)" + ctx.Parameter.Name);
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx) => ctx.Return.Write("(byte*)" + ctx.ReturnVarName);
    }
Did I get it right?
João Matos
@tritao
Looks good to me
Peter Dräxler
@PeterDraex
It produced invalid code:
            var __arg0 = (byte*);
the variable name is missing
João Matos
@tritao
You got the methods swapped
Peter Dräxler
@PeterDraex

Aha, thanks.

Now I'm not sure how to write CSharpMarshalToManaged for GUID. GUID contains method that returns GUID as array of bytes, but I need to return a pointer to that array, right? To get a valid pointer, I'd first have to allocate a new array on the global heap, right? I assume such code would be too complex for ctx.Return.Write().

João Matos
@tritao
About the marshaling strategy, a global allocated array works, however if you know the native code does not keep ownership of the passed in pointers, you can just use either a local variable or stackalloc.
How you organize the code, you can just write a non-generated .cs file with helper methods that you call on type map. Or you can write more complex code inside the type map, using ctx.Before text generator, which allows you to write anything you'd like.
Peter Dräxler
@PeterDraex
I'll try it and get back later. Thank you for now.
Peter Dräxler
@PeterDraex

@tritao I found out that GUID is defined as struct in included headers.

typedef struct {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    byte           Data4[ 8 ];
} GUID;

Do you have any idea why it's not being generated in C#? The verbose output just shows:

Typedef 'GUID' was ignored due to ignored type
João Matos
@tritao
@PeterDraex We ignore types defined in system headers by default
You can explicitly enable it though
Peter Dräxler
@PeterDraex

Hello, my C function contains this output parameter:

_Outptr_result_maybenull_ PWEBAUTHN_ASSERTION                   *ppWebAuthNAssertion

It generates parameter of class WEBAUTHN_ASSERTION, which results in System.AccessViolationException when I try to access the object.
What would the correct type of this parameter in generated code?

João Matos
@tritao
Please open a new issue about it, and include the native code, and generated managed code.
Peter Dräxler
@PeterDraex
👍 Done. mono/CppSharp#1381
Peter Dräxler
@PeterDraex
Any thoughts on how to resolve this? mono/CppSharp#1381
fsinisi90
@fsinisi90
Hi there! To handle parameters with default values should I add a "HandleDefaultParamValuesPass" in my Driver? Or are the passes already added automatically? I have a default value in my C++ function but it's still marked as required in C#.
fsinisi90
@fsinisi90
Ok, solved with options.GenerateDefaultValuesForArguments = true;