Click here to Skip to main content
15,443,631 members
Articles / Programming Languages / C++
Posted 12 Apr 2005


27 bookmarked

Registering any COM component through coding

Rate me:
Please Sign up or sign in to vote.
1.91/5 (7 votes)
12 Apr 2005
Registering any COM component through coding.


Many a times, it happens that when we are using COM components in our application and if it is not registered with the system or unregistered accidentally, our application breaks in the middle.. So, it is a good habit for a programmer to re-register the COM components in the beginning of the application..

Sometimes we find the need to register DLLs from the code.. So, this code snippet will help you register any DLL/OCX by just passing the absolute path of the component into the function...

Using the code

Copy the following function into your code (as a global function) and call that function as shown below...

//Developed By : Jigar Mehta
//Date : [13/04/2005 11:08:16]
//If returns    Zero, DLL successfully registered...
//        -2 means DLL can not be loaded..
//        -3 means DLL Entry point can not be found..
//        -4 means Could not register the file... 
//                 DLL Registration failed..
int RegisterComponent(char *absPath)
    HINSTANCE hDLL = LoadLibrary(absPath);
    if(hDLL == NULL)
        //-2 means DLL can not be loaded..
        return -2;            

    typedef HRESULT (CALLBACK *HCRET)(void);
    HCRET lpfnDllRegisterServer;
    lpfnDllRegisterServer = 
         (HCRET)GetProcAddress(hDLL, "DllRegisterServer");

    if(lpfnDllRegisterServer == NULL)
        //-3 means DLL Entry point can not be found..
        return -3;            

    //Call the function by function pointer..
        //-4 means Could not register the file... 
        //DLL Registration failed..
        return -4;            
    return 0;

How to call this function:

int nVal = RegisterComponent("C:\\ZButton.OCX");
if(nVal == 0)
    AfxMessageBox("Component Successfully Registered...");
else if(nVal == -2)
    AfxMessageBox("DLL can not be loaded..\r\nReason could "
       "be path is incorrect\r\nor.. Component is corrupt");
else if(nVal == -3)
    AfxMessageBox("DLL Entrypoint for function "
                 "DLLRegisterServer could not be found..");
else if(nVal == -4)
    AfxMessageBox("DLL Registration Failed..");
    AfxMessageBox("Unknown error in registering the file..");

Points of Interest

So, it's just calling the function with proper arguments and the DLL will be registered.. Happy coding..


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
India India
Completed my M.Sc. in Computer Application and Information Technology and was giving my services to GAYTES Information Systems Private Ltd. - developing telecommunication solutions, like Voicemail Systems, IVRS, Call Center Applications, Unified Messaging etc, worked as Team Leader. Currently giving my services to VARAHA Systems working on embedded platform.

Comments and Discussions

QuestionWhat about to export tlb? Pin
Md. Marufuzzaman28-May-09 1:47
professionalMd. Marufuzzaman28-May-09 1:47 
I do not have the same opinion, sometimes we need to export tlb files.

Md. Marufuzzaman

GeneralDo not really agree... Pin
ReorX12-Apr-05 23:13
MemberReorX12-Apr-05 23:13 
GeneralRe: Do not really agree... Pin
Jigar Mehta12-Apr-05 23:25
MemberJigar Mehta12-Apr-05 23:25 
GeneralRe: Do not really agree... Pin
jojok8-Jul-05 6:55
Memberjojok8-Jul-05 6:55 

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.