Click here to Skip to main content
11,641,416 members (59,603 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DLL Interop
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 11-May-12 5:56am
Edited 11-May-12 6:01am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
losmac at 11-May-12 12:09pm
   
Good answer, my 5!
SAKryukov at 11-May-12 12:30pm
   
Thank you.
--SA
Member 8456258 at 11-May-12 12:37pm
   
This is a good solution however due to lack of time I used solution 2. Thank you.
SAKryukov at 11-May-12 13:54pm
   
You are welcome. If you agree it makes sense, please accept if formally (green button) -- thanks.
--SA
Monjurul Habib at 11-May-12 18:04pm
   
5!
SAKryukov at 11-May-12 18:08pm
   
Thank you, Monjurul.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
losmac at 11-May-12 12:09pm
   
Good answer, my 5!
Monjurul Habib at 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)

  Print Answers RSS
0 Mika Wendelius 400
1 DamithSL 213
2 CPallini 159
3 OriginalGriff 145
4 Afzaal Ahmad Zeeshan 129
0 Mika Wendelius 400
1 OriginalGriff 205
2 DamithSL 203
3 CPallini 185
4 Afzaal Ahmad Zeeshan 129


Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 11 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100