Click here to Skip to main content
15,356,094 members
Articles / Programming Languages / C++
Article
Posted 12 Apr 2005

Stats

42.6K views
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.

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? Pin
Md. Marufuzzaman28-May-09 1:47
professionalMd. Marufuzzaman28-May-09 1:47 
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 
See.. First of all thanks for ur precious comment..

I am talking about application's Proprietary components that are developed by the same company and used by the application.. And you can always give relative path like ".\\SystemFiles\\XXX.ocx" to register the copy that is used by the application.. And if we take the thing into consideration that newer version of OCX at some other place would disturb this and other applications.. But we should also take it into account that what if newer version of Component is not having methods which were supported by older version.. (deprecated methods are removed..) So, I still always prefer to ship perfect version of components with software and install them in some folder in application path, and we can always register them through relative path through method shown above...

Thanks again for discussion.. Rose | [Rose]

Jigar Mehta
(jigarmehta@gatescorp.com)

Software Developer
Gates Information Systems
India
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.