[PW API C#] List all open datasource connections / sessions

Hi all,

I am having difficulty in creating a utility function to return a list of all open connections for an application. I can get a count of the open connections using aaApi_GetOpenDsHandles(ref dsCount); however the Pointer that is returned by the function does not seem to be accepted by aaApi_GetDatasourceNameByHandle. Perhaps this is because it is a pointer to an array of pointers since it will return multiple handles one for each datasource? Here is the code I have so far:

//Wrappers

[DllImport("dmscli.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr aaApi_GetOpenDsHandles
(
    ref int Count
);

[DllImport("dmscli.dll", CharSet = CharSet.Unicode)]
public static extern bool aaApi_GetDatasourceNameByHandle
(
    IntPtr hDataSource,
    StringBuilder StringBuffer,
    int BufferSize
);

//Code
    //Login to 2 different datasources (for testing purposes)
    PWWrapper.aaApi_LoginDlg(PWWrapper.DataSourceType.Unknown, null, 0, null, null, null);
    PWWrapper.aaApi_LoginDlg(PWWrapper.DataSourceType.Unknown, null, 0, null, null, null);
    
    int dsCount = -1;
    StringBuilder dsNames = new StringBuilder(1024);
    
    IntPtr hDataSource = PWWrapper.aaApi_GetOpenDsHandles(ref dsCount);
    
    if (dsCount > 0)
    {                
        PWWrapper.aaApi_GetDatasourceNameByHandle(hDataSource, dsNames, dsNames.Capacity);
        // Returns false - According to the SDK: "The passed handle is NULL or it does not specify a datasource"
    }
    
    bool freeDSPtrs = PWWrapper.aaApi_Free(hDataSource);
    Debug.Print(freeDSPtrs.ToString());

If I know the datasource name/s then using aaApi_GetDatasourceHandlesByName does get me the handle, however it would be useful to see a list of all open connections for this specific task.
Thanks,

Edward

  • Resolved this issue now, as expected I needed to iterate over the Pointer array. Working Code:

    public static Dictionary<string, IntPtr> GetOpenDataSourceConnections()
    {
        // Initialise variables
        Dictionary<string, IntPtr> connections = new Dictionary<string, IntPtr>();
        int dsCount = -1;
        int typeSize = Marshal.SizeOf(typeof(IntPtr));
        StringBuilder dsName = new StringBuilder(256);
    
        IntPtr hDataSource = PWWrapper.aaApi_GetOpenDsHandles(ref dsCount);
        
        for (int i = 0; i < dsCount; i++)
        {
            IntPtr dsPtr = Marshal.ReadIntPtr(hDataSource, i * typeSize);
            PWWrapper.aaApi_GetDatasourceNameByHandle(dsPtr, dsName, dsName.Capacity);
            connections.Add(dsName.ToString(), dsPtr);
        }
        return connections;
    }

    Answer Verified By: Edward Ashbolt