Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hello everyone

Can some one tell me if there is an easy way to use HRESULT in C#?
I have a DLL that uses HRESULT for error/success codes and I am trying to use this DLL in my c# code. I can not add a reference to the DLL file. VS2010 .NET 4.0 does not recognize the dll file as an assembly component. Therefore I use import dll file using interopservice and entry point to the functions.
Functions should be defined as HRESULT someFunction() and the return code I need to evaluate it using SUCCEEDED(HRESULT hr) and FAILED(HRESULT hr).
Thanks for the help.
Updated 11-May-12 6:01am

The question has nothing to do with HRESULT. You would need to use it the same ways as in COM. The only problem is using unmanaged DLL in your .NET assembly.

There are two ways to do it.

First one is P/Invoke. If you need learn P/Invoke, please see:[^],[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

Another approach is using C++/CLI. You can create a mixed-mode (managed+unmanaged) C++/CLI DLL. You can mix C++ with C++/CLI and wrap all native calls to your native DLL into .NET C++/CLI "ref" classes. The obtained executable can be used like a regular .NET assembly and referenced by your C# assembly.

About C++/CLI, please see:[^],[^],[^].

Share this answer
Maciej Los 11-May-12 12:09pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 11-May-12 12:30pm    
Thank you.
Member 8456258 11-May-12 12:37pm    
This is a good solution however due to lack of time I used solution 2. Thank you.
Sergey Alexandrovich Kryukov 11-May-12 13:54pm    
You are welcome. If you agree it makes sense, please accept if formally (green button) -- thanks.
Monjurul Habib 11-May-12 18:04pm    
HRESULT is a Int32 value - so use that in your DLLImport declaration, and check the actual int returned against the range of valid values. There is a list of common ones here:[^]
and a more extensive list here:[^]
Share this answer
Maciej Los 11-May-12 12:09pm    
Good answer, my 5!
Monjurul Habib 11-May-12 18:05pm    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900