How do I handle null or optional dll struct parameters in C#
问题 How do I deal with optional struct arguments in dll methods called from C# using pinvoke? For example, the lpSecurityAttributes parameter here should be passed null when absent. The correct way of passing struct 's seems to be using ref , but it cannot have optional parameters, or take null in general. What ways are there to achieve this? 回答1: You have a few options 1) Use a class instead of a struct I think this method is the easiest. Simply declare the struct as a class : [StructLayout