Using COM (or DLLs) to make data-access routines customizable by (smart) end-users.

I have built an inventory analysis algortihm that must be customized by the end user to integrate with legacy data systems. I want to architect the application in a way that simplifies the integration.

The demo version of my application uses global functions like this one:

int GetInventory(COleDateTime sampleTime, int resourceID)
      // Use input data to seed random number generator.
      srand(resourceId + sampleTime.GetDay());
      // Generate random data for demo implementation.
      return (rand()%20);

Instead of retrieving data from a database or other source the demo version just makes the data using a random-number generator. When integrated in a real business environment, the logic should be changed to retrieve the data from a real database. Therefore, any end-user my application would want to customize the definition of this function to pull data out of their own database and possibly do related computations.

To facilitate the customization, I might give the end-user the entire source code for my application and let them alter the implementation of GetInventory() and recompile.

That's a bit sloppy, however, and I would like to know a more professional approach to acheiving the same purpose. I think COM can help, but I need guidance.

Work In Progress:

I guess I could define a simple COM object "IMyDataAccessRoutines" that my application uses and change the implementation of GetInventory to:

int GetInventory(COleDateTime sampleTime, int resourceID)
      int response(0);
      IMyDataAccessRoutines* pMyDataAccessRoutines;
      HRESULT hr = CoCreateInstance(???,???,(void**)pMyDataAccessRoutines);
      if(SUCCEEDED(hr)) {
            pMyDataAccessRoutines->GetInventory(sampleTime, resourceId, response);
      return response;

If I put the rand calls in the COM object implementation and deliver the COM object as a DLL, then the end-user could customize the application by building his own version of this COM object and replacing the DLL on his system.

How do I make this work correctly?
COM is so confusing that I am sure I will make some mistakes if I don't get a little help.

What I would ideally like to see is complete instructions for dealing with this simple example.
The solution should say how to:
1) Implement the global function GetInventory()
2) Build the default COM object or other DLL.
3) Build the end-user COM object or other DLL.
4) Switch the application at run-time back and forth between using the orignal or end-user version of the DLL.

The example is simple and clear, so I am really hoping for a clear explanation for this particular case.

Who is Participating?
christophmConnect With a Mentor Commented:
Hi djelescu.

I believe you will find a concise and easy to find example of how to write automation and ActiveX in Kruglinski, "Inside Visual C++".  I believe the book is in the fifth edition, in the fourth edition that i have you should follow through the first two examples in chapter 24.  Using just those two examples you will know how to write a self-registering automation component.

The simple bank deposit example in Kruglinski's book will have to be modified to support the generality and user customization you want.

In brief : Your program utilizes an/the automation component to do the data base access (functions) and you specify in your program the 'data characteristics' that are important to your main program code; for example,
  char     szOrderNumber[26];
  char     szCustomerName[51];
  int      fOrderValue;

in the automation component you will have to build 'methods' (functions) that return values that you use in your main program to 'fill' the 'OrderNumber', 'CustomerNumber', etc. fields.  this is all explained in the book.

All the DB access will be in your component and can be modified 'site to site' to fit each customer's particular DB server software.  BTW - this is pretty close to the kind of archicture that microsoft's three tier (DNA) concept describes.

You SHOULD give each customer example  source code for say... SQL Server.

You MUST (MUST!) tell each customer what methods they MUST include in their component (because your program depends upon finding those methods!) and you must tell each customer what the data items ('CustomerNumber', 'OrderValue', etc.) look like; i.e., char[26], float, etc.

One last thing - if you wish to avoid modifying your main program you must have the different users use the same 'name' for the component - then your main program will know what string to look for in the registry and your main program will not have to be modifed - or the source code delivered - site to site.

Another last thing.  I believe you will be very satisfied with this solution - the fifty bucks for the Kruglinski book is a bargain for just this one technique and the book is loaded with lots of other good stuff.  You are doing something a little strange in that Customer #1 will have a component named perhaps "djelescus_component" and Customer #2 will have a component named the same ("djelescus_component").  The two components are different and you will have to mind your p's and q's to keep straight which customer is using which actual component since you have them named the same!

I am not including any sample code or detail explanation of how to do this - it is explained PERFECTLY in the Kruglinski book and i would just be copying from the book.
djelescuAuthor Commented:
Adjusted points from 200 to 250
   From y u explenation I know this things.  U have a logic(like a random number generator).U also allow changes in u r fun:(Not the source code of random number generator).So u can use com here(If u r COM object contain only random number generator(only srand function why u change it as a com,only one advantage is u can reduce the  recompilation time).
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

tdubroffCommented: what?
djelescuAuthor Commented:
Adjusted points from 250 to 270
djelescuAuthor Commented:
sisimon- It seems like your answer was actually a question. Here is my response:

I want to make it easy for end-users of my product to replace routines like GetInventory(), which presently mimics data-access behavior using rand, with real data access routines that connect to proprietary databases etc.

However, I don't want to alter my source code for each customization because my source code is proprietary and they are the ones doing the customization.

And I don't want them to recompile the application using object files because that also is unprofessional. My application should be compiled by me.

If the end-user can implement the data-access routines in a DLL and direct my program to use that DLL, then the problem is solved. I just need to give specifications for them to use in building their DLL. (And support run-time switching between the original and new DLL).

This is why I am interested in the COM/DLL approach.
I too am starting to look into COM do something very similar.  I've been reading Inside COM which is pretty good, but I haven't tried any applications yet.  You may want to email  I think he is an industry-recognized expert on COM.
djelescuAuthor Commented:
Adjusted points from 270 to 280
djelescuAuthor Commented:
Thanks for the tip!
I wil post anything I learn from dbox.
djelescuAuthor Commented:
Adjusted points from 280 to 350
djelescuAuthor Commented:
Thank you for your response.
I think your reference is a good one.

I leave the topic ungraded for now so that my colleagues can consider the issues here as well.
I will close the topic on Monday, awarding your answer with a B grade.

Feel free to add more comments to raise your grade from a B to an A.

Especially I am interested in an organized, safe way for someone to create a DLL from scratch that will work with my program even though my program was made first, which is what makes this unusual.

Hi djelescu,

If you are still pursuing COM you should check out: "Active Template Library, A Developer's Guide", by Tom Armstrong, ISBN: 1-55851-580-1  This is an excellent book.  thanks - christophm
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.