Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the return value from a method for an HRESULT of S_OK

Tags:

c#

hresult

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...
{
}
like image 868
saumil patel Avatar asked Dec 10 '25 15:12

saumil patel


2 Answers

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.

like image 168
Jigsore Avatar answered Dec 12 '25 04:12

Jigsore


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
}
like image 45
thumbmunkeys Avatar answered Dec 12 '25 03:12

thumbmunkeys



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!