Click here to Skip to main content
Click here to Skip to main content
Go to top

Registering any COM component through coding

, 12 Apr 2005
Rate this:
Please Sign up or sign in to vote.
Registering any COM component through coding.

Introduction

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..
    if(FAILED((*lpfnDllRegisterServer)()))            
    {
        //-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..");
}
else
{
    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..

License

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

Share

About the Author

Jigar Mehta
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? PinmemberMd. Marufuzzaman28-May-09 1:47 
GeneralDo not really agree... PinmemberReorX12-Apr-05 23:13 
GeneralRe: Do not really agree... PinmemberJigar Mehta12-Apr-05 23:25 
GeneralRe: Do not really agree... Pinmemberjojok8-Jul-05 6:55 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 13 Apr 2005
Article Copyright 2005 by Jigar Mehta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid