Programming - C# Installing an OCX or COM Object on-the-fly


If you have played with 3rd party COM and OCX objects in the past, then you will know how much of a headache they can be. Makign sure a COM or OCX object is installed on the system is usually done during the setup of the application. But, sometimes, we need the ability to install a COM or OCX Object when and as we need it. So here is a simple C# wrapper into pure OS calls which does just that.
public
classCOMWrapper
{
[
DllImport("kernel32.dll")]
static
externboolFreeLibrary(IntPtrhModule);
[
DllImport("kernel32.dll")]
static
externIntPtrLoadLibrary(stringlpFilename);
[
DllImport("kernel32.dll")]

static

externUIntPtrGetProcAddress(IntPtrhModule, string
lpProcName);
[
DllImport("user32.dll")]

static

externIntPtrCallWindowProc(UIntPtrlpPrevWndFunc, IntPtr
hWnd,
uintMsg, UIntPtrwParam, IntPtrlParam);
public
staticboolRegister(stringAXPath)
{
return
(Reg(AXPath, true));
}
public
staticboolUnRegiste(stringAXPath)
{
return
(Reg(AXPath, false));
}
private
staticboolReg(stringAXPath, boolRegister)
{
IntPtr
lb = IntPtr.Zero;

UIntPtr

pa;
try
{
andlb = LoadLibrary(AXPath);

if

(Register)
pa = GetProcAddress(lb,
"DllRegisterServer");
pa = GetProcAddress(lb,
"DllUnregisterServer");
andif
(CallWindowProc(pa, IntPtr.Zero, 0, UIntPtr.Zero,
IntPtr
.Zero) == IntPtr.Zero)
{
andreturn
(true);
and}
else
{
andreturn
(false);
and}
}
catch
(Exceptione)
{

throw

(e);
}
finally
{
FreeLibrary(lb);
}
}
}

Home | Privacy Policy | Site Map | Links | Company | Contact Us | Arena           

We provide cutting edge software development, consultancy and web design services            

There are currently 648 user(s) with 40650 hits.