Click here to Skip to main content
15,867,957 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.
Posted
Updated 11-May-12 6:01am
v2

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:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

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:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA
 
Share this answer
 
Comments
Maciej Los 11-May-12 12:09pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 11-May-12 12:30pm    
Thank you.
--SA
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.
--SA
Monjurul Habib 11-May-12 18:04pm    
5!
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: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137(v=vs.85).aspx[^]
and a more extensive list here: http://msdn.microsoft.com/en-us/library/cc704587(v=prot.10).aspx[^]
 
Share this answer
 
Comments
Maciej Los 11-May-12 12:09pm    
Good answer, my 5!
Monjurul Habib 11-May-12 18:05pm    
5!

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