Calling C DLL method from C# Code

FacultiesIndia used Ask the Experts™
I wanted to call the following two C DLL methods from C# code.
  BOOL FAR PASCAL GetData (char * pszName, int *pnAge, char *pszAddress, char *pcType,
          long *plDOB, float *pfSal, double *pdSal, UINT *puPhone, byte *byData, BOOL *pbVal);

  void FAR PASCAL SetData (char * pszName, int nAge,  char *pszAddress, char cType,
        long lDOB, float fSal, double dSal, UINT uPhone, byte cData, BOOL bVal);

I know that to call unmanaged DLL methods from managed code, we need to defined DLLImport attributes.

My question is how to define it for the above two methods

If someone can give the actual code snippet that defines the DLLImport attributes for the above two sample methods will be very much appreciated.

Thanks in advance for your help.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
masheikSoftware Engineer

The following link gives simple methods to use dll's created in C and use them in C#
take a look at it,may be helpful



mohan sekar...thanks for the link...i had already seen this is not demonstrating how to supply float and double...i'm not able to get it work for double & you know how to write DLLImport for methods with float and double as parameters.

masheik...this link that you had given demonstrates with a method that has no parameters...but I wanted something that involves data types that I had mentioned above.

Here's some code for that, a very good book about that kind of question is .NET and COM from Adam Nathan

// c_dll.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include <stdio.h>
#include "windows.h"
__declspec(dllexport) BOOL MyGetData(char* pszName, int *pnAge, char *pszAddress,
						  long *pIDOB, float *pfSal, double *pdSal,
						  UINT * puPhone, char *byte, BOOL pbVal) {
	  printf("pszName = %s \n"
		     "pnAge = %d\n"
			 "pszAddress = %x\n"
			 "long pIdoB = %d", pszName, *pnAge, pszAddress,
		   *pIDOB = 100;
	  return TRUE;
This is the C side I just ignore a few parameter....
This is the C# side
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EE_Examples
    class Program
        static extern bool MyGetData(String pszName, out long pnAge,
                                 Byte address, out long pIODB,
                                 out float pfSal, out double pdSal,
                                 out uint puPhone,
                                 Byte anotherAddress, bool flag);
        static void Main(string[] args)
            int i = 10;
            string pszName = "some name";
            long pnAge = 0;
            Byte foo = 0;
            float floatVal;
            double dVal;
            Byte dummy = 0;
            long pIODB;
            uint puPhone = 0;
            bool b_rval;
            b_rval = MyGetData(pszName, out pnAge, foo, out pIODB,
                               out floatVal, out dVal, out puPhone, dummy, true);
            Console.WriteLine("pioDB is now {0}", pIODB);

Open in new window


 Friedrich...Thanks for your code snippet. It was very helpful.

I also got to know about "Microsoft Interop Assistant" today. This tool automatically converts the C source code into an equivalent C# DLLImport attribute assigned code snippet. This makes the whole process look very manageable.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial