Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much


by Rudif (Hermit)
on Dec 21, 2001 at 02:51 UTC ( #133647=sourcecode: print w/ replies, xml ) Need Help??

Category: Win32 Stuff
Author/Contact Info Rudi Farkas
Description: If you are Windoze COM programmer, you may find use for this guid generator, perhaps when you clone an existing MSVC project and you need to change the guids therein.

Requires Win32-API.

Update: generate() now returns one guid in scalar context, array of one or more guids in array context.

Update 2: Thank you $code or die for pointing out the GuidGen() function in libwin32-018.
I just installed ActivePerl build 630 and tried GuidGen() - after some searching. I could not find it mentioned in the new chm documentation. Attempt to call Win32::GuidGen() as you suggested produces an error "Undefined subroutine &Win32::GuidGen called at ...". Text search for GuidGen in C:\Perl produced a few examples of calls via package Win32::Resource, which does not seem to be documented either, but following these examples worked for me:

#!perl -w use strict; #my $guid = Win32::GuidGen(); #Undefined subroutine &Win32::GuidGen called at line 3. use Win32::Resource; my $guid2 = Win32::Resource::GuidGen(); print "$guid2\n"; __END__ {A8D7B8DA-1AA8-4575-A51F-8EA05E93D032}
However, Win32::Resource::GuidGen() does not seem to support an optional argument for generating a list of several guids at a time.


#! perl -w

package Win32::API::Guid;

require 5.005_62;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not expo
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration    use Win32::API::Guid ':all';
# If you do not need this, moving things directly into @EXPORT or @EXP
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(    generate ) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( );
our $VERSION = '0.02';

use Win32::API;
my $CoCreateGuid = new Win32::API( "ole32.dll", "CoCreateGuid", [ 'P' 
+], 'N' );

sub generate {
    my ($n) = @_;
    $n = 1 if (!defined($n) || $n < 1);
    my $guid = ' ' x 16;
    my $hr = $CoCreateGuid->Call( $guid );
    my ($guidData1, $guidData2, $guidData3, @guidData4) = unpack("LS2C
+8", $guid);
    my @guids;
    for (my $i = 0; $i < $n; ++$i)    {
            my $guidstr = sprintf(
                $guidData1, $guidData2, $guidData3, 
                $guidData4[0], $guidData4[1], $guidData4[2], $guidData
                $guidData4[4], $guidData4[5], $guidData4[6], $guidData
            push @guids, $guidstr;
    return wantarray? @guids : $guids[0];



Win32::Guid - 
Perl extension to generate a GUID (or UUID) for use in COM or RPC


  use Win32::Guid 'generate';
  my @guids = generate(3);
  my $guid = generate;


This module provides a simple Win32::API wrapper for the COM function,
CoCreateGuid().  The module contains one function, generate(),
which returns 36-character long strings containing GUIDs.  These
look something like:


The parameter indicates the number of GUIDs to generate (the default i
+s 1). 

In scalar context, returns a single guid, while in array context it 
returns the specified number guids in an array.

These can be used in a MIDL file for class, interface, or library
declarations, or in various other places related to COM.

=head1 INSTALL

Prerequisite: module Win32-API.
Simply copy this file to C:\Perl\site\lib\Win32\API\

=head1 AUTHOR

Rudi Farkas,

=head1 SEE ALSO

CoCreateGuid() in Win32 API documentation.

Win32::Guid module by Ken Bandes,

"Imitation is the sincerest form of flattery"
This module is functionally equivalent to Ken's, 
only using the Win32::API rather than an xs-generated dll.


(c) 2001 Rudi Farkas. This file is Free Software; 
you can redistribute it and/or modify it under the same terms as Perl 



#! perl -w

######################### We start with some black magic to print on f
BEGIN { $| = 1; print "1..3\n"; }
END {print "not ok 1\n" unless $loaded;}
use Win32::API::Guid 'generate';
$loaded = 1;
print "ok 1\n";
######################### End of black magic.

    local $" = "\n";
    @a = generate();
    print "@a\n";
    print scalar(@a) == 1 ? "ok" : "not ok", " 2\n";
    @a = generate(5);
    print "@a\n";
    print scalar(@a) == 5 ? "ok" : "not ok", " 3\n";

Comment on Win32-API-Guid
Select or Download Code
Replies are listed 'Best First'.
Re: Win32-API-Guid
by $code or die (Deacon) on Dec 22, 2001 at 02:25 UTC
      it gives me an error : > 1..3Can't locate Win32/ in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib . ) at C:/Perl/site/lib/Win32/API/ line 25. BEGIN failed--compilation aborted at C:/Perl/site/lib/Win32/API/ line 25. where can i find ?btw i have had to create C:/Perl/site/lib/Win32/API/ cause this folder didnt exist. Thats a totally mess !!!

        Win32::API should be installed on your system. Are you using ActiveState Perl?

        ___ Simon Flack ($code or die)
        $,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
        =~y'_"' ';eval"die";print $_,lc substr$@,0,3;

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://133647]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2015-10-04 03:24 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (98 votes), past polls