Community Forums
Results 1 to 3 of 3

Thread: Create GUID in InstallScript

  1. #1
    Join Date
    Jul 2007
    Posts
    13

    Exclamation Create GUID in InstallScript

    Hi,

    I have a basic msi project where i want to create a new guid every time an installation is done. I want to do this in a custom action in installscript but could not find any related function. could someone pl guide me on the options available to accomplish this.

    The guid i want to generate has nothing to do with the installation project related guids (product id, comp id etc). It is going to be used primariliy by my application.

    regards
    d2

  2. #2
    Join Date
    Jul 2003
    Location
    North Kingstown, RI, USA
    Posts
    1,356
    There is no internal function to create a GUID. This is something I put together that does the job, but I have not cleaned it up here for public consumption, so it is still product specific.

    Code:
    	// Declare block
    	prototype OLE32.CoCreateGuid(POINTER);
    	prototype OLE32.StringFromGUID2(POINTER, BYREF STRING, LONG);
    
    	typedef GUID
    	begin
    		LONG Data1;
    		SHORT Data2;
    		SHORT Data3;
    		STRING  Date4[8];
    	end;
    
    ///////////////////////////////////////////////////////////////////////////////
    //
    // Function:  CreateStringGUID
    //
    //  Purpose:  This function creates a string GUID using
    //            calls to OLE32.DLL
    //
    ///////////////////////////////////////////////////////////////////////////////
    function CreateStringGUID(hMsi) 
    
    NUMBER nResult, cbMax, i, nSize;
    GUID gGuid;
    GUID POINTER pGuid;
    STRING lpszGuid[64], strGuid, svPropVal, szMsg, szDLL;  
    
    begin
    	// Initiate some things first
    	pGuid = &gGuid;			// Set the pointer to the address of gGuid
    	gGuid.Data1 = 0;
    	gGuid.Data2 = 0;
    	gGuid.Data3 = 0;
    	szDLL = "OLE32.dll";  //Probably do not have to initiate OLE32 ....
    	if UseDLL(WINSYSDIR ^ szDLL) < 0 then		// File doesn't exist.
    		SprintfMsiLog("Unable to open OLE32.DLL.Can't find file!");
    		//Forcing return of ERROR_SUCCESS because this function is not 
    		//critical to the operation of the application. GUID can be set 
    		//through netlink. This allows setup to continue. 
    		return ERROR_SUCCESS;
    	endif;
    
    	// Call CoCreateGuid from OLE32.DLL and get the GUID as pGuid
    	nResult = CoCreateGuid(pGuid);
    	if nResult != S_OK then
    		SprintfMsiLog("Call to OLE32.DLL failed - CoCreateGuid()");
    		//Forcing return of ERROR_SUCCESS because this function is not 
    		//critical to the operation of the application. GUID can be set 
    		//through netlink. This allows setup to continue. 
    		return ERROR_SUCCESS;
    	endif;
        
        nSize = SizeOf(lpszGuid);
    	// Call StringFromGUID2 from OLE32.DLL and change pGuid into a string - strGuid
    	nResult = StringFromGUID2(pGuid, lpszGuid, nSize-1);
    	if nResult = 0 then 
    		//Buffer size is too small, resize buffer  
    		Resize(lpszGuid, SizeOf(lpszGuid)*2);
    		SprintfMsiLog("Call to OLE32.DLL failed - StringFromGUID2()"); 
    		nSize = SizeOf(lpszGuid);
    		nResult = StringFromGUID2(pGuid, lpszGuid, nSize-1);
    		if nResult = 0 then
    			szMsg = "Unable to convert GUID to string value. Please contact Technical Support. Buffer was too small. Size: &#37;d"; 
    		//Forcing return of ERROR_SUCCESS because this function is not 
    		//critical to the operation of the application. GUID can be set 
    		//through netlink. This allows setup to continue. 
    			SprintfMsiLog(szMsg, nSize);
    			return ERROR_SUCCESS;
    		endif;
    	endif;
    
    	// Change the unicode string, strGuid, into an ANSI string
    	Resize(strGuid, nResult);
    	nResult = WideCharToMultiByte(
    		  CP_ACP,            /* code page*/
    		  0,            /* performance and mapping flags*/
    		  lpszGuid,    /* wide-character string*/
    		  nResult-1,          /* number of chars in string*/
    		  strGuid,     /* buffer for new string*/
    		  nResult,          /* size of buffer */
    		  0,     /* default for unmappable chars */
    		  0  /* set when default char used*/
    		);
    	
    	
    	
    	SprintfMsiLog("strGuid Value post terminator: %s", strGuid); 
    	
    	if( nResult > 0 ) 
    	then
        	nResult = MsiSetProperty(hMsi, "PUBLIC_PROPERTY", strGuid);
    	endif;
    	    
    	nResult = UnUseDLL("OLE32.dll");
    
    end;
    Last edited by DLee65; 07-12-2007 at 12:38 PM. Reason: Cleaned up code a bit
    Regards,
    Dan Lee

    Release Engineer

  3. #3
    Join Date
    Jun 2018
    Posts
    1
    Tidied the code provided by DLee65.

    Code:
    typedef GUID
    begin
    	LONG Data1;
    	SHORT Data2;
    	SHORT Data3;
    	CHAR Data4(8);
    end;
    
    // Creates a GUID structure using ole32.dll library.
    prototype OLE32.CoCreateGuid(GUID POINTER);
    
    // Converts a GUID structure to string using ole32.dll library.
    prototype OLE32.StringFromGUID2(GUID POINTER, BYREF WSTRING, LONG);
    
    // Returns a GUID string in the format "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" using ole32.dll.
    prototype STRING CreateStringGUID();
    
    function STRING CreateStringGUID()
    	GUID gGuid;
    	WSTRING svGuid[39]; // GUID length with null terminator
    	NUMBER nResult;
    begin
    	nResult = UseDLL(WINSYSDIR ^ "ole32.dll");
    	if nResult < ISERR_SUCCESS then
    		SprintfMsiLog("Couldn't load ole32.dll. Error code: %d", nResult);
    		abort;
    	endif;
    	
    	nResult = CoCreateGuid(&gGuid);
    	if nResult < ISERR_SUCCESS then
    		SprintfMsiLog("Couldn't generate GUID. Error code: %d", nResult);
    		abort;
    	endif;
    
    	nResult = StringFromGUID2(&gGuid, svGuid, SizeOf(svGuid));
    	if nResult = 0 then
    		SprintfMsiLog("Couldn't convert GUID to a string value. Error code: %d", nResult);
    		abort;
    	endif;
    
    	return svGuid;
    end;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •