473,624 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("file name.dll", CharSet = CharSet.Ansi, SetLastError =
true, ExactSpelling = true)]
private static extern bool MethodName(
[MarshalAs(Unman agedType.VBByRe fStr)] ref string astring,
[MarshalAs(Unman agedType.VBByRe fStr)] 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(Unman agedType.VBByRe fStr)] 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 "Informatio n Not Found". Thank god for google.

Mar 8 '07 #1
4 5953
Hi,

"Zytan" <zy**********@y ahoo.comwrote in message
news:11******** **************@ 64g2000cwx.goog legroups.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(Unman agedType.VBByRe fStr)] 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(Unman agedType.VBByRe fStr)] 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("file name.dll", CharSet = CharSet.Ansi, SetLastError =
true, ExactSpelling = true)]
private static extern bool MethodName(
[MarshalAs(Unman agedType.VBByRe fStr)] ref string astring,
[MarshalAs(Unman agedType.VBByRe fStr)] ref string anotherstring);

So, what does [MarshalAs(Unman agedType.VBByRe fStr)] 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(Unman agedType.VBByRe fStr)] 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
2267
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 to work. Is this really true? Or did I miss something? Of course, it is easy to get the module source from somewhere, put it into a tmpfile etc. ... but I'd be nice to be able to skip that extra step. Thanks,
7
2235
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 and then in others, I'll do:
5
1631
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 execute the main() method of each imported plugin. Is that a good way to go about it? If so, how do I import everything in the plugins dir? The method raises an
4
2186
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
2707
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 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
7
11807
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
6897
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 the second is this: <style type="text/css"> @import "/stylesheets/style.css"; </style>
1
3443
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 DownloadIntra(char *host, int port, char *user, char *pwd, int channel); - Connection* CreateConnection(char *host, int port, char *user, char *pwd);
3
2202
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 script to open the txt file and import. Both these scripts seem to work well (ish) but is there a better way of doing this? and is there a way of doing this and only having to run a single script? Copies of the script below. Any help would be much...
2
7103
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 driver, connects and tries to create two tables. That works just fine. Then it tries to import data into one of the tables but it seems I can't use an import command with Statement.execute() because an exception is thrown if I attempt to do this....
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8330
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6107
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.