Solved

"COM DLL interface"??

Posted on 2010-08-13
38
530 Views
Last Modified: 2012-05-10
Great news: I've been asked to try and create a 'DLL interface' (or a COM DLL interface) and I've never created a DLL and I've never worked with COM (I have however created a SOAP CGI exe interface but I don't know how the two are related).

I need some advice, help, links, references or general tidbits of information.

I need to know things like, can my DLL use database connectivity such as a TADOConnection and TADOQuery?

Can I embed images inside a DLL?

Where does 'COM' come into all of this?

any other snips of information
0
Comment
Question by:rfwoolf
  • 18
  • 12
  • 7
  • +1
38 Comments
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427447
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427451
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427456
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427506
Hi Geert

Thanks for the links, I mean, I can google myself :p but yes if you have any particular links that you personally recommend because you've gone down this road before then please do share.
Also if you know the answers to those small questions about embedding images or database connectivity, then please let me know.
Good to have your input as always :)
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427517
Is this link:
  http://delphi.about.com/od/kbcontrolole/ss/word_automation.htm
about COM programming?
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427526
In other words, what you should be saying to me is that strictly speaking a DLL is not a proper 'COM' interface, but they are both interfaces / APIs.
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33427535
>> I need to know things like, can my DLL use database connectivity such as a TADOConnection and TADOQuery?

Sure, why not?

>> Can I embed images inside a DLL?

Of course, you can include they in the resources, the same as into exe file

>> Where does 'COM' come into all of this?

I suggest you read some books, because COM is much more difficult technology than SOAP
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427540
lol, i opened 2 pages on about.com
i posted the wrong one

the small questions:

yes, you can imbed images

yes, you can access the database
allthough you need to wrap the call with
CoInitialize ...
try

finally
  CoUnInitialize;
end;

2 ways:
1: per thread:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24043411.html

once for all threads by good ol ziolko:
http://www.experts-exchange.com/Programming/Misc/Q_20530169.html

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427551
there is a good book aroundn it's lying in a cupboard here somewhere ...
can't find it.

and i was trying to find the link (ebook)
posted wrong link on about:

this is a very good site:
http://www.djpate.freeserve.co.uk/Automation.htm
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33427581
>> CoInitialize ...

1. it is enough to call CoInitialize/CoUnInitialize only once for process

2. They are called automatically in initialization/finalization section if you're uses ComObj unit. ADODB uses ComObj, so you don't need to call CoInitialize/CoUnInitialize at all
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427668
"yes, you can access the database
allthough you need to wrap the call with
CoInitialize ...
try

finally
  CoUnInitialize;
end;
"

Okay some clarification: you say you need to 'wrap the call', does this mean when you are CALLING the DLL or do you mean something else?
So in my DLL if I want to publish a function that uses TADOQuery, is it not straightforward? If it's not, I would appreciate the function code.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427675
aflarin >> depends on parameter if you only have to call it once or per thread

{ CoInitFlags determines the COM threading model of the application or current
  thread. This bitflag value is passed to CoInitializeEx in ComServ initialization.
  Assign COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED to this variable before
  Application.Initialize is called by the project source file to select a
  threading model.  Other CoInitializeEx flags (such as COINIT_SPEED_OVER_MEMORY)
  can be OR'd in also.  }
var
  CoInitFlags: Integer = -1;  // defaults to no threading model, call CoInitialize()

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427679
if you set the parameter to COINIT_MULTITHREADED
you wouldn't need to think about :)
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427693
before they had the unit ADODB ...

i used to have this in my code, before i bumped into the post of ziolko:

CoInitialize(nil);
try

  Conn := TAdoConnection.Create(nil);
  try
   // etc
    Qry := TAdoQuery.Create(nil);


finally
  CoUnInitialize;
end;

Now i just have that parameter ... no more bother
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427699
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427764
I'm still confused about where COM comes into all this,
for example, according to the one About.com tutorial, a simple hello world call looks like this:

==================================
library TestLibrary;
 
 uses SysUtils, Classes, Dialogs;
 
 procedure DllMessage; export;
 begin
   ShowMessage('Hello world from a Delphi DLL') ;
 end;
 
 exports DllMessage;
==================================
That looks really easy!
So I imagine creating a TADOQuery and TADOConnection to be also easy. I don't see where COM comes in?


This is basically what I'm going to be doing:
1. Creating a type declaration of a product, say, TProduct with these properties:
Product.Id
Product.Description
Product.RetailPrice
Product.StockOnHand
Product.SupplierId
Product.Barcode
Product.Image

2. Most of my functions are going to be creating an instance of a TADOQuery and a TADOConnection and then returning an instance of TProduct populated with a record from the ADOQuery.

So in theory this is kinda straightforward.
Unless there's really COM.
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427778
One thing I have to ask: If you look at my last post, is there an easier way to do this other than DLL? These functions will be called by another EXE that's probably written in VB. If I could create an EXE instead of a DLL I might find this a lot easier.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427801
the dll has to export functionality so others can import this functionality

COM ... is the definition of how this is exported

with a ordinary dll,
you will have to give a description of all the procedures/function in your dll

with a com dll, regsrv32 will be called to register the com dll in the registry
then other programmers can import the definition with import type library

have you done this yet ? import type library
(from delphi 2010 menu / Component / Import Component / Type library)

once you have installed your dll, you can try this too
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427813
"with a com dll, regsrv32 will be called to register the com dll in the registry"
Thanks for clarifying that Geert. It's starting to make bits of sense.
Can I just clarify if a dll or COM Dll really is the best way to go here?
If we only have 2 or 3 functions and these functions and types don't really need to be 'imported', then can't I just create an EXE somehow that other EXE's can call?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427832
there are 3 types of ideas/programming techniques you need to write a com dll

1: thread programming
2: interface programming (class factory, enumerator, etc ...)
3: installing/importing the dll

>
2: has the delphi interface programming
and then next level com interface programming

bob swart has a few articles on this too, i think
ah yes:
http://www.drbob42.com/examines/examin31.htm
http://www.drbob42.com/examines/examin34.htm
http://www.drbob42.com/examines/examin36.htm
http://www.drbob42.com/examines/examin37.htm

here is his list:
http://www.drbob42.com/examines/index.htm
0
 
LVL 13

Assisted Solution

by:aflarin
aflarin earned 200 total points
ID: 33427833
@Geert

>> var
>>   CoInitFlags: Integer = -1;  // defaults to no threading model, call CoInitialize()

This variable is automatically modified when your ObjectFactory is initialized. If you initialize ObjectFactory as tmApartment, CoInitFlags = COINIT_APARTMENTTHREADED, etc

So you don't have to call CoInitializa/CoUninitialize in the main thread. For other threads you have to call CoInitializa/CoUninitialize, but you can do it only once.

@Rfwoolf

That's a simple dll, there is no COM there. You can create a simple dll with COM via Delphi's Wizards: ActiveX library at first, then Com object/Automation object

Also you can create EXE which will be support COM and it will be accessible from VB. Look at AutoServ sample into Delphi's demos dir
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427853
if you have to call it from VB

then the easiest would be create a standard dll
which you provide the calling syntax for (just copy the exports clause and definition of the procs/funcs)

the vb guys just define the procedures in their code
and call them (off course with right types for the variables)

the next step is change that into a com dll
if it's .NET programmers this last step may be a must
otherwise it's a call to unmanaged code
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427858
>>aflarin
So I do have to work with COM and threads? : /

I'm gonna go read Geert's recommended book "Delphi COM Programming" (can't promise I'll get through it) and I'll have a look at the demo.

===

Thanks for this initial bits of information guys, hopefully I can wrap up and award points :p
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427862
there is also the other option :
recreate their app with delphi and just call the units you would create like usual

they wouldn't like that however ... :)
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427875
>>Geert
>>"then the easiest would be create a standard dll
which you provide the calling syntax for"
I know I'm a pain, but, would I have to implement COM in that case?
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427876
>> i didn't get through the book the first few times either ... :)
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33427889
you can actually write almost anything without com

so it's not a must

you have to however, provide the other people with headers
otherwise it's like a black box
PE Explorer could help a little with this
but you need to leave some debug info for PE Eplorer to work
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33427892
>> If we only have 2 or 3 functions and these functions and types don't really need to be 'imported', >> then can't I just create an EXE somehow that other EXE's can call?

if you would like to create an EXE - The simplest way to call it from VB is COM

if you would like to create a DLL - you have two simple ways:

1. COM
2. import/export DLL functions


 
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33427915
>> So I do have to work with COM and threads? : /

I think, the main thread will be enough. In this case it's not a difficult work
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33427926
aflarin>>

It's like, the more you guys answer, the more confused I get.
Yours answers each time **imply** that there's a way I can do this that's close to "normal" Delphi  programming, that is, without threads, and without COM - the kind of stuff like "Drop a TADOConnection on a DataModule and execute a query." - that's the kind of stuff I would prefer, and I've tried to explain how small and "simple" this 'dll' needs to be - run a TADOQuery and return a TProductInfo, and the VB guys just need to know the name of the function and what to expect back -- so, I need to understand if I must go study COM and threads, or if there's an easy way to do this.

I'm not asking for code just yet because I try do that myself or I can open a new question when I get stuck, but I need to know if I'm on the right track, and whether that track (in this example) needs COM and threads.
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 300 total points
ID: 33427998
this is your only problem then
>>returning an instance of TProduct

you also need to give back the definition of TProduct to the VB guys

with com you would call a function
which calls a class factory which creates a interfaced instance to TProduct

the TProduct would be defined as a class with a interface IProduct

ms-help://embarcadero.rs2010/rad/Automation_Objects_(Win32_Only).html

type
  IProduct = dispinterface
    function GetId: integer; dispid 0;
    function GetDescription: OleVariant; dispid 1;
    function GetRetailPrice: Single; dispid 2;
    function GetStockOnHand: Integer; dispid 3;
    function GetSupplierId: integer; dispid 4;
    function GetBarcode: OleVariant; dispid 5;
    function GetImage: OleVariant; dispid 6;
  end;
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33428113
>> It's like, the more you guys answer, the more confused I get.

It's normal state when you receive a lot of information. You should to read something about COM and decide if it suit you. I think it is better way than using DLL funcion if your project will be used from VB.

BTW why VB guys don't connect database and receive product info from there? you can create a stored proc to incapsulate query text and some additional processing
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33428133
>>aflarin
"You should to read something about COM and decide if it suit you. I think it is better way than using DLL function..."
So what you're saying is I can just make this a normal 'DLL' function without COM programming?
If that's the case then that's definitely the way I'd like to go.

"BTW why VB guys don't connect database and receive product info from there? you can create a stored proc to incapsulate query text and some additional processing"
This is a good question and in this case it's not really up to me. I think we want to hide the intelligence of our system, but like you say we can just use a SP.
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33428183
>> So what you're saying is I can just make this a normal 'DLL' function without COM programming?

Yes, you can do it as dll function. Is it possible to use it from VB or VB.net? Yes

What VB guys prefer: using dll function or using COM object? I think they prefer to use COM object.

0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33428401
hiding the intelligence from VB guys ?
would that be because VB is still .... basic ... ?
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 33428423
"would that be because VB is still .... basic ... ?"
:p

By the way, tried to find your "Delphi COM Programming" book online as a PDF or eBook just in case I need to know COM (still can't quite figure out if it's compulsory with a dll or not). Might have to settle for 'a chapter' or two in one of the 'general' Delphi books.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 33469995
e-books ? i have a humble collection here
http://veerle-en-geert.be/delphi/ebooks.htm
0
 
LVL 14

Expert Comment

by:systan
ID: 33599969
>>It's like, the more you guys answer, the more confused I get.
lol, lol
Sorry guys for the pluggin, but I enjoy reading your responces.(not all)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now