How to call a dll function in C#?

I am new to C#.   I am try to test out my dll using C# Window Form application. Is there any reference that someone has that show how to declare and use  the DLL functions  within C# application
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
what kind of dll are you talking about
a .net dll (which is actually an assembly) or a com dll
     Here you want to use Managed code from UnManaged code(in VC++ )

The Extract from MSDN might help you. There are Possibly 3 Ways

1)Directly Accessing a Managed API
If an unmanaged client is written in C++, it can be compiled with the Visual Studio .NET C++ compiler as a "mixed mode image." After this is done, the unmanaged client can directly access any managed API. However, some coding rules do apply to accessing managed objects from unmanaged code; check the C++ documentation for more details.
Direct access is the preferred option since it does not require any special consideration from managed API developers. They can design their managed API according to managed API design guidelines (DG) and be confident that the API will still be accessible to unmanaged callers.

2) Exposing a Managed API as a COM API
Every public managed class can be exposed to unmanaged clients through COM interop. This process is very easy to implement, because the COM interop layer takes care of all the COM plumbing. Thus, for example, every managed class appears to implement IUnknown, IDispatch, ISupportErrorInfo, and a few other standard COM interfaces.
Despite the fact that exposing managed APIs as COM APIs is easy, managed and COM object models are very different. Therefore, exposing managed API to COM should always be an explicit design decision. Some features available in the managed world have no equivalent in the COM world and will not be usable from COM clients. Because of this, there is often tension between managed API design guidelines (DG) and compatibility with COM.
If COM clients are important, write your managed API according to the managed API design guidelines, and then write a thin COM-friendly managed wrapper around your managed API that will be exposed to COM.

3)Exposing a Managed API as a Flat API
Sometimes unmanaged clients cannot use COM. For example, they might already be written to use flat APIs and cannot be changed or recompiled. C++ is the only high-level language that allows you to expose managed APIs as flat APIs. Doing this is not as straightforward as exposing a managed API as a COM API. It is a very advanced technique that requires advanced knowledge of C++ interop and the differences between the managed and unmanaged worlds.

Expose your managed API as a flat API only if absolutely necessary. If you have no choice, be sure to check the C++ documentation and be fully aware of all the limitations.

Reffer :

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hi xewoox,

Assuming you've created the DLL and want to access its methods:

1. Right click on your project you want to add the reference to.
2. Add Reference
3. Browse to your DLL -> Add it.
4. In the Using declarations add: Using YourDllNameHere;
5. To access it, just call the class it encapsulates. I.e. MyClass myClass = new MyClass();

xewooxAuthor Commented:
It is an unmanaged code dll.  It is originally compiled under Visual Studio 6 as an MFC Dll
Use DllImport. There is a sample attached. It's cryptic but there are lots of samples available on the internet. This one is an extract from a TestBench in c# that was written to prove a C++ unmanaged dll.
Note tha conversion between data types. The dll used LPSTR.
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Wrapper
    public class WrapperClass
	[DllImport("DevName", CallingConvention=CallingConvention.Cdecl )]
		public static extern int dllOpenDevice( 
		[MarshalAs(UnmanagedType.LPArray)] byte[] ProductRef,
		[MarshalAs(UnmanagedType.LPArray)] byte[] SWDesc,
		[MarshalAs(UnmanagedType.LPArray)] byte[] SensorDesc );
		[DllImport("DevName", CallingConvention=CallingConvention.Cdecl )]
		public static extern int dllCloseDevice();
    public static int OpenDevice( ref String ProductRef,
                                  ref String SWDesc,
                                  ref String SensorDesc )
        byte[] bProductRef = new byte[MAX_MSGLEN];
        Int32 iRet = dllOpenDevice( bProductRef, ..............
        if( iRet == 0 )
            ProductRef = System.Text.Encoding.ASCII.GetString( bProductRef ............... );

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.