Registering OCX programmatically fails
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Registering OCX programmatically fails

  1. #1
    Join Date
    Nov 2003
    Posts
    2,185

    Registering OCX programmatically fails

    Hi all,

    I want to register an ocx like this:

    Code:
    	// Declare variables
    	bool bFailed = false;
    	HMODULE hMod;
    	DLLREGISTERSERVERPROC DllRegisterServerFunction;
    
    	// Register file
    	hMod = LoadLibrary("mycontrol.ocx");
    
    	// Check if we can load library
    	if (hMod != NULL)
    	{
    		// Get proc address
    		DllRegisterServerFunction = (DLLREGISTERSERVERPROC) GetProcAddress(hMod, "DllRegisterServer");
    
    		// Check if we can run the function
    		if (DllRegisterServerFunction != NULL)
    		{
    			// Register
    			switch (DllRegisterServerFunction())	
    			{
    			case S_OK:
    				bFailed = false;
    				break;
    
    			case SELFREG_E_TYPELIB:
    				bFailed = true;
    				break;
    
    			case SELFREG_E_CLASS:
    				bFailed = true;
    				break;
    
    			case  E_OUTOFMEMORY:
    				bFailed = true;
    				break;
    
    			case E_UNEXPECTED:
    				bFailed = true;
    				break;
    
    			default:
    				bFailed=false;
    				break;
    			}
    		}
    		else
    		{
    			bFailed = true;
    		}
    	}
    	else
    	{
    		// We failed
    		bFailed = true;
    	}
    
    	// Return result
    	return !bFailed;
    I have inserted all the errors in the switch state, but it still continues at default. Any idea what is going wrong?

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Registering OCX programmatically fails

    Here's some code that works for me...

    Code:
        FARPROC lpDllEntryPoint=NULL;
            //  find the entry point.
            lpDllEntryPoint = (FARPROC)GetProcAddress(hLib, _T("DllRegisterServer"));
            if (lpDllEntryPoint != NULL)
            {
                rc = (*lpDllEntryPoint)();
                if (rc != S_OK)
                {
                    CString msg;
                    msg.Format("Bad RC from DllRegisterServer");
                    AfxMessageBox(msg);
                }
            }
    Gort...Klaatu, Barada Nikto!

  3. #3
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Registering OCX programmatically fails

    yes, I am using exact the same code. Until someone complained that the ocx wasn't registering.

    The ocx function is not returning S_OK, but GetPRocAddress returns a valid address! That's the weird thing about it. I started adding the switch statement to see why it fails, but I cannot find the error.

  4. #4
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Registering OCX programmatically fails

    Have you tried using the Dependency walker to check for missing dlls?
    Gort...Klaatu, Barada Nikto!

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Registering OCX programmatically fails

    Well, you can start by catching the actual value returned by the function:
    Code:
    HRESULT hres = DllRegisterServerFunction();
    switch(hres)
    {
     //..
    }
    Then, you can lookup in the header where SELFREG_E_TYPELIB and all the others are defined, and identify the return code.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Registering OCX programmatically fails

    Quote Originally Posted by Mike Harnad
    Have you tried using the Dependency walker to check for missing dlls?
    That was the first doubt I had too. However,OP is able to load the dll successfully. So would rule that out.

    My next suspicion is, the OP is running this as a non-admin , so DllRegisterServer is unable to make any registry entries.

    To OP, did you try if running

    regsvr32 ocx

    works using the same logon ?

  7. #7
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Registering OCX programmatically fails

    well, I am not having problems, but the one encountering problems is no admin, but he says regsvr32 works!

    I asked him if the function inside the ocx is correct, but it is a 3rd party control.

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Registering OCX programmatically fails

    How exactly is s/he using regsvr32 ? What params is s/he passing ?
    Also, as others pointed out, do you know what the return value of the function call is ?

  9. #9
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Registering OCX programmatically fails

    One checks a HRESULT for Success using the SUCCEEDED macro.

    So, the correct thing to do is -
    Code:
    HRESULT hr = E_FAIL;
    
    if (SUCCEEDED (hr = DllRegisterServerFunction())
    {
      // Registration succeeded
    }
    else
    {
      // Registration failed
    }
    There can be more success states than S_OK, and more failure states than ones you've listed.

    Hence, the code entering default does not necessarily imply registration success or failure.
    Last edited by Siddhartha; November 1st, 2005 at 01:19 PM.

  10. #10
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Registering OCX programmatically fails

    As Cilu pointed out you should first find the actual result code and see what it means. Or we might start a wild goose chase here.
    Last edited by PadexArt; November 1st, 2005 at 01:28 PM.
    Har Har

  11. #11
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Registering OCX programmatically fails

    I think you have a point there using SUCCEEDED. I am going to try that!

  12. #12
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Registering OCX programmatically fails

    I used this code now:

    Code:
    HRESULT hRes = DllRegisterServerFunction();
    if (FAILED(hRes))
    {
        // We failed
        bFailed = true;
    }
    The value of hRes is -2147221008

  13. #13
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Registering OCX programmatically fails

    Quote Originally Posted by Tischnoetentoet
    I used this code now:

    Code:
    HRESULT hRes = DllRegisterServerFunction();
    if (FAILED(hRes))
    {
        // We failed
        bFailed = true;
    }
    The value of hRes is -2147221008
    Error Lookup tells -
    "CoInitialize has not been called."
    Wierd message this given the fact that COM Library Initialization is not needed for Registration unless DllRegisterServer itself instantiates a COM Object (which is a bad practise).

    Insert this -
    Code:
    ::CoInitialize (NULL);
    before you call DllRegisterServer and let us know if it helps...

  14. #14
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Registering OCX programmatically fails

    which means

    [CoInitialize has not been called. ]

    To know how I got this, if you are using Visual Studio, go to start menu ->programs->Visual studio->Visual Studio tools->Error lookup. and punch in the error code there.

    Alternately, you should see this same tool under tools menu under VC 6.0

  15. #15
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Registering OCX programmatically fails

    thanks Kirants for pointing that out!

    Using ::CoInitialize (and of course ::CoUnintialize()), it works!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center