VirtualBox

Ignore:
Timestamp:
Jan 29, 2014 11:21:48 AM (11 years ago)
Author:
vboxsync
Message:

doc/manual/en_US/SDKRef.xml: document more C binding conversion details, and add a section about event handling in C API clients

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/manual/en_US/SDKRef.xml

    r50262 r50267  
    17761776          to correctly use the C binding, as it is vital for developing API
    17771777          client code which manages memory correctly, updates the reference
    1778           counters correctly, avoiding crashes and memory leaks. Concepts such
    1779           as event handling are described in the API specification (see
    1780           <xref linkend="events" />) in great detail detail, and the sample
    1781           illustrates the practical aspects of how to use both types of event
    1782           handling, active and passive, from a C application. If you look at
    1783           the code complexity of active event handling (and its inherenly
    1784           platform/compiler specific elements) it should be clear that passive
    1785           event handling should be used whereever possible.</para>
     1778          counters correctly, avoiding crashes and memory leaks. Often API
     1779          clients need to handle events, so the C API specifics are also
     1780          described below.</para>
    17861781        </sect3>
    17871782
     
    20172012        </sect3>
    20182013
     2014        <sect3 id="c-eventhandling">
     2015          <title>Event handling</title>
     2016
     2017          <para>The VirtualBox API offers two types of event handling, active
     2018          and passive, and consequently there is support for both with the
     2019          C API binding. Active event handling (based on asynchronous
     2020          callback invocation for event delivery) is more difficult, as it
     2021          requires the construction of valid C++ objects in C, which is
     2022          inherently platform and compiler dependent. Passive event handling
     2023          is much simpler, it relies on an event loop, fetching events and
     2024          triggering the necessary handlers explicitly in the API client code.
     2025          Both approaches depend on an event loop to make sure that events
     2026          get delivered in a timely manner, with differences what exactly needs
     2027          to be done.</para>
     2028
     2029          <para>The C API sample contains code for both event handling styles,
     2030          and one has to modify the appropriate <code>#define</code> to select
     2031          which style is actually used by the compiled program. It allows a
     2032          good comparison between the two variants, and the code sequences are
     2033          probably worth reusing without much change in other API clients
     2034          with only minor adaptions.</para>
     2035
     2036          <para>Active event handling needs to ensure that the following helper
     2037          function is called frequently enough in the primary thread:
     2038          <screen>g_pVBoxFuncs->pfnProcessEventQueue(cTimeoutMS);</screen></para>
     2039
     2040          <para>The actual event handler implementation is quite tedious, as
     2041          it has to implement a complete API interface. Especially on Windows
     2042          it is a lot of work to implement the complicated <code>IDispatch</code>
     2043          interface, requiring to load COM type information and using it
     2044          in the <code>IDispatch</code> method implementation. Overall this is
     2045          quite tedious compared to passive event handling.</para>
     2046
     2047          <para>Passive event handling uses a similar event loop structure,
     2048          which requires calling the following function in a loop, and
     2049          processing the returned event appropriately:
     2050          <screen>rc = IEventSource_GetEvent(pEventSource, pListener, cTimeoutMS, &amp;pEvent);</screen></para>
     2051
     2052          <para>After processing the event it needs to be marked as processed
     2053          with the following method call:
     2054          <screen>rc = IEventSource_EventProcessed(pEventSource, pListener, pEvent);</screen></para>
     2055
     2056          <para>This is vital for vetoable events, as they would be stuck
     2057          otherwise, waiting whether the veto comes or not. It does not do any
     2058          harm for other event types, and in the end is cheaper than checking
     2059          if the event at hand is vetoable or not.</para>
     2060
     2061          <para>The general event handling concepts are described in the API
     2062          specification (see <xref linkend="events" />), including how to
     2063          aggregate multiple event sources for processing in one event loop.
     2064          As mentioned, the sample illustrates the practical aspects of how to
     2065          use both types of event handling, active and passive, from a C
     2066          application. Additional hints are in the comments documenting
     2067          the helper methods in <computeroutput>VBoxCAPI_v4_3.h</computeroutput>.
     2068          The code complexity of active event handling (and its inherenly
     2069          platform/compiler specific aspects) should be motivation to use
     2070          passive event handling whereever possible.</para>
     2071        </sect3>
     2072
    20192073        <sect3 id="c-uninitialization">
    20202074          <title>C API uninitialization</title>
     
    21342188          safety in case of an error in the source code.</para>
    21352189
     2190          <para>To gloss over the platform differences, API client code should
     2191          no longer rely on XPCOM specific interface names such as
     2192          <code>nsISupports</code>, <code>nsIException</code> and
     2193          <code>nsIEventQueue</code>, and replace them by the platform
     2194          independent interface names <code>IUnknown</code> and
     2195          <code>IErrorInfo</code> for the first two respectively. Event queue
     2196          handling should be replaced by using the platform independent way
     2197          described in <xref linkend="c-eventhandling" />.</para>
     2198
    21362199          <para>Finally adjust the string and array handling to use the new
    21372200          helpers, as these make sure the code works without changes with
     
    21512214          <para>Starting with version 2.2, VirtualBox offers a C binding for
    21522215          its API which works only on platforms using XPCOM. Refer to the
    2153           old SDK documentation, it still applies unchanged. The fundamental
    2154           concepts are similar (but the syntactical details are quite
    2155           different) to the newer cross-platform C binding which should be
    2156           used for all new code, as the support for the old C binding will go
    2157           away in a major release after version 4.3.</para>
     2216          old SDK documentation (included in the SDK packages for version 4.3.6
     2217          or earlier), it still applies unchanged. The fundamental concepts are
     2218          similar (but the syntactical details are quite different) to the
     2219          newer cross-platform C binding which should be used for all new code,
     2220          as the support for the old C binding will go away in a major release
     2221          after version 4.3.</para>
    21582222        </sect3>
    21592223      </sect2>
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette