Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 541
  • Last Modified:

"COM DLL interface"??

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
rfwoolf
Asked:
rfwoolf
  • 18
  • 12
  • 7
  • +1
2 Solutions
 
Geert GruwezOracle dbaCommented:
0
 
Geert GruwezOracle dbaCommented:
0
 
Geert GruwezOracle dbaCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rfwoolfAuthor Commented:
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
 
rfwoolfAuthor Commented:
Is this link:
  http://delphi.about.com/od/kbcontrolole/ss/word_automation.htm
about COM programming?
0
 
rfwoolfAuthor Commented:
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
 
aflarinCommented:
>> 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
 
Geert GruwezOracle dbaCommented:
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
 
Geert GruwezOracle dbaCommented:
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
 
aflarinCommented:
>> 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
 
rfwoolfAuthor Commented:
"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
 
Geert GruwezOracle dbaCommented:
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
 
Geert GruwezOracle dbaCommented:
if you set the parameter to COINIT_MULTITHREADED
you wouldn't need to think about :)
0
 
Geert GruwezOracle dbaCommented:
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
 
Geert GruwezOracle dbaCommented:
0
 
rfwoolfAuthor Commented:
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
 
rfwoolfAuthor Commented:
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
 
Geert GruwezOracle dbaCommented:
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
 
rfwoolfAuthor Commented:
"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
 
Geert GruwezOracle dbaCommented:
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
 
aflarinCommented:
@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
 
Geert GruwezOracle dbaCommented:
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
 
rfwoolfAuthor Commented:
>>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
 
Geert GruwezOracle dbaCommented:
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
 
rfwoolfAuthor Commented:
>>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
 
Geert GruwezOracle dbaCommented:
>> i didn't get through the book the first few times either ... :)
0
 
Geert GruwezOracle dbaCommented:
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
 
aflarinCommented:
>> 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
 
aflarinCommented:
>> 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
 
rfwoolfAuthor Commented:
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
 
Geert GruwezOracle dbaCommented:
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
 
aflarinCommented:
>> 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
 
rfwoolfAuthor Commented:
>>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
 
aflarinCommented:
>> 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
 
Geert GruwezOracle dbaCommented:
hiding the intelligence from VB guys ?
would that be because VB is still .... basic ... ?
0
 
rfwoolfAuthor Commented:
"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
 
Geert GruwezOracle dbaCommented:
e-books ? i have a humble collection here
http://veerle-en-geert.be/delphi/ebooks.htm
0
 
systanCommented:
>>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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 18
  • 12
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now