1

I am trying to call WaitForSingleObject method from C#, as documented here:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx

In order to call this function I need to create a Handle, or I need to get a Handle of type IntPtr, how can it be done?

I've tried this function that I found: http://www.pinvoke.net/default.aspx/kernel32.WaitForSingleObject

[DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
    public static extern IntPtr CreateEvent(HANDLE lpEventAttributes, [In, MarshalAs(UnmanagedType.Bool)] bool bManualReset, [In, MarshalAs(UnmanagedType.Bool)] bool bIntialState, [In, MarshalAs(UnmanagedType.BStr)] string lpName);

Or for instance, when I am getting handle from console:

IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;

It throws a DllNotFoundException.

What's the issue here?

I need it in order to run the process with this function call, and to take a dump form its process, for my ClrMd library learning.

Any help will be appreciated.

Code sample:

  static void Main(string[] args)
    {
         var autoEvent = new AutoResetEvent(false);

         //this is where I get the DllNotFoundException
         WaitForSingleObject(autoEvent.Handle, WAIT_TIMEOUT );
    }


    [DllImport("kernel32.dll")]
    static extern uint WaitForMultipleObjects(uint nCount, IntPtr[] lpHandles, bool bWaitAll, uint dwMilliseconds);

public const Int32 WAIT_TIMEOUT = 0x102;
Pavel Durov
  • 1,287
  • 2
  • 13
  • 28
  • 3
    The much better question is *why* you want to do that (the current explanation is.. confusing to say the least). The only reason would be if you're interacting with an existing library, but in that case you'd obviously already have the handle. In all other situations there are c# classes that are better to use. In any though there's no such thing as a "coredll.dll" that's all in the kernel32 as the documentation says – Voo Nov 15 '15 at 10:03
  • Well, the only answer that I can give you on "why", is just cause I'm trying things out , I'm trying to figure out why it doesn't work... – Pavel Durov Nov 15 '15 at 10:14
  • 1
    When I use Process.GetCurrentProcess().Handl - I am getting Unable to load DLL 'coredll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) as well – Pavel Durov Nov 15 '15 at 10:21
  • 1
    As I said, no such thing as coredll.dll the functions are in kernel32. If you want to write native code, you have to learn how that works, otherwise you'll run into way bigger troubles (like having the wrong pinvoke definition for your strings) than not knowing where in the documentation the dll that exports those symbols is specified (right at the end btw). – Voo Nov 15 '15 at 10:33
  • Ok, I get it - but even when I am not using coredll.dll I get DllNotFoundException – Pavel Durov Nov 15 '15 at 10:35
  • 1
    Can you elaborate more on what you are trying to "try out and learn"? Passing random handles does not accomplish anything that I can think of... (In particular window handles are completely incompatible with this API). – usr Nov 15 '15 at 10:50
  • I want to call native method (WaitForMultipleObjects) which waits for some handle (don't really mind which one), then I want to see it on thread stack using ClrMd library, from dump file - that's it :). – Pavel Durov Nov 15 '15 at 11:01
  • Sorry, my mistake I thought I've changed to kernel32.dll but I didn't, also I've posted WaitForMultipleObjects instead of WaitForSingleObject - sorry – Pavel Durov Nov 15 '15 at 11:14

3 Answers3

2

I would not go through WinApi to get this from C#: you have EventWaitHandler and other synchronization objects in C#, use them:

WaitHandle wh = new EventWaitHandler();
//do whatever you need
...
WaitHandler.WaitOne(wh); // equivalent to WaitForSingleObject in WinApi

you can use wh.SafeWaitHandle if you really need to interop with WinApi

Also I suspect Process.GetCurrentProcess().MainWindowHandle cannot work in a Console Application, that has not any window at all

Gian Paolo
  • 4,161
  • 4
  • 16
  • 34
  • I'm not trying to find a better solution, I am trying to find why it doesn't work. Is there any other valid Handle that I can pass to WaitForSingleObject function? Maybe Process.GetCurrentProcess().Handle? – Pavel Durov Nov 15 '15 at 10:16
  • try to use wh.SafeWaitHandle as HANDLE to pass to WaitForSingleObject. Of course you need an Handle of a synchronization object, not of a window. – Gian Paolo Nov 15 '15 at 10:20
  • Still getting Unable to load DLL 'coredll.dll', I've used AutoResetEvent as synchronization object. Maybe there is something that I miss.. – Pavel Durov Nov 15 '15 at 10:26
  • 2
    post some more detailed code, it's impossible to help without – Gian Paolo Nov 15 '15 at 10:33
1

I want to call native method (WaitForMultipleObjects) which waits for some handle (don't really mind which one), then I want to see it on thread stack using ClrMd library, from dump file

OK, so what about new ManualResetEvent(false).WaitOne()? This should show up in the dump file. And it's reliable.

Just picking any existing handle is not reliable because it might be signaled or be destroyed at any time. Or, you might change its state by waiting. There is no need, a ManualResetEvent can create you a fresh handle.

usr
  • 168,620
  • 35
  • 240
  • 369
0

My mistake I've posted WaitForMultipleObjects instead of WaitForSingleObject, the main issue was that WaitForSingleObject stayed with DllImport("coredll.dll"...) I don't know where did I found it but I did...

Sorry for the confusion

Pavel Durov
  • 1,287
  • 2
  • 13
  • 28