Question

How can I compare the value returned from a method with HRESULT? I tried this but it is not working:

FPropStg.DeleteMultiple(1, psProp) == VSConstants.S_OK

The type defintion for DeleteMultiple() is:

HRESULT IPropertyStorage.DeleteMultiple(Ulong, Propspec)

I have written VSConstants.S_OK. Is there a way I can write S_OK directly? I made an attempt to do so but got an error that indicated that S_OK does not exist in the current context.

I also checked HRESULT against the Windows common system-wide codes. But the value I am receiving for HRESULT is not in that list. Note that I have included the namespaces System.Exception and System.Security.Cryptography.StrongNameSignatureInformation.

All that said, I basically have two questions:

  1. Is there a way to write S_OK instead of VSConstants.S_OK?
  2. How can I compare the return value of a method with S_OK?
HRESULT hr = FPropStg.DeleteMultiple(1, psProp);

if (hr == S_OK) // S_OK does not exist in the current context...
{
}
Was it helpful?

Solution

What if you set PreserveSig to false? Something like this:

You declare the function similar to this (I made it up, I don't know the exact signature...but you do)

[DllImport("ole32.dll", EntryPoint = "DeleteMultiple", ExactSpelling = true, PreserveSig = false)]
public static extern void DeleteMultiple(ulong cpspec, PropSpec[] rgpspec);

and call it this way

try
{
    FPropStg.DeleteMultiple(1, psProp);
}
catch (Exception exp)
{
    MessageBox.Show(exp.Message, "Error on DeleteMutiple");
}

Explanation: when PreserveSig is false you can omit the returned HRESULT value, but internally this value is actually checked, so if HRESULT is different from S_OK an exception is thrown.

OTHER TIPS

You can use this enum to define OK, it is from pinvoke:

enum HRESULT : long
{
S_FALSE = 0x0001,
S_OK = 0x0000,
E_INVALIDARG = 0x80070057,
E_OUTOFMEMORY = 0x8007000E
}

HRESULT is simply an unsigned 32bit integer value. You can build your own constants class to help you make these comparisons:

public static class HResults
{
    public static readonly int S_OK = 0;
    public static readonly int STG_E_ACCESSDENIED =  unchecked((int)0x80030005);
}

Used like:

if (HResults.S_OK == FPropStg.DeleteMultiple(1, psProp))
{
    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top