mono/CppSharp

How to override

Maxwell175 opened this issue · 7 comments

Brief Description

In my situation I have a typedefed C enum that is intended to be passed as a parameter to a function. However, this function declaration merely identifies the parameter as a long so there is no way for CppSharp to figure out that the 2 are related. How can I override the parameter of this function to use the Enum type?

OS: Windows

Used headers
// attribute 
typedef enum
{
    SP_ATTR_TIMEOUT         = 1,
    SP_ATTR_LOG             = 2        
} SP_ATTR_ENUM;

SP_EXPORT int       SP_API SP_SetProperty   (SP_HANDLE hDiag, long lProperty, long lFlag, const void *lpValue);

Need more info to be able to help you here.

Basically CppSharp generated a function with the following signature:

public static int SP_SetProperty(__IntPtr hDiag, int lProperty, int lFlag, __IntPtr lpValue)

I need to get it to change to:

public static int SP_SetProperty(__IntPtr hDiag, SP_ATTR_ENUM lProperty, int lFlag, __IntPtr lpValue)

What kind of approaches can I use for this?

You can change the type of your parameter as a processing step, see for example the helper methods here: https://github.com/mono/CppSharp/blob/3f923b1/src/Generator/Library.cs#L474

You can do something like SetFunctionParameterUsage but instead use it to modify the type of the parameter to the one of the enum.

So this is where I got stuck. I found the FindFunction function and used it to get the function, then grabbed the right parameter, but the problem is how to I set the type to an enum? Calling FindEnum returns an Enumeration object. I'm not sure how to properly put these together.

So this is where I got stuck. I found the FindFunction function and used it to get the function, then grabbed the right parameter, but the problem is how to I set the type to an enum? Calling FindEnum returns an Enumeration object. I'm not sure how to properly put these together.

Think you need to do something like param.QualifiedType = new QualifiedType(TagType(enum)).

Got it. Here is the code I ended up with:

ctx.FindFunction("SP_SetProperty").First().Parameters[1].QualifiedType =
            new QualifiedType(new TagType(ctx.FindEnum("SP_ATTR_ENUM").First()));

Would be nice to have a helper function for this as well.

Feel free to send a PR