Is it possible that ctrl.Handle.ToInt32() returns null?

  • Thread starter Thread starter Jack Wright
  • Start date Start date
J

Jack Wright

Dear All,
I am trying to pass a Window Handle to a Win32 dll function...but I
sometimes get NullReferenceException...this is usually the first time
I start my application...here is the sample code...

MyControl: Control
{
....
protected override void OnCreateControl()
{
base.OnCreateControl();
SCSkin.ExcludeWnd(this.Handle.ToInt32(),0);
}

}

ExcludeSkin is does an call... this is the code in ildasm...
..method public hidebysig instance int32 ExcludeWnd(int32 hWnd,
int32
withChildren) cil managed
{
// Code size 8 (0x8)
.maxstack 2
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: call int32 SkinCrafter.SCSkin::impExcludeWnd(int32,
int32)
IL_0007: ret
} // end of method SCSkin::ExcludeWnd

Am I doing something wrong? Please help...

TALIA

Many Regards
Jack
 
Jack,
You need to call CreateControl(), this will then trigger your
OnCreateControl method after it has created the handle. I assume you
are explicitly calling your OnCreateControl method because otherwise
there would already be a handle. If I am right (you are explicitly
calling OnCreateControl then you need to replace your call to
OnCreateControl() with CreateControl this will ensure you have a
handle and then the base will automatically call your OnCreateControl
override. you may also want to wrap your call in a if(!this.created).

Hope this helps

Cecil Howell

The CreateControl method forces a handle to be created for the control
and its child controls. This method is used when you need a handle
immediately for manipulation of the control or its children; simply
calling a control's constructor does not create the Handle.
 
I am not explicitly calling OnCreateControl method...it gets called
automatically...I am not doing anything special...

Thanks & Regards
Jack
 
Back
Top