CLR GUID

Started by stanl, June 29, 2013, 08:17:55 AM

Previous topic - Next topic

stanl

Just trying to get some more practice. The code below doesn't fail, but obviously not the correct way to go about creating a GUID from .NET
I wasn't able to implement a System.GUID class so I went the CLRType route. Most of the code I looked at seemed to imply NewGuid().Guid.ToString() was the way to go, but I don't see how to go about calling the method.


Code (WINBATCH) Select

;***************************************************************************
;** Reverse GUID
;**
;** Reference:
;**       REQUIRES WinBatch 2013A or newer & CLR version 4.0
;**
;** Stan LittleField, June 29,2013
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

ObjectClrOption( 'use', 'System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' )
oGUID = ObjectClrType('System.GUID','Guid')  ;having trouble with 2nd parameter
;cGUID = oGUID.NewGuid().Guid

Message("",ObjectTypeGet(oGUID))

;code to reverse the GUID


oGUID=0

Exit

td

Code (winbatch) Select

objectClrOption( 'use', 'System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' )

; Construct a guid using any random guid as a constructor parameter.
strGuid = "{D28AC658-2D3C-4111-B297-1E7D132F2FE4}"
oGUID = ObjectClrNew('System.Guid',strGuid )

; Returns a VT_RECORD and WinBatch does not have built in functionality
; to handle this variant type directly.
rGUID = oGUID.NewGuid()  ; Returns a vt_record

; However, ObjectClrType can turn it into a regular COM/CLR object
; with a little help from the FCL.
strctGuid = ObjectClrType("System.Guid", rGUID)

Message("String Representation of New Guid", strctGuid.ToString())
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Thanks Tony!!!
Initially when I read the MSDN http://msdn.microsoft.com/en-us/library/system.guid.aspx I felt the System.Guid wasn't a class, but as they said a type. Your example is really something to learn from.
Allow me a tangent. Winbatch found a way to represent positional parameters in COM by using ::  ... is it conceivable that a function like
oGuid = ObjectClrType("System.Guid::NewGuid()") could evolve?

td

Quote from: stanl on June 29, 2013, 11:19:00 AM
Allow me a tangent. Winbatch found a way to represent positional parameters in COM by using ::  ... is it conceivable that a function like
oGuid = ObjectClrType("System.Guid::NewGuid()") could evolve?

WinBatch supports named parameters in COM Automation method object calls using the the :: syntax.  ObjectClrType is a equivalent to ObjectType for CLR based objects and method parameters when ObjectType's variant conversions wont do.  These two functions, in part, serve the same purpose as cast operators in other languages.  I do not see the connect between a casting from one type to another and named parameters.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade