views:

76

answers:

2

Hello,

I need to InterOp Win32 code (unmanaged Win32 DLL's and Exe) completely with .NET. I need to call Win32 unmanaged code(DLL exported functions) at runtime i.e (knowing the types of data types in Win32 signatures and need to pass data according to that type at runtime).

This is 100% possible in case of COM. You can convert COM unmanaged code to managed assemblies using tlbimp.exe and use now reflection API to work with those managed types(actual were unmanaged types now converted managed using tlbimp).

But same functionality I need to get in terms of Win32(i.e) in .NET framework. How?? I know MS provided Export table reading API ..but I couldn't find exact API for InterOp of Win32 unmanaged code

Regards

+1  A: 

Yes, take a look at http://www.pinvoke.net/

Using the services of System.Runtime.InteropServices you can interop with native Win32 code or any DLL that exports C type functions.

Chris Taylor
System.Runtime.InteropServices enables you to get types from TypeLibs or others to extract out (e.g u can extract whole signatures defined in binary) but you need then REFLECTIONS to know the types at runtime and call those methods at runtime as well.In case of Win32 we can extract types and signatures by reading Export table and headers of PE(Plain Win32 DLL) now here comes calling those methods at runtime same we did in case of COM using reflection. But reflection applies only managed assemblies. To me plain win32 DLL's can't be converted to managed assemblies.
Usman
No, you can't automagically generate the p/invoke declarations because native code does not have sufficient metadata. The link to pinvoke.net provides a huge about of ready made interop declarations for many of the Win32 APIs. You can follow the same pattern for non-Win32 API but you will need to do it manually.
Chris Taylor
To me PInvoke is just a call forwarder to unmanaged world and it does nothing else. No type checking at runtime it does.Is'nt?
Usman
@Usman, on the .NET side the types are very important. The interop services use the type information and various attributes to determine how to correctly marshat the data (arguments and return values) from the managed world to the unmanaged and back.
Chris Taylor
+1  A: 

Yes.

See PInvoke on MSDN.

brickner