DllImport and Data Marshalling

R

Rookie

Hi,

I had a question on DllImport. On importing a function from a VC++ dll
using DllImport (to a C# program), the function argument data types
and the return types may be of a type that is not supported by C#. In
this case if I am not mistaken the system performs default marshalling
- matching the data types to its most similar equivalent in C#. Is
this right?

Also, I presume this can be overridden by using MarshalAs. Is this
right as well?

However my main question is if I do not want to override the default
marshalling, how do I figure out what is the default marshalled (C#)
type for a given VC++ type?

Hope to hear from someone on this. Thank you.
 
S

Shakir Hussain

Marshaling allocates unmanaged memory, copies unmanaged memory blocks and
converts managed to unmanaged types.

System.Runtime.InteropServices.UnmanagedType to get the equivalent types in
c#.

There are lot of converstion routines in Marshal class as static functions

for example

Marshal.StringToBSTR - Converts .NET string to COM string.
 
B

BMermuys

Rookie said:
Hi,

I had a question on DllImport. On importing a function from a VC++ dll
using DllImport (to a C# program), the function argument data types
and the return types may be of a type that is not supported by C#. In
this case if I am not mistaken the system performs default marshalling
- matching the data types to its most similar equivalent in C#. Is
this right?

Also, I presume this can be overridden by using MarshalAs. Is this
right as well?

However my main question is if I do not want to override the default
marshalling, how do I figure out what is the default marshalled (C#)
type for a given VC++ type?

Not sure if you see it right. You always have to specify the parameters
(managed ones) yourself when declaring a function prototype for import.

Some managed types can be marshalled as more then one unmanaged type,
therefore one of the unmanaged types is the default and MarshalAs lets you
override it.

There is a table available suggesting what managed type to use for a given
unmanaged type:
http://msdn.microsoft.com/library/d...cpguide/html/cpconplatforminvokedatatypes.asp

HTH,
greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top