Click here to Skip to main content
15,885,998 members
Articles / Desktop Programming / ATL

ATL/AUX Library

Rate me:
Please Sign up or sign in to vote.
4.11/5 (5 votes)
17 Mar 2000CPOL 184.6K   1.5K   84  
A set of VC++ helpers and patterns to help automate some routine coding tasks.
////////////////////////////////////////////////////////////////////////////////
// Generate COMDAT GUIDs, as well as IIDs and smart pointer includes
// for interface names via __uuidof().
// (c) Andrew Nosenko (andien@geocities.com)
//
// Run this script against any text file containing interface names.
// E.g.:
//
//   dumpbin /LINKERMEMBER Uuid.lib >ax
//   cscript idgen.js ax
// 
// Or:
//
//   cscript idgen.js MyProject_i.c MyProject_id.h MyProject_sp.h
//

var fileSys = new ActiveXObject("Scripting.FileSystemObject");

if ( WScript.Arguments.Count() < 1 ) {
  WScript.Echo("cscript " + fileSys.GetFileName(WScript.ScriptFullName) + " <in_file> [<out_id> [<out_sp>]]");
  WScript.Quit();
}

var fileIn = fileSys.GetFile(WScript.Arguments.Item(0));
var streamIn = fileIn.OpenAsTextStream();

var re = /(D?IID_(\w+))/;
var dic = new Object;
var count = 0;

var reGUID = /const\s+((IID)|(CLSID))\s+(\w+)\s+=\s+(\{.+\};)/
var reGUID_MIDL = /MIDL_DEFINE_GUID\((\w+),\s(\w+),(\w+,)(\w+,)(\w+,)(\w+,)(\w+,)(\w+,)(\w+,)(\w+,)(\w+,)(\w+,)(\w+)\);/

var dicGUID = new Object;
var countGUID = 0;

while ( !streamIn.AtEndOfStream ) {
  str = streamIn.ReadLine();
  if ( re.exec(str) != null ) {
    iid = RegExp.$1;
    if ( iid == "IID_DEFINED__" ) continue;
    iface = RegExp.$2;
    if ( !dic[iface] ) {
      dic[iface] = iid;
      count++;
    }
  }
  var a = reGUID_MIDL.exec(str);
  if ( a != null ) {
    guid = a[2];
    if ( !dicGUID[guid] ) {
      dicGUID[guid] = "EXTERN_C const "+a[1]+" __declspec(selectany) " + guid + 
      " = {"+a[3]+a[4]+a[5]+"{"+a[6]+a[7]+a[8]+a[9]+a[10]+a[11]+a[12]+a[13]+"}};"
      countGUID++;
    }
  }
  if ( reGUID.exec(str) != null ) {
    guid = RegExp.$4
    if ( !dicGUID[guid] ) {
      dicGUID[guid] = "EXTERN_C const "+RegExp.$1+" __declspec(selectany) "+guid+" = "+RegExp.$5;
      countGUID++;
    }
  }
}
streamIn.Close();

WScript.Echo("GUIDs: "+countGUID+", Interfaces: "+count);

var outFileName;
if ( WScript.Arguments.Count() > 1 ) outFileName = WScript.Arguments.Item(1)
else outFileName = fileSys.GetBaseName(WScript.Arguments.Item(0)) + "_id.h";
var streamOut = fileSys.CreateTextFile(outFileName);

var outFileNameSp;
if ( WScript.Arguments.Count() > 2 ) outFileNameSp = WScript.Arguments.Item(2)
else outFileNameSp = fileSys.GetBaseName(WScript.Arguments.Item(0)) + "_sp.h";
var streamOutSp = fileSys.CreateTextFile(outFileNameSp);

var date = new Date();
streamOut.WriteLine(
  "// Interface identifiers -- by Andrew Nosenko (andien@geocities.com)\n"+
  "// " + date + "\n");
streamOutSp.WriteLine(
  "// Smart pointers -- by Andrew Nosenko (andien@geocities.com)\n"+
  "// " + date + "\n");

// define comdat GUIDs
var guidScope = fileSys.GetBaseName(outFileName)+"_"+date.getTime();
streamOut.WriteLine(
  "// Named GUIDs\n"+
  "#ifndef "+guidScope+"\n"+
  "#define "+guidScope);
for ( var guid in dicGUID ) {
  streamOut.WriteLine(dicGUID[guid]);
//  WScript.Echo(guid);
}
streamOut.WriteLine("#endif // "+guidScope+"\n");

// IIDs and SPs
for ( var iface in dic ) {
  var iid = dic[iface];
  // define IID
  streamOut.WriteLine("#if defined(__" + iface + "_FWD_DEFINED__) && !defined(" + iid +")");
  streamOut.WriteLine("#define " + iid + " __uuidof(" + iface + ")");
  streamOut.WriteLine("#endif");
  // define IPtr
  streamOutSp.WriteLine("#ifdef __" + iface + "_FWD_DEFINED__");
  streamOutSp.WriteLine("_COM_SMARTPTR_TYPEDEF("+ iface +", " + iid + ");");
  streamOutSp.WriteLine("#endif");
//  WScript.Echo(iface);
}

streamOut.Close();
streamOutSp.Close();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions