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:
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:
8. Now click Finish.
9. Now write the two function ADDs and Subtract in the following way:
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) #endif11. Now we need to create a definition file.
12. Now go to File-->New-->File. You will get below window:
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:
15. Now go to the Project--> Project Properties and define the Export.def file in the following way:
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:
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
I WOULD BE DELIGHTED TO HEAR FROM YOU