Sunday, June 13, 2010

Make Turbo C DLL to use in C# windows application




Please visit my new Web Site WWW.Codedisplay.com



In some cases of our professional life we need to parse or decode or handle large number of file. As you knew that for IO purpose turbo C is faster than other technologies like dot net or something else. In my cases i need to develop an ASN decoder where i need to decode more than million of files. In such cases i need to develop a ASN decoder. In my cases i have done the following way:

1. Create a Turbo C program which will decode ASN files.
2. Convert the Turbo C program to a DLL to use in C# windows application.
3. After that upload data into the SQL Server/Oracle database.

Here in this article i will explain only how one can create a DLL file created in Turbo C and then use this DLL file in C# windows application. Steps were given below:



1. Create a Turbo C program Like below:
#include<stdio.h>
#include<conio.h>

int ADDs(int num1,int num2)
{
 return num1+num2;
}

int Subtract(int num1,int num2)
{
  return num1-num2;
}

void main()
{
 //printf("%d",ADDs(10,10));
}

2. Now open Visual Studio 2005.
3. Go to File--> New --> Project.
4. Now configure Language= VC++ and from templates select Win32 Project like below:

VC++ Project

5. Now type the name of the DLL and then click on OK.
6. Now click on Next.
7. Select Application Type=DLL like below:

VC++ Application Type

8. Now click Finish.
9. Now write the two function ADDs and Subtract in the following way:

VC++ Application Type

10. Now your VC++ code window will like below:
#include "stdafx.h"


#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    return TRUE;
}


int ADDs(int num1,int num2)
{
 return num1+num2;
}

int Subtract(int num1,int num2)
{
  return num1-num2;
}


#ifdef _MANAGED
#pragma managed(pop)
#endif
11. Now we need to create a definition file.
12. Now go to File-->New-->File. You will get below window:

DLL Definition File

13. Save the file as Export.def in the project root folder.
14. Now write the below content in Export.Def file:
LIBRARY "mymath"
EXPORTS
  ADDs
  Subtract

Or

Right cilck on mymath solution. Click Add-->New Item like below:

Def File

15. Now go to the Project--> Project Properties and define the Export.def file in the following way:

Define definition file

16. Now build your project.
17. Go to the debug folder and copy the mymath.dll file into your system32 folder.
18. Now create an C# windows application. Within the form Add two textboxes and one command button.
19. Now under button click event write below sample code:
private void button1_Click(object sender, EventArgs e)
        {
            int a=Adds(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
            MessageBox.Show(a.ToString());
        }
20. Don't forget to import DLL as well as including using System.Runtime.InteropServices; So full code will be:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("mymath.dll")]
        public static extern int Adds(int Num1, int Num2);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a=Adds(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
            MessageBox.Show(a.ToString());
        }
    }
}

21. Now run the project. Hope you will get your desired output.

1 comments:

nr m said...

What if I have to include a header file too.. On building the project that is on step 16 , it gives error stating:

visual studio 2010\projects\mymath\mymath\mymath.cpp(5): fatal error C1083: Cannot open include file: 'division.h': No such file or directory

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash