The documentation of IDirectInputDevice8::Acquire says:

Return Value

If the method succeeds, the return value is DI_OK, or S_FALSE if the device was already acquired. If the method fails, the return value can be one of the following error values: DIERR_INVALIDPARAM, DIERR_NOTINITIALIZED, DIERR_OTHERAPPHASPRIO.

However, SharpDX's Device.Acquire() is a void. The documentation for this method is not really helpful.

How can I know if a call to Device.Aquire() has succeeded or not?

Alternatively, where is the source for Acquire() located? I've tried looking at the contents of Device.cs, but not only it is not located in there, but Device is marked as partial, which means the source for Acquire() is probably in a different file, but I can't seem to find it.


2 Answers 2


I believe the correct way to use the Acquire method is to assume it has worked correctly - if it fails to acquire the device, a SharpDXException will be thrown from the Acquire method. In this way, there is no need for a return value.

A disassembly of the Acquire method shows the code as:

public unsafe void Acquire()
  // ISSUE: cast to a function pointer type
  // ISSUE: function pointer call
  (Result) (__calli((__FnPtr<int (void*)>) *(IntPtr*) (*(IntPtr*) this._nativePointer + IntPtr(7) * sizeof (void*)))(this._nativePointer)).CheckError();

The CheckError call is a method on the Result struct, which looks like this:

/// <summary>
/// Checks the error.
/// </summary>
public void CheckError()
    if (_code < 0)
        throw new SharpDXException(this);

(Source from https://github.com/sharpdx/SharpDX/blob/b1596ddd38a5de77704412910a4fb265a4ab8525/Source/SharpDX/Result.cs)


I suppose you could invoke GetLastError(). but depending on how your program is structured it might contain a stale value (for example, if an earlier call failed and Acquire() succeeded but did not clear the error code) and it might not be accurate depending on how that SharpDX function is written.

You could work around the "stale" issue by invoking SetLastError() to prime (clear) the error code before invoking SharpDX.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .