dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.81k stars 196 forks source link

NetworkListManager does not work to receive events in .Net MAUI project #484

Closed NilsHoyer closed 2 months ago

NilsHoyer commented 2 months ago

I'm trying to catch the events of the NetworkListManager, and it works fine for me in a simple .Net Core 8.0 project. But not for a .Net MAUI project.

Can you imagine, that this is a problem of your code? If not, I will try to find a Microsoft member to take a look at this.

My code:

internal class MyClass : INetworkConnectionCostEvents
{
    public void Initialize()
    {
        var networkListManager = new NetworkListManager();

        var guid = typeof(INetworkConnectionCostEvents).GUID;
        ((IConnectionPointContainer)networkListManager).FindConnectionPoint(ref guid, out var connectionPoint);
        connectionPoint.Advise(this, out var cookie);
    }

    public HRESULT ConnectionCostChanged(Guid connectionId, NLM_CONNECTION_COST newCost)
    {
        return 0;
    }

    public HRESULT ConnectionDataPlanStatusChanged(Guid connectionId)
    {
        return 0;
    }
}

Both methods will never executed.

dahall commented 2 months ago

That is curious. There is nothing too special about this implementation. Threading model? COM initialization? Annotating the class with [ComVisible(true)] maybe?

NilsHoyer commented 2 months ago

[ComVisible(true)] and CoInitializeEx() doesn't help.

Now I will try to get the INetwirkLostManager object on the recommended way:

INetworkListManager *pNetworkListManager = NULL; 
HRESULT hr = CoCreateInstance(CLSID_NetworkListManager, NULL,
        CLSCTX_ALL, IID_INetworkListManager,
        (LPVOID *)&pNetworkListManager);

If this doesn't work, I will mention this issue to the MAUI developers.

NilsHoyer commented 2 months ago

I didn't find a solution, so I will inform the MAUI project about this issue.