VB6 reference fixer

Being tired of my VB6 references running amok due to COM compatibility issues I decided to write a reference fixer.

This little app can do two things:

  • go through all the references in a .vbp file and update TypeLib GUIDs to their correct value (those change when the referenced project’s binary compatibility is broken i.e. if you changed a method signature)
  • set compatibility mode to No compatibility, Project compatibility or Binary compatibility (0,1,2)

Download the application (source included) here. (EDIT: link broken for now, sorry)
Command line is: FixRefs.exe pathtosome.vbp
where switch is:

  • -f – fix references
  • -c0 – set compatibility mode to None
  • -c1 – set compatibility mode to Project
  • -c2 – set compatibility mode to Binary

The interesting part in the code (apart from text parsing is:

[DllImport(”oleaut32.dll”, CharSet = CharSet.Unicode, PreserveSig = false)]
private static extern void LoadTypeLibEx(String strTypeLibName, RegKind regKind, [MarshalAs(UnmanagedType.Interface)] out ITypeLib typeLib);
…
private enum RegKind {
    RegKind_Default = 0,
    RegKind_Register = 1,
    RegKind_None = 2
}
…
public static TYPELIBATTR GetTypeLibInfo(string file) {
    ITypeLib it;
    LoadTypeLibEx(file, RegKind.RegKind_Register, out it);
    IntPtr pStructTypeLibAttr;
    it.GetLibAttr(out pStructTypeLibAttr);
    TYPELIBATTR tlibAttr = (TYPELIBATTR) Marshal.PtrToStructure(pStructTypeLibAttr, typeof (TYPELIBATTR));
    it.ReleaseTLibAttr(pStructTypeLibAttr);
    return tlibAttr;
}

I use the LoadTypeLibEx API to get a pointer to ITypeLib, call it’s GetLibAttr method and marshall the pointer to a struct that holds, among other things, the TypeLib GUID.

I use this app in a NAnt build file, set compatibility to None, fix refs, compile, reset compatibility to binary.

This entry was posted in Work. Bookmark the permalink.

2 Responses to VB6 reference fixer

  1. I’m sorry, the src seems to have been lost. You still have the important part above though, the rest is just text parsing.

Leave a Reply

Your email address will not be published. Required fields are marked *