79239881

Date: 2024-11-30 15:38:39
Score: 2
Natty:
Report link

I tested this case in detail, and experimentally found out the following:

If you marshal out a parameter that.

  1. has a meaningful type that does not allow null without an explicit Nullable wrapper that is not supported by the marshalizer (e.g. float).
  2. Declared in the marshalizable library as float* (a pointer to a memory region that may be empty)

Thus, if the marshalizable function writes null there, then in C# the value of such a variable will be 0 (since out will declare it as 0 when initializing the variable, and null will simply not be written).

As a result, we will have out float = 0 without any error on the part of dotnet, so, answering your own question, in this case you can disregard the compliance with the original documentation and use the more convenient variant with out float instead of out IntPtr, but keep in mind that in case of an error, in the place where null should be, there will be 0.

I hope I was able to explain it in a clear way, and that people who have the same question will understand me.

I would be glad if people who are more knowledgeable in the subject would complement me if I have made a mistake.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): have the same question
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Eduard Gushchin