OpenEvent SetEvent

G

Gionni

I have to translate this code in VB6 in C#:

Declare Function OpenEvent Lib "kernel32.dll" Alias "OpenEventA" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal lpName As String) _
As Long

Declare Function SetEvent Lib "kernel32.dll" _
(ByVal hEvent As Long) _
As Long

Public Const EVENT_MODIFY_STATE As Long = 2

Private Sub sendTrap()
Dim sdsTrap As Long
sdsTrap = OpenEvent(EVENT_MODIFY_STATE, 0, "_sdsTrapEvent")
If sdsTrap <> 0 Then
SetEvent(sdsTrap)
End If
End Sub

I tried this:

public const int EVENT_MODIFY_STATE = 2;

[DllImport("KERNEL32.DLL", EntryPoint="OpenEventA")]
private static extern long OpenEvent
(
long dwDesiredAccess,
long bInheritHandle,
[MarshalAs(UnmanagedType.LPStr)] string lpName
);

[DllImport("KERNEL32.DLL")]
private static extern bool SetEvent
(
long hEvent
);

public void sendTrap()
{
long sdsTrap;
sdsTrap = OpenEvent(EVENT_MODIFY_STATE, 0, "_sdsTrapEvent");
if (sdsTrap != 0)
{
SetEvent (sdsTrap);
}
}

The OpenEvent seems to be ok, but the SetEvent fails (I tested and it
returns false).

How could I do this?

Thanks in advance, Gionni
 
M

Mattias Sjögren

[DllImport("KERNEL32.DLL", CharSet=CharSet.Auto)]
private static extern IntPtr OpenEvent
(
uint dwDesiredAccess,
bool bInheritHandle,
string lpName
);

[DllImport("KERNEL32.DLL")]
private static extern bool SetEvent
(
IntPtr hEvent
);



Mattias
 
G

Gionni

Thanks a lot, Mattias. I did not know where I could find the right signatures.

Cheers, Gionni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top