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
Describe the solution you'd like
I'd like to write code like the following:
var myStruct = CreateStruct();
PInvoke.MyMethod(..., myStruct, ...);
[...]
OUTER_STRUCT CreateStruct ()
{
INNER_STRUCT is;
// fill is
OUTER_STRUCT os;
// fill os
os.Inner = &is; // unfortunately pointer type instead of a C# reference type
return os;
}
That doesn't work though, as the address &is is invalid once I return from CreateStruct.
Is your feature request related to a problem? Please describe.
I'm frustrated when I can't write it like that but instead have to write code like the following as a workaround:
var disposers = new List<Action>();
try
{
var myStruct = CreateStruct(disposers);
PInvoke.MyMethod(..., myStruct, ...);
}
finally
{
foreach(var disposer in disposers)
disposer();
}
[...]
OUTER_STRUCT CreateStruct (List<Action> disposers)
{
INNER_STRUCT is;
// fill is
INNER_STRUCT* isPinned = GCHandle.Alloc(is, GCHandleType.Pinned);
disposers.Add(() => isPinned.Free());
OUTER_STRUCT os;
// fill os
os.Inner = (INNER_STRUCT*) is.AddrOfPinnedObject();
return os;
}
Describe alternatives you've considered
None so far.
Additional context
See also discussion around #1002 (comment)
Best regards,
D.R.
The text was updated successfully, but these errors were encountered:
Oh, those all use explicit layout unions, so either C# or the runtime will likely not allow what you're asking for. Can you try declaring the structs yourself with the proposed modifications and see if it works? You can start by copying out the CsWin32 generated ones into your own code. If you use the same namespace and type names, CsWin32 will gracefully stop emitting the types that you yourself define.
Describe the solution you'd like
I'd like to write code like the following:
That doesn't work though, as the address &is is invalid once I return from CreateStruct.
Is your feature request related to a problem? Please describe.
I'm frustrated when I can't write it like that but instead have to write code like the following as a workaround:
Describe alternatives you've considered
None so far.
Additional context
See also discussion around #1002 (comment)
Best regards,
D.R.
The text was updated successfully, but these errors were encountered: