private static int OpenAuthCallbackFromUnmanaged(IntPtr creds, uint ncreds, IntPtr cbdata) { // Give back the structure that hold cbdata and the callback target OpenAuthManagedCB cbAndUserData = (OpenAuthManagedCB)Marshal.PtrToStructure(cbdata, typeof(OpenAuthManagedCB)); int offset = 0; int credIndex = 0; ConnectCredential[] cc = new ConnectCredential[ncreds]; // Loop thru credentials and initialize the ConnectCredential array while (credIndex < ncreds) { IntPtr currentCred = MarshalHelper.IntPtrOffset(creds, offset); ConnectCredential cred = (ConnectCredential)Marshal.PtrToStructure(currentCred, typeof(ConnectCredential)); offset += Marshal.SizeOf(cred); cc[credIndex] = cred; credIndex++; } // Call the delegate with the ConnectCredential array, this allow the user to answer the result cbAndUserData.cbManaged(ref cc, cbAndUserData.cbdata); offset = 0; credIndex = 0; // Loop thru ConnectCredential array and copy back to unmanaged memory while (credIndex < ncreds) { IntPtr currentCred = MarshalHelper.IntPtrOffset(creds, offset); Marshal.StructureToPtr(cc[credIndex], currentCred, true); offset += Marshal.SizeOf(cc[credIndex]); credIndex++; } return(0); }
/// <summary> /// This function should be called first to get a connection to the Hypervisor. If necessary, authentication will be performed fetching credentials via the callback See virConnectOpen for notes about environment variables which can have an effect on opening drivers /// </summary> /// <param name="name">URI of the hypervisor</param> /// <param name="auth">Authenticate callback parameters</param> /// <param name="flags">Open flags</param> /// <returns>a pointer to the hypervisor connection or NULL in case of error URIs are documented at http://libvirt.org/uri.html </returns> public static IntPtr OpenAuth(string name, ref ConnectAuth auth, int flags) { // Create a structure that hold cbdata and the callback target OpenAuthManagedCB cbAndUserData = new OpenAuthManagedCB(); cbAndUserData.cbdata = auth.cbdata; cbAndUserData.cbManaged = auth.cb; // Pass the structure as cbdata IntPtr cbAndUserDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cbAndUserData)); Marshal.StructureToPtr(cbAndUserData, cbAndUserDataPtr, true); // Create the real ConnectAuth structure, it will call OpenAuthCallbackFromUnmanaged via callback ConnectAuthUnmanaged connectAuth = new ConnectAuthUnmanaged(); connectAuth.cbdata = cbAndUserDataPtr; connectAuth.cb = OpenAuthCallbackFromUnmanaged; connectAuth.CredTypes = auth.CredTypes; return(OpenAuth(name, ref connectAuth, flags)); }
/// <summary> /// This function should be called first to get a connection to the Hypervisor. If necessary, authentication will be performed fetching credentials via the callback See virConnectOpen for notes about environment variables which can have an effect on opening drivers /// </summary> /// <param name="name">URI of the hypervisor</param> /// <param name="auth">Authenticate callback parameters</param> /// <param name="flags">Open flags</param> /// <returns>a pointer to the hypervisor connection or NULL in case of error URIs are documented at http://libvirt.org/uri.html </returns> public static IntPtr OpenAuth(string name, ref ConnectAuth auth, int flags) { // Create a structure that hold cbdata and the callback target OpenAuthManagedCB cbAndUserData = new OpenAuthManagedCB(); cbAndUserData.cbdata = auth.cbdata; cbAndUserData.cbManaged = auth.cb; // Pass the structure as cbdata IntPtr cbAndUserDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cbAndUserData)); Marshal.StructureToPtr(cbAndUserData, cbAndUserDataPtr, true); // Create the real ConnectAuth structure, it will call OpenAuthCallbackFromUnmanaged via callback ConnectAuthUnmanaged connectAuth = new ConnectAuthUnmanaged(); connectAuth.cbdata = cbAndUserDataPtr; connectAuth.cb = OpenAuthCallbackFromUnmanaged; connectAuth.CredTypes = auth.CredTypes; return OpenAuth(name, ref connectAuth, flags); }