• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1097
  • Last Modified:

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
3 Solutions
Anurag ThakurCommented:
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 : http://msdn.microsoft.com/en-us/library/ms973872.aspx

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


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now