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
Calling the _NewEnum method on an instance of INetFwAuthorizedApplications fails with the following exception:
Unhandled exception. System.Runtime.InteropServices.InvalidOleVariantTypeException: Specified OLE variant is invalid.
at System.StubHelpers.ObjectMarshaler.ConvertToManaged(IntPtr pSrcVariant)
at Windows.Win32.NetworkManagement.WindowsFirewall.INetFwAuthorizedApplications.get__NewEnum()
at Fires.Program.Main() in C:\Users\Fots\source\Firewallcs\Program.cs:line 12
The source generated also suggests it returns an object when I think it should either be IUnknown (which can be cast to IEnumVARIANT) or IEnumVARIANT directly.
This all works perfectly in windows-rs (see below for equivalent code).
Expected behavior
I expect to be able to retrieve the _NewEnum so I can iterate through the collection.
Of course with marshaling disallowed, you have to own all COM reference counted pointers, etc. It's hard to get it right. But there's less for CsWin32 to get wrong as well, which is why this approach doesn't throw.
This looks like a bug in how we emit code when we allow marshaling. But I don't know what that bug is yet.
As a possible workaround, you can set allowMarshaling: false in NativeMethods.json and then reach the loop like this:
unsafe
{
var fwMgr = (INetFwMgr.Interface)new NetFwMgr();
INetFwAuthorizedApplications* authorizedApplications = fwMgr.LocalPolicy->CurrentProfile->AuthorizedApplications;
var pUnkApplicationsEnum = authorizedApplications->_NewEnum;
var pEnum = (IEnumVARIANT.Interface)Marshal.GetObjectForIUnknown((IntPtr)pUnkApplicationsEnum);
VARIANT v = default;
uint fetched = 0;
while (pEnum.Next(1, &v, &fetched).Succeeded)
{
}
}
Of course with marshaling disallowed, you have to own all COM reference counted pointers, etc. It's hard to get it right. But there's less for CsWin32 to get wrong as well, which is why this approach doesn't throw.
Thanks heaps, yeah I worry disabling marshalling will maybe make my other uses of the library a little less pretty. 😄
If you want to copy out the definition of the interface into your user code, CsWin32 will stop emitting it. You can then make changes to it to tinker till you find out how to make it work. If you get to that point, please share what you learn.
Actual behavior
Calling the
_NewEnum
method on an instance ofINetFwAuthorizedApplications
fails with the following exception:The source generated also suggests it returns an
object
when I think it should either beIUnknown
(which can be cast toIEnumVARIANT
) orIEnumVARIANT
directly.This all works perfectly in windows-rs (see below for equivalent code).
Expected behavior
I expect to be able to retrieve the
_NewEnum
so I can iterate through the collection.Repro steps
NativeMethods.txt
content:NativeMethods.json
content (if present):N/A
Here's the equivalent in Rust...
Cargo.toml
main.rs
Context
0.4.422-beta
]: 0.3.106netstandard2.0
]: net8.0-windowsLangVersion
(if explicitly set by project): [e.g.9
]: N/AThanks a lot in advance!
Fotis
The text was updated successfully, but these errors were encountered: