-3

Need to create a Delphi DLL that can be called from Delphi and .NET. What does .NET like more, COM or dll? Or does it matter?

I just remember hearing that COM can be annoying because of versioning.

3
  • 1
    The answer is, "it depends". Sometimes one way is better, sometimes the other. Perhaps you should revisit the question and provide more of the details. Commented May 5, 2015 at 15:29
  • COM objects are compiled to DLLs. If you mean COM vs Win32 DLL, bear in mind that Win32 DLLs have no support for versioning. Having the client complain that your application crashes do to the wrong version is more annoying than the compiler warning you about the wrong version Commented May 5, 2015 at 15:29
  • 1
    There really is not much distinction between the versioning capabilities of COM and Win32 DLL's. Versioning with COM objects relies on the developer following prescribed practices. Equally effective practices can be employed when developing a Win32 DLL, it's just that most people don't. But there is nothing in COM that forces you to adopt versioning best practice and you can choose not to (or worse: get it wrong if you're not careful).
    – Deltics
    Commented May 5, 2015 at 23:46

3 Answers 3

2

The issue of versioning a Win32 DLL and a COM library is a very specific implementation detail that has little to do with which of the two approaches is most suitable for consumption by a .NET client.

Versioning

COM versioning relies on consistent application of prescribed practices. It is possible to ignore these practices (or to implement them improperly) and create a COM library which suffers the same problems as a Win32 DLL in respect of version stability, consistency and dependency.

Equally it is possible to develop a Win32 DLL that implements mechanisms to ensure version safety, you just have to develop and implement these strategies yourself.

The issues with versioning in COM being perceived as 'annoying' most likely stems from the overhead involved in maintaining backward compatibility in new versions via version independent program ID's. You can choose not to adopt this practice however (as Microsoft decided to do with MSXML from 4.0 onward, for example).

However, this is a separate concern than ensuring that interfaces are immutable, introducing new classes and interfaces with version specific names for example (IMyInterface10, IMyInterface20, IMyInterface30 for versions 1, 2 and 3 etc).

Marshalling and Meta-Data

Beyond versioning however, a COM library greatly simplifies the job of passing complex structured information between a client and the library and ensuring that things like constants used in parameters etc are consistently defined. With a DLL you (and the consumers of your DLL) need to be very careful if you need to exchange anything more complex than simple numeric values or pointers to immutable strings (buffers).

COM provides mechanisms that simplify the exchange of more complex information (called "marshalling" in the jargon).

The COM specification also provides for meta-data to describe the interfaces exposed by your library. This enables your COM objects to be imported very easily for use by a .NET client (or a Win32 client for that matter).

Win32 DLL's have no consistent specification for providing equivalent meta-data, making it more difficult for clients to access the services they provide. Typically the Win32 DLL provider will have to supply headers or other interface declarations for each language or environment they support. If a developer is using some other language for which these headers are not provided then they must do a manual conversion based on those headers that are provided.

Plug-Ins vs Shared Libraries

There are some use cases where a Win32 DLL is the more appropriate option, but these tend to involve situations where the DLL is used as a private, loadable module to extend a single, specific application via a private interface mechanism, rather than when making those libraries available to others for general use.

Since you specifically intend sharing this library between Delphi and .NET clients however, this does not seem to be one of those special cases.

Conclusion

For all these reasons then, in the case of a general purpose library such as yours appears to be, COM is almost always to be preferred.

If versioning is likely to be important it remains your responsibility to ensure you are familiar with and adopt good versioning practices correctly and consistently within your COM library, if they are to be effective.

1
  • It's a bummer the question is closed and downvoted, this answer could be very useful to have around. Commented Jun 18, 2015 at 13:21
2

COM is very demanding on certain things being done on certain ways. You need to be aware of stuff like threading models and some other esoteric stuff. The good thing is that a COM object integrates nicely in the .NET world because you get things like intellisense and whatsnot. In that aspect it is "better" integrated (also, Delphi makes COM easy when compared with, say, C++). Some of my own experiences are in this StackOVerflow question.

DLL are probably simpler, but you still need to use specific data types and be very aware of what you do when using shared memory or objects. There are some pointers in this StackOverflow question.

As David said, the real answer is it depends on what you need to do.

1

COM registration is a real PITA.... For instance you need administration rights.

So I would use a DLL if I had the choice.

For marshalling strings, consider using WideString on Delphi side and BSTR on c# side.

Not the answer you're looking for? Browse other questions tagged or ask your own question.