Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

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)

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.