Click here to Skip to main content
12,245,023 members (46,284 online)
Rate this:
 
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 6:56am
Edited 11-May-12 7: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 11-May-12 12:09pm
   
Good answer, my 5!
SAKryukov 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.
SAKryukov 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!
SAKryukov 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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 11 May 2012
Copyright © CodeProject, 1999-2016
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