Is there is a way of exporting and using classes from another dll,
I have 2 dll's and I am trying to access classes in between, was wondering if this is possible.
There are a variety of ways to achieve this, including but not limited to the following:
Use runtime packages rather than DLLs. Then you can use any types, variables, etc. from another module. Note that this forces you to use runtime packages in all of your modules, and to compile all of the modules with the same version of Delphi.
Continue to use DLLs, but access the types via interfaces rather than Delphi classes. Interfaces, unlike classes, can be exported across DLL boundaries.
Continue to use DLLs, but access the types using unit scope procedures and functions rather than classes. This would lead you to an interface of the same nature as the Win32 interface.
Of the above options, they are arranged in order of decreasing convenience. The most convenient is to use runtime packages but that may place an undesirable constraint on you that all modules are compiled with the same Delphi version. Interfaces are usually more convenient to consume than a Win32 style interface, but there may be more programming overhead in setting up such an architecture. You'll have to make the choice that you feel best suits your needs.
If you can avoid using separate modules in the first place, and build everything into a single executable file, then that is far and away the most convenient approach.
Related
I am having trouble get a clear picture of the advantages/disadvantages of using Sharemem vs SimpleSharemem to pass strings between a main program and DLLs.
Here is the background for why I am looking into this: My main program is developed in XE2 64-bit (although I may upgrade to XE7) and the DLLs will either be developed with XE2 (or higher) or FPC 64-bit (the ideal scenario). The main program and the DLLs need to be able to pass strings contained in records. Ideally I would like to have the DLLs developed by people who may not have Delphi and it appears as though FPC supports Sharemem but not SimpleSharemem.
Are there performance differences between Sharemem vs SimpleSharemem? Are there other reasons to prefer one vs the other in the scenario I describe (aside from the apparent FPC support for Sharemem)?
Thank you!
Sharemem is the old way of sharing memory managers. It relies on a DLL being deployed alongside your application. SimpleSharemem is designed to work with FastMM which handles sharing in a different way from Sharemem. So for modern versions of Delphi that use FastMM as their memory manager, use SimpleSharemem.
Shared memory managers allow you to allocate memory in one module and deallocate in another, using the standard language heap functions like GetMem, New etc.
However, you are attempting rather more than this. You are hoping to share a memory manager between modules compiled by different compilers. Specifically Delphi and FPC. Delphi's ShareMem is designed to allow sharing between modules compiled in Delphi. Likewise FPC's ShareMem is designed to allow two FPC compiled modules to share memory manager. There is no support for sharing between Delphi and FPC modules.
Even further, you are hoping to be able to pass string objects between modules. This requires the implementation of the string object to be compatible across the interop boundary. That might be the case for Delphi's UnicodeString for XE2 and XE7 modules, but if so it is only by chance. There is no guarantee of that. Future releases of Delphi may change the implementation. And as for mixing Delphi and FPC strings, there's no reason to believe that they can be mixed. I doubt they can.
So my advise is to stop trying to use shared memory managers and stop trying to pass native language strings between modules compiled with different languages.
In order to pass text between modules in a mixed compiler environment you need to use valid types for binary interop. Typical approaches include:
Passing null-terminated C strings, PWideChar. Trivial when passing text in. When passing text out this becomes more difficult because you may need callee to allocate but caller to deallocate. Either export a deallocator or allocate on a shared heap, e.g. the COM heap.
Use the COM BSTR type. In both Delphi and FPC this is wrapped by WideString. This type is designed for binary interop.
My choice would be to use WideString.
I have the source in delphi of an open source electrical solver (OpenDSS)
Normally, when installing OpenDSS, the engine is registered as a COM component.
I want to compile the engine 8 times making 8 differente dll's that internally are exactly the same. Why? because I want to use 8 mono-core solvers at once to perform monte carlo simulations.
The problem is that by simply renaming the dll's and registering them, windows recognize them as the same COM component. So the question is; What changes shall I make in the source so when I compile the library I cheat windows?
I hope que question is clear enough, and thank you in advance.
Naively, the answer to your question is to make eight different versions of the code that are identical apart from having different CLSIDs. Keep all the IIDS of the exposed interfaces the same, but vary the CLSIDs. That allows you to register eight distinct COM servers with different CLSIDs to identify them. But you request the same IID from each COM object that you create.
This is a pretty vile solution though. It doesn't scale very well. It's expensive of memory. It's just really unwieldy. It would surely be possible to make the code threadsafe. Usually this can be done by locating the globals and dealing with them. Typically by converting them into parameters or state.
If you cannot manage that then you could create a stub executable to allow the server to run out-of-proc. Then instantiate separate instances of the out-of-proc server.
I believe that most IoC containers allow you to wire dependencies with XML configuration file. What are cons and pros for using configuration file vs. registering dependencies in code?
These pros and cons are based on my work with spring. It may be slightly different for other containers.
XML
pro
flexible
more powerful than annotations in some areas
very explicit modelling of the dependencies of your classes
con
verbose
difficulties with refactoring
switching between several files
Annotations
pro
less file switching
auto configuration for simple wirings
less verbose than xml
con
more deployment specific data in your code (usually you can override this with xml configs)
xml is almopst(?) always needed, at least to set up the annonation based config
annotation magic may lead to confusion when searching for the class that is used as dependency
Code
pro
Can take advantage of strongly-typed languages (e.g. C#, Java)
Some compile-time checking (can't statically check dependencies, though)
Can take advantage of DSLs (e.g. Binsor, Fluent interfaces)
Less verbose than XML (e.g. you don't need to always specify the whole assembly qualified name (when talking .net))
con
wiring via code may lead to complex wirings
hard dependencies to IOC container in the codebase
I am using a mix of XML+Annotation. Some things especially regarding database access are always configured via xml, while things like the controllers or services are mostly configured via annotations in the code.
[EDIT: I have borrowed Mauschs code PROs]
XML pros:
Can change wiring and parameters without recompiling. Sometimes this is nice to have when switching environments (e.g. you can switch a fake email sender used in dev to the real email sender in production)
Code pros:
Can take advantage of strongly-typed languages (e.g. C#, Java)
Some compile-time checking (can't statically check dependencies, though)
Refactorable using regular refactoring tools.
Can take advantage of DSLs (e.g. Binsor, Fluent interfaces)
Less verbose than XML (e.g. you don't need to always specify the whole assembly qualified name (when talking .net))
I concur. I have found ioc containers to give me very little, however they can very easily make it harder to do something. I can solve most of the problems I face just by using my programming language of choice, which have allways turned out to be simpler easier to maintain and easier to navigate.
I'm assuming that by "registering dependencies in code" you mean "use new".
'new' is an extraordinarily powerful dependency injection framework. It allows you to "inject" your "dependencies" at the time of object creation - meaning no forgotten parameters, or half-constructed objects.
The other major potential benefit is that when you use refactoring tools (say in Resharper, or IntelliJ), the calls to new change too
Otherwise you can use some XML nonsense and refactor with XSL.
I need to return objects from a DLL made in Delphi, to an app made in Delphi, too. The objective is to do a subsystem that can be modify in the future without to modify the main app. So, I imagine developing the subsystem in a DLL is a (good??) idea... i am programming in Windows XP, Delphi 7. I did read DLLs only return basic data type, but there must to be a way to do that...
Best regards.
I prefer to apply COM to such a model, which allows you to create external "objects" which you then can direct from within your application. Creating COM objects in Delphi is extremely simple, use your ActiveX creation methods to create an ActiveX library and then create COM objects. You then use the interface unit in your main application, and when you CoCreate an instance of the object it loads the appropriate DLL. The only tricky part of this is that your com objects must be registered on the system to function properly... which in the Win7/Vista world requires elevated access... although once this is done, it is seamless.
The BEST way is to use a COM wrapper as suggested by skamradt.
It is possible, but not a good idea to pass object references as pointers to DLL's. Refer particularly to Peter Haas's comments.
If you do pass an object from a Delphi DLL to a Delphi app you will have the following problems:
You must use the same version of Delphi for the app and DLL.
Both app and DLL MUST have the same implementation of the object - or at least identical layout of all fields in the class - OK if you are using standard objects such as TStringList.
You should use shared memory or runtime packages, otherwise you will get weird access violations.
I have had to maintain code where this was done - it was nightmarish as you couldn't change classes without a lot of re-compiling.
You can use interfaces and most of your problems, wrt compiler/rtl versions or even other languages just go away.
Delphi's interfaces are always IUnknown-compatible, which makes them compatible with most OO-enabled languages on Windows.
One thing to keep in mind, though: Don't use AnyString, stick to WideString, which is the Stringtype used by COM.
A platform- and language independent way could be to exchange serialized objects.
It has a performance impact, but it has advantages too: the DLL works without modifications with other languages and platforms like .Net or Java (via JNA Java Native Access). It does not depend on any special features of the operating system so it can as well be used on Linux or MacOS if you compile the library with Free Pascal.
For the serialization, you could use JSON or XML. There are open source libraries for Delphi, for example SuperObject and OmniXML.
Is it possible to do Aspect Oriented Programming in Delphi? I would be interested in native support as well as third party solutions.
I don't have a specific problem I want to solve with AOP, but am simply interested in studying AOP.
AOP depends on two things:
The ability to inject additional code into an existing unit of code
A mechanism to place conditions on where code should be injected.
This is commonly referred to as code weaving. It is a specialization within the larger study of program transformation.
JIT compiled languages have more options for implementing code weaving than statically compiled programs because more information is retained in the bytecode/IL. They also support reflection, which offers the ability to manipulate code at runtime.
Delphi.NET and Prism have the same access to these capabilities as any other .NET language.
There are two AOP frameworks for Delphi Win32 that I'm aware of. The first is MeAOP, which has already been mentioned. The second is Infra. Both projects take a similar approach to AOP. They use a combination of RTTI and clever pointer manipulation to intercept method calls so you can run additional code before or after the method call. You define your cross-cutting feature as a subclass of the framework's AOP class. You register the methods you want intercepted by passing the method name as a string argument to the AOP framework.
Both frameworks are still actively developed and are actually larger in scope than just AOP. Unfortunately documentation is somewhat sparse (and in Infra's case mostly in Portuguese)
Another project attempted AOP through source code weaving back in 2004 with some success. Basically they built an aspect weaver on top of a general purpose program transformation tool called DMS and used it to inject code into delphi source files prior to compilation. Their aspect oriented language was primarily influenced by AspectJ.
http://www.gray-area.org/Research/GenAWeave/ has links to the original paper and presentation as well as some videos of the transformation process.
It may also be possible to use runtime code instrumentation to accomplish this as well. Its a technique used by some profilers to inject counters and stack traces into running code without modifying the original source. A similar technique could be used to inject crosscutting concerns into a statically compiled executable. The PinTool project is a good example of this.
ClassHelpers in the later versions of Delphi allow some very limited level of AOP type behavior. You can use ClassHelpers to inject behavior into other classes without descending from them. It allows overriding existing methods and then optionally calling that existing method.
The limitation is you must declare a ClassHelper for a specific class and it descendants. Additionally a class can only have one ClassHelper.
These are similar to Extension methods in C#.
The DSharp library features AOP:
https://bitbucket.org/sglienke/dsharp
More info can be found at: https://bitbucket.org/sglienke/dsharp
Also have a look at TVirtualMethodInterceptor.
It's in the RTL since Delphi 2010 and allows you to do OnBefore, OnAfter, etc. calls on all virtual methods on a class.
This call alone should cover must of what you need using Rtti, not weaving which is much faster than run-time weaving.