Are there any best practices to call native C++ or Delphi Dlls from C# (ASP.NET)? Is invoking or a COM interop the best way to access those Dlls?
Asked
Active
Viewed 468 times
0
-
I suggest going for a wrapper in managed C++. what exactly do you wanna do? – Danahi Mar 14 '13 at 13:57
-
This explains your options: http://blogs.msdn.com/b/msdnts/archive/2010/05/03/call-native-c-dll-from-c-code.aspx – L-Four Mar 14 '13 at 13:58
-
2 minutes.. Just _too_ long. – Soner Gönül Mar 14 '13 at 13:58
-
You have 2 options: use COM interop or Platform Invoke. Both choices are valid. If you have a COM object - use COM interop, otherwise use P/Invoke. – oleksii Mar 14 '13 at 14:00
-
I don't have any COM objects so far ... just some simple license calculation functions, written in Delphi. – Konrad Mar 14 '13 at 14:02
-
If you only have a handful of functions, I'd say p/invoke – David Heffernan Mar 14 '13 at 14:05
1 Answers
1
You can use DllImport to achieve your objective, as per the following:
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
You can use this with any Win32 compatible DLL, including Delphi ones.

Martin
- 16,093
- 1
- 29
- 48
-
Does this also works on 64bit machines calling 32bit DLLs? Or do I have to provide the 64bit DLLs of the native code for those cases? – Konrad Mar 14 '13 at 14:00
-
2@Konrad If you build your C# code as x86 then you provide 32-bit DLL. If you build as "AnyCPU" you need to provide 32-bit DLL on x86 OS, and 64-bit DLL on x64 OS. – Alvin Wong Mar 14 '13 at 14:02