views:

450

answers:

2

Is there any way to generate a new GUID from inside an INNO Installer script? I'm not after a new AppId, just want to get some GUID text.

Regards

Tris

+1  A: 

You can do this by calling the Windows API function CoCreateGuid (in "OLE32.dll"), which you declare inside a [Code] section in your script. Sorry, I haven't used Inno Setup in awhile so I don't know exactly how to do this. To help, here's a sample API declaration for the GetWindow() function:

Function GetWindow (HWND: Longint; uCmd: cardinal): Longint;
    external '[email protected] stdcall';

And here's a link to a VB sample for using CoCreateGuid:

http://support.microsoft.com/kb/176790

Somewhere in all of this is your answer.

MusiGenesis
giorgian's answer shows how to do this in Inno Setup.
MusiGenesis
+4  A: 

Found this on innosetup newsgroup archive:

http://news.jrsoftware.org/news/innosetup/msg76234.html

I haven't tested it.

[Code]

type
  TGUID = record
    D1: LongWord;
    D2: Word;
    D3: Word;
    D4: array[0..7] of Byte;
  end;

function CoCreateGuid(var Guid:TGuid):integer;
 external '[email protected] stdcall';

function inttohex(l:longword; digits:integer):string;
var hexchars:string;
begin
 hexchars:='0123456789ABCDEF';
 setlength(result,digits);
 while (digits>0) do begin
  result[digits]:=hexchars[l mod 16+1];
  l:=l div 16;
  digits:=digits-1;
 end;
end;

function GetGuid(dummy:string):string;
var Guid:TGuid;
begin
  if CoCreateGuid(Guid)=0 then begin
  result:='{'+IntToHex(Guid.D1,8)+'-'+
           IntToHex(Guid.D2,4)+'-'+
           IntToHex(Guid.D3,4)+'-'+
           IntToHex(Guid.D4[0],2)+IntToHex(Guid.D4[1],2)+'-'+
           IntToHex(Guid.D4[2],2)+IntToHex(Guid.D4[3],2)+
           IntToHex(Guid.D4[4],2)+IntToHex(Guid.D4[5],2)+
           IntToHex(Guid.D4[6],2)+IntToHex(Guid.D4[7],2)+
           '}';
  end else
    result:='{00000000-0000-0000-0000-000000000000}';
end;

function InitializeSetup(): Boolean;
begin
  MsgBox(GetGuid(''), mbInformation, MB_OK);
  Result := False;
end;
giorgian
That's what I was looking for.
MusiGenesis
Also, there's another win32 API call that converts the raw GUID from CoCreateGuid() into a string representation in the format {00000000-0000-0000-0000-000000000000}, which would greatly simplify the above function, but I don't remember what it's named.
MusiGenesis
StringFromCLSID is the second win32 API call to use for this, also from ole32.dll.
MusiGenesis