DLL Import method

Z

Zytan

I have VB code that shows how to import a DLL function. I compiled
it. I used .NET Reflector to see the C# code, and it shows this:

[DllImport("filename.dll", CharSet = CharSet.Ansi, SetLastError =
true, ExactSpelling = true)]
private static extern bool MethodName(
[MarshalAs(UnmanagedType.VBByRefStr)] ref string astring,
[MarshalAs(UnmanagedType.VBByRefStr)] ref string anotherstring);

I believe the function internally deals with ANSI strings (8-bit
strings, what some people call ASCII, even though ASCII is only 7-
bit). So, I get why CharSet = CharSet.Ansi. MSDN says CharSet.Ansi
does 'String marshaling', and my understanding is that this will
convert Unicode strings into ANSI for me. Great, because C# uses
Unicode.

I highly doubt this function calls the Win32 SetLastError function, so
SetLastError could be false. I wonder why it defaults to true. I
assume because there's no harm done.

I think ExactSpelling means that I have to name my function
MethodName, if the function inside of the DLL is also called
MethodName. Fine.

So, what does [MarshalAs(UnmanagedType.VBByRefStr)] mean? VBByRefStr
= VB string that is passed ByRef. Ok, that's the same as a C# string
which is passed by ref. So, why isn't "ref string s" enough? VB and
C# strings are managed, so why does it say it's an unmanaged type?
Why does it need MarshalAs if CharSet.Ansi will handle it for me?

Zytan

P.S. Does anybody other than me notice that pressing F1 on any of
these terms shows the help feature in C# is utterly useless? In every
case it returns "Information Not Found". Thank god for google.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Zytan said:
I have VB code that shows how to import a DLL function. I compiled
it. I used .NET Reflector to see the C# code, and it shows this:
So, what does [MarshalAs(UnmanagedType.VBByRefStr)] mean? VBByRefStr
= VB string that is passed ByRef. Ok, that's the same as a C# string
which is passed by ref. So, why isn't "ref string s" enough? VB and
C# strings are managed, so why does it say it's an unmanaged type?
Why does it need MarshalAs if CharSet.Ansi will handle it for me?

Go a search by P/Invoke , this stand for Platform/Invoke and is the way you
call unmanaged methods from .NET.

Most of the time you will Pinvoke part of the win32 API, take a look at
www.pinvoke.net for a list of signature of functions.
 
Z

Zytan

So, what does [MarshalAs(UnmanagedType.VBByRefStr)] mean? VBByRefStr
Go a search by P/Invoke , this stand for Platform/Invoke and is the way you
call unmanaged methods from .NET.

Most of the time you will Pinvoke part of the win32 API, take a look at www.pinvoke.net for a list of signature of functions.

Ok, I will do that. Thanks for the advice, Ignacio.

I think I realize my flawed way of thinking. I was wondering why I
need marshalling of this unmanaged type (since VB didn't seem to
care), but, the fact is the DLL contains a function that is unmanaged,
and this is the way that I tell C#, a 'managed language', that it will
be passing its managed data into an unmanaged function.

Zytan
 
Z

Zytan

I have VB code that shows how to import a DLL function. I compiled
it. I used .NET Reflector to see the C# code, and it shows this:

[DllImport("filename.dll", CharSet = CharSet.Ansi, SetLastError =
true, ExactSpelling = true)]
private static extern bool MethodName(
[MarshalAs(UnmanagedType.VBByRefStr)] ref string astring,
[MarshalAs(UnmanagedType.VBByRefStr)] ref string anotherstring);

So, what does [MarshalAs(UnmanagedType.VBByRefStr)] mean?
http://msdn.microsoft.com/library/e...imeInteropServicesUnmanagedTypeClassTopic.asp

So, why isn't "ref string s" enough?

I still don't know why. My code just does work with just "ref string
astring". I need [MarshalAs(UnmanagedType.VBByRefStr)] for it to
work. VB doesn't need anything like this in its DllImport. Does this
mean the DLL was created in VB? I thought C# did automatic
marhshalling.

My code works, I just want to know why I need to explicitly state the
type of marshalling required.

Zytan
 
Z

Zytan

Most of the time you will Pinvoke part of the win32 API, take a look at www.pinvoke.net for a list of signature of functions.

Be careful with this site. For example:

http://www.pinvoke.net/default.aspx/user32/SendMessage.html
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As
IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As
IntPtr) As IntPtr

wParam and lParam should both increase to 64-bits on 64-bit machines,
but only one of them is IntPtr (32/64-bit type), where as the other is
Integer (always 32-bit). This is wrong.

Zytan
 

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