You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docfx/docs/getting-started.md
+59Lines changed: 59 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,6 +160,65 @@ For example:
160
160
}
161
161
```
162
162
163
+
### Optional out/ref parameters
164
+
165
+
Some parameters in win32 are `[optional, out]` or `[optional, in, out]`. C# does not have an idiomatic way to represent this concept, so for any method that
166
+
has such parameters, CsWin32 will generate two versions: one with all `ref` or `out` parameters included, and one with all such parameters omitted. For example:
167
+
168
+
```c#
169
+
// Omitting the optional parameter:
170
+
IsTextUnicode(buffer);
171
+
172
+
// Passing ref for optional parameter:
173
+
IS_TEXT_UNICODE_RESULTresult=default;
174
+
IsTextUnicode(buffer, refresult);
175
+
```
176
+
177
+
### Working with Span-typed and MemorySize-d parameters
178
+
179
+
In the Win32 APIs there are many functions where one parameter is a buffer (`void*` or `byte*`) and another parameter is the size of that buffer. When generating
180
+
for a target framework that supports Spans, there will be overloads of these functions that take a `Span<byte>` which represents both of these parameters,
181
+
since a Span refers to a chunk of memory and a length. For example, an API like [IsTextUnicode](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-istextunicode)
182
+
has a `void*` parameter whose length is described by the iSize parameter in the native signature. The CsWin32 projection of this method will be:
Instead of passing the buffer and length separately, in this projection you pass just one parameter. Span is a flexible type with many things that can
189
+
be converted to it safely. You will also see Span parameters for things that may look like a struct but are variable sized. For example, [InitializeAcl](https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-initializeacl)
190
+
looks like it returns an ACL struct but the parameter is annotated with a `[MemorySize]` attribute in the metadata, indicating it is variable-sized based on another parameter.
191
+
Thus, the cswin32 projection of this method will project this parameter as a `Span<byte>` since the size of the parameter is variable:
CsWin32 will also generate a struct-typed parameter for convenience but this overload will pass `sizeof(T)` for the length parameter to the
212
+
underlying Win32 API, so this only makes sense in some overloads such as [SHGetFileInfo](https://learn.microsoft.com/windows/win32/api/shellapi/nf-shellapi-shgetfileinfow)
213
+
where the parameter has an annotation indicating it's variable-sized, but the size is only ever `sizeof(SHFILEINFOW)`:
0 commit comments