Generating GUIDs

  • Revision slug: Generating_GUIDs
  • Revision title: Generating GUIDs
  • Revision id: 85426
  • Created:
  • Creator: Biesi
  • Is current revision? No
  • Comment /* Canonical form */ grammar error (is->are)

Revision Content

GUIDs are used in Mozilla programming for identifying several types of entities, including XPCOM Interfaces (this type of GUIDs is callled IID), components (CID), and add-ons, like extensions and themes, although add-ons can (and should) be identified with IDs of form <tt>extensionname@organization.tld</tt> since Firefox 1.5.

Canonical form

The common form of a GUID is <tt>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx</tt>, where each <tt>x</tt> stands for a hexadecimal digit. There are a number of tools that can be used to generate a GUID in the canonical form.

Online tools

Windows

Windows users can use the GuidGen tool from Microsoft to obtain a GUID. (This tool is also part of MS Visual C++)

Linux

Use /usr/bin/uuidgen. This can be found in package libuuid1 (Debian).

Perl

jkeiser's Mozilla tools include a UUID generator with output format of both C++ and IDL style.

nsIUUIDGenerator

A UUID can be generated from privileged Mozilla code using nsIUUIDGenerator. See the linked page for details.

Mozilla-specific format

When #define-ing IIDs and CIDs in Mozilla C++ code, you generally use the following format:

// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
#define NS_...ID \
{ 0xXXXXXXXX, 0xXXXX, 0xXXXX, \
  { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX } }

You can generate code in this format using one of the following tools.

Online tools

  • http://db.cs.helsinki.fi/~pettay/cgi-bin/mozuuid.pl

bash

You can put the following into your <tt>.bashrc</tt> file:

uuidgen-c++()
{
       local UUID=$(uuidgen)
       echo "// $UUID"
       echo "#define NS__IID \\"
       echo "{ 0x${UUID:0:8}, 0x${UUID:9:4}, 0x${UUID:14:4}, \\"
       echo -n "  { 0x${UUID:19:2}, 0x${UUID:21:2}, 0x${UUID:24:2}, "
       echo -n "0x${UUID:26:2}, 0x${UUID:28:2}, 0x${UUID:30:2}, "
       echo "0x${UUID:32:2}, 0x${UUID:34:2} } }"
}

Perl

#!/usr/bin/perl
$uuid = `uuidgen`;
chomp $uuid;
print $uuid, "\n";
@parts = ($uuid =~ /^(.{8})-(.{4})-(.{4})-(..)(..)-(..)(..)(..)(..)(..)(..)$/);
print "{ 0x$parts[0], 0x$parts[1], 0x$parts[2], \\", "\n", " { ";
for (3 .. 9) {
    print "0x$parts[$_], ";
}
print "0x$parts[10] } }", "\n";
{{ wiki.languages( { "fr": "fr/G\u00e9n\u00e9ration_de_GUID", "ja": "ja/Generating_GUIDs", "pl": "pl/Generowanie_GUID" } ) }}

Revision Source

<p><b>GUID</b>s are used in Mozilla programming for identifying several types of entities, including XPCOM <a href="en/Interfaces">Interfaces</a> (this type of GUIDs is callled IID), components (CID), and add-ons, like <a href="en/Extension">extensions</a> and <a href="en/Themes">themes</a>, although add-ons <a href="en/Install_Manifests#id">can (and should) be identified</a> with IDs of form <tt>extensionname@organization.tld</tt> since <a href="en/Firefox_1.5">Firefox 1.5</a>.
</p>
<h3 name="Canonical_form"> Canonical form </h3>
<p>The common form of a GUID is <tt>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx</tt>, where each <tt>x</tt> stands for a hexadecimal digit. There are a number of tools that can be used to generate a GUID in the canonical form.
</p>
<h4 name="Online_tools">Online tools</h4>
<ul><li> <a class="external" href="http://www.somacon.com/p113.php">Generate GUID Online</a>
</li><li> <a class="external" href="http://www.famkruithof.net/uuid/uuidgen">UUID (GUID) Generator on the WEB</a>
</li><li> You can get a GUID from one of the bots (such as botbot, firebot) on <a class="external" href="irc://irc.mozilla.org/mozilla">#mozilla</a> IRC channel by <tt>/msg</tt>ing "uuid" to them.
</li></ul>
<h4 name="Windows">Windows</h4>
<p>Windows users can use the <a class="external" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=94551F58-484F-4A8C-BB39-ADB270833AFC&amp;displaylang=en">GuidGen</a> tool from Microsoft to obtain a GUID. (This tool is also part of MS Visual C++)
</p>
<h4 name="Linux">Linux</h4>
<p>Use <code>/usr/bin/uuidgen</code>. This can be found in package <code>libuuid1</code> (Debian).
</p>
<h4 name="Perl">Perl</h4>
<p><a class="external" href="http://www.johnkeiser.com/mozilla/mozilla_tools.html">jkeiser's Mozilla tools</a> include a UUID generator with output format of both C++ and IDL style.
</p>
<h4 name="nsIUUIDGenerator">nsIUUIDGenerator</h4>
<p>A UUID can be generated from privileged Mozilla code using <code><a href="en/NsIUUIDGenerator">nsIUUIDGenerator</a></code>. See the linked page for details.
</p>
<h3 name="Mozilla-specific_format"> Mozilla-specific format </h3>
<p>When <code>#define</code>-ing IIDs and CIDs in Mozilla C++ code, you generally use the following format:
</p>
<pre class="eval">// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx
#define NS_...ID \
{ 0xXXXXXXXX, 0xXXXX, 0xXXXX, \
  { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX } }
</pre>
<p>You can generate code in this format using one of the following tools.
</p>
<h4 name="Online_tools_2"> Online tools </h4>
<ul><li> http://db.cs.helsinki.fi/~pettay/cgi-bin/mozuuid.pl
</li></ul>
<h4 name="bash"> bash </h4>
<p>You can put the following into your <tt>.bashrc</tt> file:
</p>
<pre>uuidgen-c++()
{
       local UUID=$(uuidgen)
       echo "// $UUID"
       echo "#define NS__IID \\"
       echo "{ 0x${UUID:0:8}, 0x${UUID:9:4}, 0x${UUID:14:4}, \\"
       echo -n "  { 0x${UUID:19:2}, 0x${UUID:21:2}, 0x${UUID:24:2}, "
       echo -n "0x${UUID:26:2}, 0x${UUID:28:2}, 0x${UUID:30:2}, "
       echo "0x${UUID:32:2}, 0x${UUID:34:2} } }"
}
</pre>
<h4 name="Perl_2"> Perl </h4>
<pre>#!/usr/bin/perl
$uuid = `uuidgen`;
chomp $uuid;
print $uuid, "\n";
@parts = ($uuid =~ /^(.{8})-(.{4})-(.{4})-(..)(..)-(..)(..)(..)(..)(..)(..)$/);
print "{ 0x$parts[0], 0x$parts[1], 0x$parts[2], \\", "\n", " { ";
for (3 .. 9) {
    print "0x$parts[$_], ";
}
print "0x$parts[10] } }", "\n";
</pre>
{{ wiki.languages( { "fr": "fr/G\u00e9n\u00e9ration_de_GUID", "ja": "ja/Generating_GUIDs", "pl": "pl/Generowanie_GUID" } ) }}
Revert to this revision