473,414 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

DLL Import method

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.

Mar 8 '07 #1
4 5930
Hi,

"Zytan" <zy**********@yahoo.comwrote in message
news:11**********************@64g2000cwx.googlegro ups.com...
>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.
--
Ignacio Machin
machin AT laceupsolutions com
Mar 8 '07 #2
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.
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

Mar 8 '07 #3
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/en...ClassTopic.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

Mar 9 '07 #4
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/...ndMessage.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

Mar 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Petri Savolainen | last post by:
I was trying to roll my own python code importer, but in the end, it seems that no matter what you try, it is always necessary to supply a REAL file object for python lower-level import machinery...
7
by: Matthew Wilson | last post by:
Hi- I'm writing a bunch of classes, several of which need functions and variables defined in the math module. In some instances, I'm going to import my module like this: import myshapes ...
5
by: Dfenestr8 | last post by:
Hi. I have a program which I want a plugin directory for. I figured the way to go about that would be to just add a plugin/ dir to sys.path, and import everything in it. Then my program can just...
4
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
7
by: steven | last post by:
Which one is better: <link rel="stylesheet" href="style.css" type="text/css" /> or <style type="text/css"> @import url("style.css"); </style>
9
by: Arancaytar | last post by:
I have so far seen two methods for including external resources as CSS stylesheets in a document. The first is this: <link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> And...
1
by: madbat87 | last post by:
Hi, I'm trying to import code from a c++ unmanaged dll in a c# project. In the dll there's a class named Connection, but the dll exposes only 2 methods (outside the class): - HBITMAP...
3
by: derfer | last post by:
I am trying to import a .inf file into access (the file comes from the output of some 3rd party software). I have managed to convert the file by opeing it in notepad and re-saving then a seperate...
2
by: WP | last post by:
Hello, I've just started to work with DB2 (using the express edition version 9.5 under windows vista) and I'm accessing it through a java program (my java's very rusty). The java program loads the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.