tags:

views:

220

answers:

3
+9  Q: 

D support for COM

Wikipedia says the following: "On Microsoft Windows, D can access COM (Component Object Model) code."

What kind of support for COM is present in D? Does it make life easier than using COM in C++. I've found this link on the D page but it doesn't tell me too much.

+2  A: 

http://www.digitalmars.com/d/2.0/interface.html#COM-Interfaces

I knew this was somewhere but it took me a while to find it. Basically, COM support in D is a hack on top of interfaces. Apparently the compiler knows about them and treats them as "special" in a few small ways, so everything works. BTW, I thought COM was dead.

dsimcha
COM definitely isn't dead, some of the new features in Windows Vista and 7 are only accessible through it. I posted an example [url=http://lists.puremagic.com/pipermail/digitalmars-d-dwt/2010-June/001923.html]here[url]
torhu
[correct link here](http://lists.puremagic.com/pipermail/digitalmars-d-dwt/2010-June/001923.html)
torhu
A ton of Windows code makes use of COM, so it can't possibly die any time soon.
casablanca
Yes I guess some of us want it do die but it seems it wont for a while. e.g. windows VSS API is COM and Outlook and Exchange still uses MAPI which is COM.
Hannes de Jager
+3  A: 

The Juno lib, written by John Chapman, contains COM support modules. Unfortunately not up to date with the latest compiler.

http://www.dsource.org/projects/juno/wiki/ComProgramming/ "Juno COM"

Should be part of phobos, beside.

To Hannes J. use auto instead of delphi's var

// Create an instance of IXMLDOMDocument3.

auto doc = DOMDocument60.coCreate!(IXMLDOMDocument3); scope(exit) doc.Release();

// Create an event provider instance.

auto events = new EventProvider!(XMLDOMDocumentEvents)(doc); scope(exit) events.Release();

events.bind("onReadyStateChange", { writefln("state changed"); }); events.bind("onDataAvailable", { writefln("data available"); });

// Tell the document to load asynchronously.

doc.put_async(com_true);

// Load the XML document.

com_bool result; doc.load("books.xml".toVariant(true), result);

BLS
Ah If I understand correctly then auto is the thing I was looking for. Thanks :-)
Hannes de Jager
Hmmm, it seems auto provides just type inference and not late binding also?
Hannes de Jager
+3  A: 

Juno has a new version .5.1 that has lots of great ways of connecting to Word, Excel, FrameMaker, Trados, etc. So, it is possible and easy. Something like this:

scope word = new DispatchObject("Word.Application");
scope wDocs = word.get("Documents");

char[] dd  = dir ~ r"\";

char[][] docs = GetFilesFromDir(dir ~ r"\", "*." ~ fromType, true);
if (docs.length == 0)
{
  info.text = "Did not find any " ~ std.string.toupper(fromType) ~
    " files in the directory... \n\nExiting...";
  return;
}
foreach(char[] d; docs)
{
  scope wDoc = wDocs.call("Open", d);//"Normal", false, 0);
  char[] txt = std.path.getName(d);  // original file ie. test if it was test.doc
  txt ~= ".doc";
  if (std.file.exists(txt))
    std.file.remove(txt);

  wDoc.call("SaveAs",
      txt,      // FileName
      0,        // FileFormat wdFormatDOC = 0
      false,    // LockComments
      "",       // Password
      false,    // AddToRecentFiles
      "",       // WritePassword
      false,    // ReadOnlyRecommended
      false,    // EmbedTrueTypeFonts
      false,    // SaveNativePictureFormat
      false,    // SaveFormsData
      false,    // SaveAsAOCELetter
      65001,    // Encoding 65001 is UTF8
      false,    // InsertLineBreaks
      false,    // AllowSubstitutions
      0         // LineEnding Const wdCRLF = 0
      );
  wDoc.call("Close");
}
word.call("Quit");
Thanks for the example code. This answers my question, although I hoped that D would have some dynamic language support for beasts like COM like the variant support Delphi has and the var keyword in C#. Perhaps in the future? I guess it seems like something that does not belong in a systems programming language, but I believe its needed for instances like this and for things like remote invocation. Considering that D has things like garbage collection, a feature like this perhaps fit. Is there a forum where one can vote for something like this?
Hannes de Jager
My understanding is that var is not a variant type it C# and exactly like using auto in D (type inference). D does have http://digitalmars.com/d/2.0/phobos/std_variant.html
he_the_great