Marshal.GetHRForException does more than just Get-HR-For-Exception

Looking at the exception string is neither an option: Join Stack Overflow to learn, share knowledge, and build your career. Even then that's arguably a runtime check, as it will happen on assembly load, which could be at runtime. June 21, at 2: Dim num1, num2 As Integer. Zero means that you want the API to act just like the single-parameter overload and call GetErrorInfo , and passing -1 disables the extra info retrieval altogether. But as Fernando pointed out, both exceptions thrown in this example are InvalidCastException.

GetHRForException also sets up an IErrorInfo interface for the exception that can be obtained by calling the COM GetErrorInfoNEEDGUID function.

Your Answer

Below is the GetOutlookInternal method that appears in the stack - as you can see, it is only dealing with Outlook Interop and is unrelated to the document opening.

It does not call into IsFileLocked:. I accidentally stumbled across this article which helped to find the cause of my issue: It turns out we had two threads, one was calling Marshal. Another thread was calling Marshal. This issue can be reproduced using the following code.

Create a new console application, import the Outlook COM reference, and paste in the code. Ensure Outlook is not running when you start the application:. The solution to this problem was simply to use the Exception. Previously this property was protected but it is now accessible since we upgraded the project to. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service , privacy policy and cookie policy , and that your continued use of the website is subject to these policies.

The IsFileLocked method looks like this: The process cannot access the file 'C: The Outlook methods Below is the GetOutlookInternal method that appears in the stack - as you can see, it is only dealing with Outlook Interop and is unrelated to the document opening. It does not call into IsFileLocked: This is the approach recommended in http: ReleaseComObject ref folder ; Helpers.

Alsty 5 Can't you debug and walk through the stack trace? This seems rather odd, and I doubt the stack trace would be corrupt with an IOException at least HResult directly instead of Marshal. Anders because the HResult property has an accessibility level of protected so it's not generally accessible. Fantastic answer JaredPar, thanks In answer to your question; I need to detect this scenario because I need a friendly message for the presentation layer - it's ok if the conflict might not exist the moment after, due to the nature of my app I was getting 'Comparison to integral constant is useless; the constant is outside range of type int' and the expression evaluated to False.

I don't have enough "rep" to comment so hopefully this "answer" is OK This can be generalized into a helper function like this: Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. GetHRForException , but if you are interested in the reflection issue, the specifics depends on the specific implementation. The reflection example that I showed you attempted to the get HResult property from the Exception. Here is the screen shot of the properties available for the class.

Fields are not shown in this view, which is why I reflected against property, and not field. You would need to use a disassembler, like Reflector or Just Decompile to get the real field names. If it is a public property, then you certainly don't need to use anything heavy like reflection.

It is only public in. Or according to the documentation it is - and mine didn't compile when attempting to access the HResult directly. Another approach is to use Exception. GetPro perties Bi ndingFlags. Instance to get a list of all properties, so that you can see the entire list.

Then, you can pick out the property name that you want. Andy - That may definately be true. And this output with the Public BindingFlag: This is rather wierd - I suspect a bug somewhere in VS or. The code from Bob works with. In any case the Marshal. For anyone reading this note my final summary comment. It's more than this solution. Get answers and train to solve all your tech problems - anytime, anywhere. Try it for free Edge Out The Competition for your dream job with proven skills and certifications.

Get started today Stand Out as the employee with proven skills.

Popular Tags

loamcalculator.mlorException() is marked with a LinkDemand. Hence, my method that calls loamcalculator.mlorException() will throw an uncatchable SecurityException at the time of JIT compile, when run in a restricted environment, like . Search This Blog Afiliação de opções binárias Catanduva Marshal gethrforexception ex. Jan 18,  · if (loamcalculator.mlorException(myException) == E_SOME_COM_ERROR) DoSomething(); This looks perfectly fine, right? Not really. It turns out this API is actually poorly named, and it actually does more than just retrieving the HR from the exception object.