2

I am trying to convert C# project to Delphi, the C# code itself is importing functions from native dll let us name it ‘dmp.dll’ which I don’t have the signature of its native functions and I have to look at how these functions are imported in c# and try to import them in Delphi code, and I did import a lot of them and they work fine, but now I am struggling with this function (StartLogging). This is how they imported in C#:

[DllImport("dmp.dll", CharSet = CharSet.Auto, EntryPoint = "StartLogging")]
public static extern int StartLogging(String AdapterName, 
  PLOG_RECORD_CALLBACK LogRecordCallback, 
  SafeWaitHandle StopLoggingEvent);

no problem about PLOG_RECORD_CALLBACK, but the problem is on this parameter SafeWaitHandle which is class exists in Microsoft.Win32.SafeHandles namespace. How can I port it to Delphi? What is the equivalent data type to it in Delphi?

And here is how they use it in the C# Code:

AutoResetEvent StopEvent = new AutoResetEvent(false);

The class AutoResetEvent exists in System.Threading Then they call the method like this:

StartLogging(comboBox1.Text, CallbackProcedure, StopEvent.SafeWaitHandle);

Then at the last and to stop the logging they use:

StopEvent.Set();

I am really confused and I don’t know how to do this, appreciate your help. Thanks

Ken White
  • 123,280
  • 14
  • 225
  • 444
Robin-Hood
  • 345
  • 3
  • 11

2 Answers2

2

You can probably use SyncObjs TEvent. If you create it with the ManualReset argument set to False, it should work about the same way. Just pass the Event.Handle (which is a THandle and is compatible with anything in the API expecting one).

I don't have a sample of using a non-manual reset event, but an example of creating a TEvent can be found in the accepted answer here; to make it a non-manually reset (IOW, AutoReset), just change the second parameter to False.

Community
  • 1
  • 1
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • +1 But it would be nice if you identified how to reproduce AutoResetEvent in Delphi and that SafeWaitHandle is a handle to a win 32 event. That's why I only commented. – David Heffernan Nov 23 '11 at 07:52
  • `TLightweightEvent` is a manual reset event without a wait handle. Can't use that here. `TSimpleEvent` is identical to `TEvent`: `TSimpleEvent = class(TEvent);` The answer is `TEvent` created with `ManualReset` set to `False`. I won't post an answer because you already did, but you need to improve it in my view. – David Heffernan Nov 23 '11 at 12:19
  • @David: Better? I'd like it to be more complete with an example of the event use, but don't have the time right now. If you want to post a better answer, I'll remove mine afterward. – Ken White Nov 23 '11 at 18:35
0

Like David Hefferman said in a response to the approved answer: TSimpleEvent (System.SyncObjs) works the same way as the AutoResetEvent of C#.

It can be used like:

_WaitEvent := TSimpleEvent.Create(nil, resetmanual {boolean}, false, '', false);
_WaitEvent.SetEvent;
_WaitEvent.WaitFor;

If you use the manual reset, just simply use: _WaitEvent.ResetEvent;

JvA
  • 63
  • 7