unhandled exception class System.NullReferenceException

  • Thread starter Thread starter Merlynx
  • Start date Start date
M

Merlynx

i am getting that error with the Event() method.
Don't know whether if i have used the CreateEvent function well as it is the
1st time i used it moreover i am net to C#.
If anyone can point me out the error it would be great.
TIA

public class Class
{

public struct SECURITY_ATTRIBUTES{
public long nLength;
public long IpSecurityDescriptor;
public long bInheritHandle;
public SECURITY_ATTRIBUTES(long nLength,long
IpSecurityDescriptor,long bInheritHandle){
this.nLength = nLength;
this.bInheritHandle = bInheritHandle;
this.IpSecurityDescriptor =
IpSecurityDescriptor;
}
}

[DllImport("kernel32")]
public static extern long CreateEvent
(SECURITY_ATTRIBUTES lpEventAttributes,bool bManualReset,bool
bInitialState,string lpName);
public void Event(){
SECURITY_ATTRIBUTES Sd = new SECURITY_ATTRIBUTES();
Sd.nLength = Marshal.SizeOf(Sd);
Sd.bInheritHandle = 0;
Sd.IpSecurityDescriptor = 0;
long Test = CreateEvent(Sd,true,false,"Test");
}
}
 
Hi,

Merlynx said:
i am getting that error with the Event() method.
Don't know whether if i have used the CreateEvent function well as it is the
1st time i used it moreover i am net to C#.
If anyone can point me out the error it would be great.
TIA

public class Class
{
prepend [StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES{
public long nLength;
public long IpSecurityDescriptor;
public long bInheritHandle;
public SECURITY_ATTRIBUTES(long nLength,long
IpSecurityDescriptor,long bInheritHandle){
this.nLength = nLength;
this.bInheritHandle = bInheritHandle;
this.IpSecurityDescriptor =
IpSecurityDescriptor;
}
}

[DllImport("kernel32")]
public static extern long CreateEvent
(SECURITY_ATTRIBUTES lpEventAttributes,bool bManualReset,bool
bInitialState,string lpName);

CreateEvent expects a pointer to a struct, a struct is a value type, so you
must use the ref keyword.
A handle has a platform dependend size, like IntPtr does.

public static extern IntPtr CreateEvent( ref SECURITY_ATTRIBUTES
lpEventAttributes, bool bManualReset, bool bInitialState, string lpName );

Keep in mind that net supports events, see AutoResetEvent and
ManualResetEvent classes...

HTH
greetings
 
Back
Top