Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do I have to "RUN RegSvr32.exe" to register an ActiveX OCX file?

COM and Automation

Do I have to "RUN RegSvr32.exe" to register an ActiveX OCX file?

by  wgcs  Posted    (Edited  )
No, you don't.

Part of the definition of an OCX file (which is really just a renamed .DLL file) is that the self registering / unregistering functions be included in the DLL, so all you have to do is:

1) Load the library
2) Find the entry point for DllRegisterServer or DllUnregisterServer
3) Call the appropriate function.

This is easy to do, but to make it easier, just use the RegSvr function below:

Code:
cResult = RegSvr('MyOcx.ocx')     && Register
cResult = RegSvr('MyOcx.ocx',.t.) && Register
cResult = RegSvr('MyOcx.ocx',.f.) && Unregister

FUNCTION RegSvr
* Author.....: William GC Steinford
* Date.......: Jan 23, 2003
* Abstract...: Programmatically Register, Unregister an OCX control
LPARAMETERS pcOCX,plReg
LOCAL llReg
llReg = iif( PCount()>1, plReg, .T. ) && Default to register

DECLARE INTEGER LoadLibrary IN kernel32 As "LoadLibraryA"; 
    STRING lpLibFileName 

LOCAL lhLib
lhLib = LoadLibraryA( pcOCX )

if (lhLib < 32) && HINSTANCE_ERROR
  RETURN "ERROR: Could not Load Library "+pcOCX
ENDIF

*// Find the entry point.
DECLARE INTEGER GetProcAddress IN kernel32;  
    INTEGER hModule,;  
    STRING lpProcName

if llReg
  lpRegProc = GetProcAddress(lhLib, 'DllRegisterServer')
  if lpRegProc>0 && Procedure exists!
    DECLARE INTEGER DllRegisterServer IN &pcOCX AS OcxReg 
    lnRes = OcxReg()
    if lnRes=0
      RETURN "SUCCESS"
    else
      RETURN "ERROR: DllRegisterServer returned "+Tran(lnRes)
    endif
  ELSE
    * //unable to locate entry point
    RETURN "ERROR: Library "+pcOCX+" has no entry point for DllRegisterServer"
  ENDIF
ELSE && Unregister
  lpRegProc = GetProcAddress(lhLib, 'DllUnregisterServer')
  if lpRegProc>0 && Procedure exists!
    DECLARE INTEGER DllUnregisterServer IN &pcOCX AS OcxUnReg 
    lnRes = OcxUnReg()
    if lnRes=0
      RETURN "SUCCESS"
    else
      RETURN "ERROR: DllUnregisterServer returned "+Tran(lnRes)
    endif
  ELSE
    * //unable to locate entry point
    RETURN "ERROR: Library "+pcOCX+" has no entry point for DllUnregisterServer"
  ENDIF
ENDIF
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top