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.
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.