[Q] Examples / Reference on how to use DLLs with C#

S

Stuart Norris

Dear Readers,

Are they any good references on calling / using windows DLL from C#
sharp.

I need to use some functions in a DLL file for a touch screen.

I have some code that calls the DLL in C++ so I was wanting to convert
it to C# so I can call the DLL natively. Or can I call the C++ from
C#?

Any reference on doing this?

BTW is there any way to list all the functions / entry points that
exist in the DLL?

Thanks

Stuie
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hello Stuart,
Are they any good references on calling / using windows DLL from C#
sharp.

Refer to the Platform Invoke (P/Invoke) documentation and the
DllImportAttribute class documentation in MSDN. I guess it's somewhere in
the "Interoperating with Unmanaged Code" section.
BTW is there any way to list all the functions / entry points that
exist in the DLL?

Good question. I have seen a tool listing entry points (called something
like Dependency Walker), but this won't help you if you don't know the
requred arguments and their types for each of the entry points.
A general answer will therefore be - don't rely on the fact that this is
possible, as unless you have the source code for the DLL, this is at least
very difficult to achieve.

If someone knows a better answer to this, please correct me.
 
M

Mike Kitchen

I have examples on my web site that use the winmm dll and call into into. So
if you were using the playsound method. you would need to include the
foloowing code:

using System.Runtime.InteropServices;

[DllImport("winmm.dll")]
private static extern bool PlaySound( string lpszName, int hModule, int
dwFlags )

Then you would call it like the following:
 
M

Mike Kitchen

I have examples on my web site that use the winmm dll and call into into. So
if you were using the playsound method. you would need to include the
foloowing code:

using System.Runtime.InteropServices;

[DllImport("winmm.dll")]
private static extern bool PlaySound( string lpszName, int hModule, int
dwFlags )

Then you would call it like the following:

PlaySound( "C:\WINNT\Media\tada.wav", 0, 1 );

Examples at http://www.publicjoe.f9.co.uk/csharp/csharp17.html
and http://www.publicjoe.f9.co.uk/csharp/csharp18.html

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
 

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