Re: C# bindings (Was: First patch)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



?Hi Justin,

I have written a little simple sample of using of C# bindings. In fact it is a sample project which connect to a clear TCP libvirt/qemu via virConnectOpen the list the storage pools. Here's the code :

/*
* Copyright (C)
*   Arnaud Champion <arnaud.champion@xxxxxxxxxx>
*   Jaromír Cervenka <cervajz@xxxxxxxxxxx>
*
* See COPYING.LIB for the License of this software
*
* Sample code for :
* Function :
*      virConnectOpen
*      virConnectNumOfStoragePools
*      virConnectListStoragePools
*/

using System;
using System.Windows.Forms;
using LibvirtBindings;

namespace virConnectOpen
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void btnConnect_Click(object sender, EventArgs e)
       {
           IntPtr conn = virConnect.Open(tbURI.Text);
           if (conn != IntPtr.Zero)
           {
               int numOfStoragePools = virConnect.NumOfStoragePools(conn);
               if (numOfStoragePools == -1)
                   ShowError("Unable to get the number of storage pools");
               string[] storagePoolsNames = new string[numOfStoragePools];
int listStoragePools = virConnect.ListStoragePools(conn, ref storagePoolsNames, numOfStoragePools);
               if (listStoragePools == -1)
                   ShowError("Unable to list storage pools");
               foreach (string storagePoolName in storagePoolsNames)
                   lbStoragePool.Items.Add(storagePoolName);
           }
           else
           {
               ShowError("Unable to connect");
           }
       }

       private void ShowError(string message)
       {
MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
   }
}


Also, the xls has some little change, you'll find the update joined to this email.

Regards,

Arnaud

--------------------------------------------------
From: <arnaud.champion@xxxxxxxxxx>
Sent: Tuesday, October 19, 2010 9:59 PM
To: "Justin Clift" <jclift@xxxxxxxxxx>
Cc: "Libvirt Developers Mailing List" <libvir-list@xxxxxxxxxx>
Subject: Re:  C# bindings (Was:  First patch)

?Well, I can make a little sample, with virConnectOpen instead of virConnectOpenAuth, because virConnectOpenAuth cannot work without callback handling, that's why it's larger. But I will be tomorrow ;) it's time to sleep for me.

--------------------------------------------------
From: "Justin Clift" <jclift@xxxxxxxxxx>
Sent: Tuesday, October 19, 2010 9:46 PM
To: "Arnaud Champion" <arnaud.champion@xxxxxxxxxx>
Cc: "Libvirt Developers Mailing List" <libvir-list@xxxxxxxxxx>
Subject: Re:  C# bindings (Was:  First patch)

On 10/20/2010 05:08 AM, Justin Clift wrote:
On 10/20/2010 04:57 AM, Eric Blake wrote:
<snip>
Ok, how's this?

http://justinclift.fedorapeople.org/csharp/csharp.html

Can we also include a sample program using the bindings, as is done in
http://justinclift.fedorapeople.org/csharp/python.html and
http://justinclift.fedorapeople.org/csharp/java.html?

Good idea. Just added a space for one in the layout.

Arnaud, would I be able to get an example program from you, for adding
there? Is "virConnectOpenAuth" the correct kind of thing?

Looked at the virConnectOpenAuth example (from Arnauds patches waiting
for review).  It doesn't seem like a suitable example for the web page,
as the example code is a lot longer/larger than we'd like for this.

Arnaud, do you have time to create an extremely simple example?

Something that shows the very minimum needed.  Sort of like this:
(I do not know C# at all though :>)

  using System;
  using LibvirtBindings;

  // Fill a virConnectAuth structure
  virConnectAuth auth = new virConnectAuth
  {
    cbdata = authData,  // The authData structure
    cb = AuthCallback,  // the method called by callbacks
    CredTypes = new[]
                    {
                      // The list of credentials types
                      virConnectCredentialType.VIR_CRED_AUTHNAME,
                      virConnectCredentialType.VIR_CRED_PASSPHRASE
                    }
  };

  // Request the connection
  IntPtr conn = libVirt.virConnectOpenAuth(tbURI.Text, ref auth, 0);

  // Get the pointer to the domain
  string domainName = new string "MyGuest";
  IntPtr domainPtr = libVirt.virDomainLookupByName(conn, domainName);

  // Start the domain
  Int something = libVirt.virDomainCreate(domainPtr);

  // Close the connection
  libVirt.virConnectClose(conn);

?

Regards and best wishes,

Justin Clift


--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

Attachment: Bindings progress 20101020.xls
Description: MS-Excel spreadsheet

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]