1

I wrote a small program in Turbo C and I would like to get or create a DLL of this program for using it with my C# application.

So how can I create a DLL of a C program using Turbo C?
I would like to use it with a C# or VB program in a DLL reference.

If found this link, but I couldn't understand it.

zx485
  • 28,498
  • 28
  • 50
  • 59
pvaju896
  • 1,397
  • 6
  • 25
  • 46

2 Answers2

3

Turbo C (last release in 1989) is a DOS based program. It cannot create Win32 DLLs.

Since you are already using Visual Studio for C#, I would strongly suggest using Visual C++ for your DLL. Visual C++ is self explanatory (hint: Win32 DLL is the project type you want).

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
3

Do not use Turbo C and compile with Visual C++ since we have to use Win32 calling conventions. Suppose math.h is your library.

#include <math.h>

extern "C"
{
    __declspec(dllexport) double __stdcall MyPow(double, double);
}

extern double __stdcall MyPow(double x, double y)
{
    return pow(x, y);
}

And then import it in your C# application, using DllImport.

class Program
{
    [DllImport("MyLibrary.dll")]
    extern static double MyPow(double x, double y);

    static void Main(string[] args)
    {
        Console.WriteLine(MyPow(2.0, 5.0));

        Console.ReadKey();
    }
}

This makes your code exteremly unmanaged. A Better approach would be creating a Managed C++ wrapper. To do so, create a new Visual C++ Dynamic Library project, enable Common Language RunTime Support (OldSyntax) under Project Properties > Configuration Properties > C/C++ > General and disable C++ Exceptions in Project Properties > Configuration Properties > C/C++ > Code Generation. Build for release target.

extern "C"
{
    #include <math.h>
}

namespace Wrapper
{
    public __gc class MyClass
    {
        public:
            static double MyPow(double x, double y)
            {
                return pow(x, y);
            }
    };
};

Then create a new Visual C# project, reference the .DLL file we just made and in Project Properties > Build, check Allow unsafe code if you're using pointers in your original library and need to modify them in your C# application.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Wrapper.MyClass.MyPow(2.0, 5.0));

        Console.ReadKey();
    }
}
Hamid Nazari
  • 3,905
  • 2
  • 28
  • 31
  • sorry bro.. getting run-time error,,!! Unable to load DLL 'MyLibrary.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) i Tried the first one - build succeceded for release target in dll and my application couldn't LOAD the DLL..!!?? HElp PLZ..!! – pvaju896 Jul 28 '10 at 15:25
  • Sorry bro..!! i tried both the win32 project and c# application ran for release target..!! it worked thanx..!! – pvaju896 Jul 28 '10 at 15:28
  • >Yes thanX bro..!! Both works well.. >and what i was lookin for was second type. >And secound type is really good. – pvaju896 Jul 28 '10 at 17:32