arsd.com

Code for COM interop on Windows. You can use it to consume COM objects (including several objects from .net assemblies) and to create COM servers with a natural D interface.

This code is not well tested, don't rely on it yet. But even in its incomplete state it might help in some cases. Strings and integers work pretty ok.

You can use it to interoperate with Word and Excel:

void wordmain() {
	// gets the name of the open Word instance, if there is one
	// getComObject gets the currently registered open one, and the
	// "false" here means do not create a new one if none exists
	// (try changing it to true to open a hidden Word)
	auto wrd = getComObject("Word.Application", false);
	writeln(wrd.ActiveDocument.Name.getD!string);
}

void excelmain() {
	// create anew Excel instance and put some stuff in it
	auto xlApp = createComObject("Excel.Application");
	try {
		xlApp.Visible() = 1;
		xlApp.Workbooks.Add()();

		xlApp.ActiveSheet.Cells()(1, 1).Value() = "D can do it";
		xlApp.ActiveWorkbook.ActiveSheet.Cells()(1,2).Value() = "but come on";

		writeln("success");
		readln();

		xlApp.ActiveWorkbook.Close()(0);
	} catch(Exception e) {
		writeln(e.toString);
		writeln("waiting"); // let the user see before it closes
		readln();
	}
	xlApp.Quit()();
}

The extra parenthesis there are to work around D's broken @property attribute, you need one at the end before a = or call operator.

Or you can work with your own custom code:

namespace Cool {
	public class Test {

		static void Main() {
			System.Console.WriteLine("hello!");
		}

		public int test() { return 4; }
		public int test2(int a) { return 10 + a; }
		public string hi(string s) { return "hello, " + s; }
	}
}

Compile it into a library like normal, then regasm it to register the assembly... then the following D code will work:

import arsd.com;

interface CsharpTest {
	int test();
	int test2(int a);
	string hi(string s);
}

void main() {
	auto obj = createComObject!CsharpTest("Cool.Test"); // early-bind dynamic version
	//auto obj = createComObject("Cool.Test"); // late-bind dynamic version

	import std.stdio;
	writeln(obj.test()); // early-bind already knows the signature
	writeln(obj.test2(12));
	writeln(obj.hi("D"));
	//writeln(obj.test!int()); // late-bind needs help
	//writeln(obj.opDispatch!("test", int)());
}

I'll show a COM server example later. It is cool to call D objects from JScript and such.

Public Imports

core.sys.windows.windows
public import core.sys.windows.windows;
Undocumented in source.
core.sys.windows.com
public import core.sys.windows.com;
Undocumented in source.
core.sys.windows.wtypes
public import core.sys.windows.wtypes;
Undocumented in source.
core.sys.windows.oaidl
public import core.sys.windows.oaidl;
Undocumented in source.

Members

Aliases

pfn_t
alias pfn_t = HRESULT function()

Register/unregister a DLL server. Input: flag !=0: register ==0: unregister

Classes

ComException
class ComException

Functions

ComCheck
bool ComCheck(HRESULT hr, string desc)
createComObject
auto createComObject(wstring c)
auto createComObject(GUID classId)
getComObject
auto getComObject(wstring c, bool tryCreateIfGetFails)

Create a COM object. The passed interface should be a child of IUnknown and from core.sys.windows or have a ComGuid UDA, or be something else entirely and you get dynamic binding.

initializeClassicCom
void initializeClassicCom(bool multiThreaded)

Mixin templates

ComObjectImpl
mixintemplate ComObjectImpl()

Mixin to a low-level COM implementation class

IDispatchImpl
mixintemplate IDispatchImpl()

Mixin to a low-level COM implementation class

Structs

ComClient
struct ComClient(DVersion, ComVersion = IDispatch)
ComGuid
struct ComGuid
ComProperty
struct ComProperty
Undocumented in source.

Meta