Click here to Skip to main content
13,052,342 members (41,567 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 23 Apr 2002

Calling COM DLLs from Console Applications

, 24 Apr 2002
Rate this:
Please Sign up or sign in to vote.
This Article explains how to call a COM DLL from a Console Application


This article shows how to call a VC++ DLL from a Console Application in C.

Getting Started

  • Create a new ATL project for a DLL , Click Finish.
  • Use ATL Object Wizard to add a simple object, Short Name as mydll , accept defaults.
  • Using Class View Add Method to the new interface , Method Name as multiply and parameters as '[in] int ifirst,[in] int isecond,[out] int* result'.

Your method should look like this:

STDMETHODIMP Cmydll::multiply(int ifirst, int isecond, int *result)
  // TODO: Add your implementation code here
  *result = ifirst * isecond;

  return S_OK;
Build and register the DLL. Now Create a Win32 Console Application named 'CallCDLL'.


#include <stdio.h>
#include <objbase.h>
#include "dll4C.h" //contains prototype of method
#include "dll4C_i.c" //contains the Class ID ( CLSID ) 
//and Interface ID ( IID )
Your Main() should like this
int main(void)
  IClassFactory* pclsf;
  IUnknown* pUnk;
  Imydll* pmydll;
  int x,y;
  int result;
  //Initialize the OLE libraries

  //get the IClassFactory Interface pointer

    printf("CoGetClassObject failed with error %x\n",hr);
    return 1;

  //Use IClassFactory's CreateInstance to create the COM object
  //and get the IUnknown interface pointer
  hr = pclsf->CreateInstance(NULL,IID_IUnknown,(void**)&pUnk);

    printf("ClassFactory CreateInstance failed with error %x\n",hr);
    return 1;

  //Query the IUnknown to get to the Imydll interface
  hr = pUnk->QueryInterface(IID_Imydll,(void**)&pmydll);

    printf("QueryInterface failed with error %x\n",hr);
    return 1;

  //Use Imydll interface for multiplications
  printf("Input two numbers to multiply:\n");
  pmydll->multiply(x,y,&result);//call the method using 
                         //the interface pointer

  printf("The product of the two numbers %d and %d = %d\n",
    x,y,result);//print the result

  //Release the interface pointers

  return 0;


That's it. Write to me for any explanations/suggestions.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amol Kakhandki
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralNice for beginner.. Pin
yogeshratna27-Mar-10 4:53
memberyogeshratna27-Mar-10 4:53 
Generalwrong multiply result Pin
fatal00531-May-06 4:03
memberfatal00531-May-06 4:03 
GeneralCreateInstance fail Error Pin
amitsax7627-Feb-06 0:06
memberamitsax7627-Feb-06 0:06 
Generalif we call from asp Pin
uno20037-Oct-03 22:29
memberuno20037-Oct-03 22:29 
GeneralA simpler way Pin
Serge Weinstock24-Apr-02 21:40
memberSerge Weinstock24-Apr-02 21:40 
GeneralRe: A simpler way Pin
Amol Kakhandki24-Apr-02 22:49
memberAmol Kakhandki24-Apr-02 22:49 
GeneralUse smart pointer in the client Pin
Dudi24-Apr-02 21:12
memberDudi24-Apr-02 21:12 
GeneralRe: Use smart pointer in the client Pin
Amol Kakhandki24-Apr-02 22:48
memberAmol Kakhandki24-Apr-02 22:48 
QuestionWhy use ClassFactory? Pin
Anonymous23-Apr-02 22:35
memberAnonymous23-Apr-02 22:35 
AnswerRe: Why use ClassFactory? Pin
Amol Kakhandki23-Apr-02 23:32
memberAmol Kakhandki23-Apr-02 23:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 25 Apr 2002
Article Copyright 2002 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid