Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

noobie: calling external DLL

2 views
Skip to first unread message

Paul Dolen

unread,
Dec 14, 2009, 11:55:25 AM12/14/09
to
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!

Jeff Johnson

unread,
Dec 14, 2009, 12:40:33 PM12/14/09
to
"Paul Dolen" <nos...@nowhere.com> wrote in message
news:c3rci51utn3bqvhgo...@4ax.com...

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


Patrice

unread,
Dec 14, 2009, 1:06:22 PM12/14/09
to
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...

--
Patrice


Paul Dolen

unread,
Dec 14, 2009, 2:51:09 PM12/14/09
to
>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

Paul Dolen

unread,
Dec 14, 2009, 2:55:51 PM12/14/09
to

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

Arne Vajhøj

unread,
Dec 14, 2009, 9:40:41 PM12/14/09
to
On 14-12-2009 11:55, Paul Dolen wrote:
> 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

Arne Vajhøj

unread,
Dec 14, 2009, 9:43:32 PM12/14/09
to
On 14-12-2009 12:40, Jeff Johnson wrote:
> "Paul Dolen"<nos...@nowhere.com> wrote in message
>> 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.

As in http://stackoverflow.com/badges/146/legendary ?

Arne

Family Tree Mike

unread,
Dec 14, 2009, 10:30:13 PM12/14/09
to
On 12/14/2009 9:40 PM, Arne Vajh�j wrote:
> On 14-12-2009 11:55, Paul Dolen wrote:
<snip>

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

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.

--
Mike

Arne Vajhøj

unread,
Dec 14, 2009, 10:50:57 PM12/14/09
to
On 14-12-2009 22:30, Family Tree Mike wrote:
> On 12/14/2009 9:40 PM, Arne Vajh�j wrote:
>> On 14-12-2009 11:55, Paul Dolen wrote:
> <snip>
>>> 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.
>>
>
> 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

Paul Dolen

unread,
Dec 15, 2009, 10:45:49 AM12/15/09
to
>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!

0 new messages