noobie: calling external DLL

P

Paul Dolen

I've been mainly using Delphi for years, and just starting on a
personal project in C#. I'm going to need to access an external DLL,
so, I searched the net for examples, and found this MSDN article:

http://msdn.microsoft.com/en-us/library/e59b22c5.aspx


Here, the MSDN sample imports user32.dll:

[DllImport("User32.dll", CharSet=CharSet.Unicode)]
public static extern int MessageBox(int h, string m, string c, int
type);

When I try to compile, I get an error that indicates I don't have the
required reference. USER32.DLL isn't in my list of possible
references. I find out that I can add to my path, so I add
windows\system32 to my references
path, and user32.dll still doesn't show up in the possible references.
So, how do I get the DLL as an available reference?

I am using C# 2008 Express Edition. I figure that since I'm working
from an MSDN example, it should work and I'm just missing one minor
thing or not understanding something. Any ideas?

I haven't been in these MS newsgroups in quite some time. Activity
level is lower than I would have expected, just a few posts per day
here. So, am I posting in the right spot? If someone knows a better
newsgroup for my question, let me know.

Thanks!
 
J

Jeff Johnson

Paul Dolen said:
I've been mainly using Delphi for years, and just starting on a
personal project in C#. I'm going to need to access an external DLL,
so, I searched the net for examples, and found this MSDN article:

http://msdn.microsoft.com/en-us/library/e59b22c5.aspx


Here, the MSDN sample imports user32.dll:

[DllImport("User32.dll", CharSet=CharSet.Unicode)]
public static extern int MessageBox(int h, string m, string c, int
type);

When I try to compile, I get an error that indicates I don't have the
required reference. USER32.DLL isn't in my list of possible
references. I find out that I can add to my path, so I add
windows\system32 to my references
path, and user32.dll still doesn't show up in the possible references.
So, how do I get the DLL as an available reference?

You can't because you're dealing with apples and oranges. The DllImport
attribute exists specifically to allow you to call functions from DLLs that
you CAN'T set references to. User32.dll is once such DLL because it's not a
managed DLL. I've got the feeling your compiler error is actually referring
to something else, or is it specifically targeting the DllImport line?
I haven't been in these MS newsgroups in quite some time. Activity
level is lower than I would have expected, just a few posts per day here.

That's because the damned kids these days want their glitzy Web forums with
badges and awards and voting and all that crap. They need to get the hell
off my lawn.
So, am I posting in the right spot? If someone knows a better
newsgroup for my question, let me know.

You posted to the perfect place. Hopefully you'll get more help than I was
able to provide, though.
 
P

Patrice

Add the :
using System.Runtime.InteropServices;

It shouldn't be commented. This namespace provide the DllImport attribute so
if this using clause is missong the DllImport attribute is not known...
 
P

Paul Dolen

That's because the damned kids these days want their glitzy Web forums with
badges and awards and voting and all that crap. They need to get the hell
off my lawn.

LOL
 
P

Paul Dolen

Add the :
using System.Runtime.InteropServices;

It shouldn't be commented. This namespace provide the DllImport attribute so
if this using clause is missong the DllImport attribute is not known...

Thanks! My mind just ignored that line since I saw it was commented.
I submitted feedback on the article saying that's a bug.
 
A

Arne Vajhøj

I've been mainly using Delphi for years, and just starting on a
personal project in C#. I'm going to need to access an external DLL,
so, I searched the net for examples, and found this MSDN article:

http://msdn.microsoft.com/en-us/library/e59b22c5.aspx

Here, the MSDN sample imports user32.dll:

[DllImport("User32.dll", CharSet=CharSet.Unicode)]
public static extern int MessageBox(int h, string m, string c, int
type);

When I try to compile, I get an error that indicates I don't have the
required reference. USER32.DLL isn't in my list of possible
references. I find out that I can add to my path, so I add
windows\system32 to my references
path, and user32.dll still doesn't show up in the possible references.
So, how do I get the DLL as an available reference?

I am using C# 2008 Express Edition. I figure that since I'm working
from an MSDN example, it should work and I'm just missing one minor
thing or not understanding something. Any ideas?

As other have explained then you need to import the namespace of
DllImport.

I would just like to emphasize the fact that .NET DLL's and
Win32 DLL's are very different.

You reference .NET assemblies (DLL's) and import their namespace.

You use DllImport on individual functions in Win32 DLL's.
I haven't been in these MS newsgroups in quite some time. Activity
level is lower than I would have expected, just a few posts per day
here.

What?

mpdlc seems to have around 50 posts per day at average during
the last few months.
So, am I posting in the right spot? If someone knows a better
newsgroup for my question, let me know.

This group is for C#. Your question is around C# language or
usage .NET framework in C#. All fine.

Arne
 
F

Family Tree Mike

On 14-12-2009 11:55, Paul Dolen wrote:

What?

mpdlc seems to have around 50 posts per day at average during
the last few months.

If you view the newsgroups from Microsoft.com/Communities/newsgroups,
you would get the impression of a lower number of posts. Their site
seems to frequently be down, with posts going AWOL.
 
A

Arne Vajhøj

If you view the newsgroups from Microsoft.com/Communities/newsgroups,
you would get the impression of a lower number of posts. Their site
going AWOL.

50 -> few

indicates a lot of downtime then ....

Arne
 
P

Paul Dolen

I would just like to emphasize the fact that .NET DLL's and
Win32 DLL's are very different.

I'm pretty ignorant of .NET, but, I did gather that much LOL.
mpdlc seems to have around 50 posts per day at average during
the last few months.

My newsreader was seeing more like a dozen, at least for the last few
days, I didn't look father back than that.
This group is for C#. Your question is around C# language or
usage .NET framework in C#. All fine.

Great, thanks!
 

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