Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MarshalAs with LPArray #3

Closed
getinthedamnbox opened this issue Aug 28, 2019 · 3 comments
Closed

MarshalAs with LPArray #3

getinthedamnbox opened this issue Aug 28, 2019 · 3 comments
Labels
bug Something isn't working discovered limitation external Issue not caused or meant to be fixed by this project

Comments

@getinthedamnbox
Copy link

Hello,

Thank you very much for developing this tool. I'm assisting a professor at my university with some programming tasks, and this tool seems like it solves one of the problems I'm working on. I have a question that I'm hoping you might be able to help me with.

In your documentation, I see this in the "Limitations" section:

[MarshalAs] attribute's properties MarshalCookie, MarshalType, MarshalTypeRef and SafeArrayUserDefinedSubType are not supported (due to Mono bug).

As expected given this issue, there are a couple of DLL imports I can't mock without Unity crashing:

[DllImport("Behavior")]
public static extern void transferActorData([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] actorParameter[] agents, int agentcount, int behavior);

[DllImport("Behavior")]
public static extern void transferObstacleData([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] obstacleParameter[] obstacles, int obstaclecount);

I'm assuming this is because they use MarshalAs with the UnmanagedType parameter.

Admittedly, I'm not familiar enough with imports of this kind to know how to work around this. (This is my first time attempting to use custom plugins in Unity.) I wanted to ask if you know of a different way to pass these arrays, which could be compatible with your tool. Is there such a thing, to your knowledge?

Thanks again for sharing your work on this.

Matt

@mcpiroman
Copy link
Owner

So to clarify, according to readme your signatures should work, as you don't use mentioned properties from [MarshalAs] (so UnmanagedType and SizeParamIndex should be fine).

However, I'm able to repro crash with these properties on array parameter. I'll investigate this (it smells like yet another bug in Mono).

In your case, it seems to me that SizeParamIndex is not required if the array is only passed into unmanaged code (not backwords) and UnmanagedType is not required as LPArray should be the default in this case. So then you could remove [MarshalAs] altogether from these imports which should avoid the crash. Please note however that I'm not expert in the interop and marshaling arrays is peculiarly complicated and apparently poorly documented thing.

@getinthedamnbox
Copy link
Author

Thank you for the clarification and the advice about removing [MarshalAs]. Just as you said, I was able to remove it entirely and avoid the crash. The tool is now working perfectly. Much appreciated!

@mcpiroman
Copy link
Owner

It turned out to be another bug in Mono, so I can't do much about it. I'll simply add it to limitations, which is quite unfortune, because unlike previous ones this is actually being used. Thanks for report.

@mcpiroman mcpiroman changed the title MarshalAs with type MarshalAs with LPArray Sep 1, 2019
@mcpiroman mcpiroman added bug Something isn't working external Issue not caused or meant to be fixed by this project labels Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working discovered limitation external Issue not caused or meant to be fixed by this project
Projects
None yet
Development

No branches or pull requests

2 participants