Finding GUIDs with GExperts Grep

I recently had a problem with access violations when calling methods of interfaces. The reason turned out to be duplicate GUIDs in the interface declarations. This caused AVs because the wrong methods were called and the parameters passed to them were not of the right type and numbers. Duplicate GUIDs are usually caused by copying existing interfaces and changing them, without also generating a GUID for the copy. (Btw: The Hotkey for generating a new GUID in the Delphi IDE is Ctrl+Shift+G.)

So I looked for a way to find all interfaces and their GUIDs in my program. There is a uInterfaces.Duplicates unit on GitHub which claims to retrieve this list. Unfortunately I needed this for a Delphi 2007 program and Delphi 2007 does not come with a System.RTTI unit, so this was not an option (and therefore I don’t know whether this unit would have worked).

StackOverflow didn’t help much either, apart from David Heffernan’s suggestion to parse the source code.

So this is what I did. I used GExperts Grep to search for GUIDs, or more eaxct: For the pattern that is used to add a GUID to an interface declaration in Delphi.

It always looks like this:

type
  ISomeInterface = interface ['{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}'];

Where X is a hexadecimal digit in upper case.

The Regular Expression for this is:

\['\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}'\]

The literals [ ] and { } must be escaped with a backslash because they have special meaning in a RegEx. [0-9A-F] means one of ‘0’..’9′ or ‘A’..’F’ which is an upper case hexadecimal digit. {4} means four of those {8} means eight and of course {12} means twelve.

You have to turn off the option “Whole Word” and turn on “Regular Expression”. Of course this needs to search at least all files in the project.

Why do I say “at least”? Because actually it needs to search all files linked into the project, including any packages if such are used. “All files in project” only searches files listed in the DPR file, so it’s potentially missing quite a lot.

Which, again, reminds me that I wanted to extend Grep Search to search all files in the MAP file. Code for getting that list is already in place and being used e.g. in the Open File Expert. I “just” need to put the pieces together.

I’m thinking about making yet another Expert out of this, that not only lists all GUIDs but shows a list of Interface names and their associated GUIDs and of course checks for duplicates.

Discussion about this post in the international Delphi Praxis forum.