Using a C++ library.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Jefferies

    Using a C++ library.

    Hello,

    How do I use a C++ library in a C# project? Can it be done under 2005? Do I
    need to anything special, I remember reading somewhere that they included
    this feature in the latest .NET.

    Cheers,
    Simon.


  • Carlos J. Quintero [.NET MVP]

    #2
    Re: Using a C++ library.

    AFAIK, it if is a managed C++ library, you can use it as a .NET reference
    and if it is a COM C++ library, you can use it as COM Reference. Also, you
    can use its functions using DllImport if they are exposed directly. The
    bottom line is that once compiled, the language used to create a library is
    not important.

    --

    Best regards,

    Carlos J. Quintero

    MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
    You can code, design and document much faster.
    Free resources for add-in developers:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


    "Simon Jefferies" <simon@cooltool sonline.co.uk> escribió en el mensaje
    news:eu4wB9tVFH A.612@TK2MSFTNG P12.phx.gbl...[color=blue]
    > Hello,
    >
    > How do I use a C++ library in a C# project? Can it be done under 2005? Do
    > I need to anything special, I remember reading somewhere that they
    > included this feature in the latest .NET.
    >
    > Cheers,
    > Simon.
    >[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Using a C++ library.


      "Simon Jefferies" <simon@cooltool sonline.co.uk> wrote in message
      news:eu4wB9tVFH A.612@TK2MSFTNG P12.phx.gbl...[color=blue]
      > Hello,
      >
      > How do I use a C++ library in a C# project? Can it be done under 2005? Do
      > I need to anything special, I remember reading somewhere that they
      > included this feature in the latest .NET.
      >
      > Cheers,
      > Simon.
      >[/color]

      Please be more explicit, what do you mean with library, if it's a dll then
      you can call exported C style functions, anything else can't be used
      directly.

      Willy.


      Comment

      • XP 64 Exchange screen problems

        #4
        Re: Using a C++ library.

        The issues of connecting .NET to a C++ library are many

        1) C++ by default uses name mangling for its entry points

        2) Do you have access to the C++ source?

        3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?

        The following .NET program is a sample caller of a C++ DLL.

        using System;
        using System.Runtime. InteropServices ;
        namespace TestingDataProj ect
        {
        public class TestDataPassing
        {
        [DllImport("Test DataPassingDLL. dll")]
        public static extern void TestDataPassing Entry(ref MyStruct TestData, int
        iDataLength);
        [StructLayout(La youtKind.Sequen tial)]
        public struct MyStruct
        {
        [MarshalAs(Unman agedType.ByValA rray,SizeConst= 64)]
        public byte [] abMyData;
        public int iMyLength;
        public MyStruct (int iDummy)
        {
        abMyData = new byte[64];
        iMyLength = 64;
        } // public MyStruct ()
        } // public struct MyStruct
        [STAThread]
        static void Main(string[] args)
        {
        MyStruct MyTestData = new MyStruct(1);

        TestDataPassing Entry(ref MyTestData, 64);
        Console.WriteLi ne(MyTestData.a bMyData[0]);
        Console.WriteLi ne("{0,8:X}",My TestData.iMyLen gth);
        } // public static void Main ()
        } // public class TestDataPassing
        } // namespace TestingDataProj ect

        The following is the C++ DLL

        #include <stdio.h>

        extern "C" __declspec(dlle xport) void TestDataPassing Entry

        (unsigned char * pabData,
        int iDataLength)

        {
        printf("Hello world\n");
        for (int iNdx = 0; iNdx < 10; iNdx++)
        *(pabData + iNdx) = 48 + iNdx;
        *((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
        } // extern "C" __declspec(dlle xport) void TestDataPassing Entry ()

        HIH,

        Pat

        "Willy Denoyette [MVP]" wrote:
        [color=blue]
        >
        > "Simon Jefferies" <simon@cooltool sonline.co.uk> wrote in message
        > news:eu4wB9tVFH A.612@TK2MSFTNG P12.phx.gbl...[color=green]
        > > Hello,
        > >
        > > How do I use a C++ library in a C# project? Can it be done under 2005? Do
        > > I need to anything special, I remember reading somewhere that they
        > > included this feature in the latest .NET.
        > >
        > > Cheers,
        > > Simon.
        > >[/color]
        >
        > Please be more explicit, what do you mean with library, if it's a dll then
        > you can call exported C style functions, anything else can't be used
        > directly.
        >
        > Willy.
        >
        >
        >[/color]

        Comment

        • Simon Jefferies

          #5
          Re: Using a C++ library.

          Its a standard C++ library (.lib). The only way I have seen that I can do it
          is to create a managed C++ project and link to the .lib this way. Is this
          the only way with .lib as it is?

          Cheers,
          Simon.

          "XP 64 Exchange screen problems"
          <XP64Exchangesc reenproblems@di scussions.micro soft.com> wrote in message
          news:D57463BA-25D4-43D1-9402-1B42501E77DC@mi crosoft.com...[color=blue]
          > The issues of connecting .NET to a C++ library are many
          >
          > 1) C++ by default uses name mangling for its entry points
          >
          > 2) Do you have access to the C++ source?
          >
          > 3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?
          >
          > The following .NET program is a sample caller of a C++ DLL.
          >
          > using System;
          > using System.Runtime. InteropServices ;
          > namespace TestingDataProj ect
          > {
          > public class TestDataPassing
          > {
          > [DllImport("Test DataPassingDLL. dll")]
          > public static extern void TestDataPassing Entry(ref MyStruct TestData, int
          > iDataLength);
          > [StructLayout(La youtKind.Sequen tial)]
          > public struct MyStruct
          > {
          > [MarshalAs(Unman agedType.ByValA rray,SizeConst= 64)]
          > public byte [] abMyData;
          > public int iMyLength;
          > public MyStruct (int iDummy)
          > {
          > abMyData = new byte[64];
          > iMyLength = 64;
          > } // public MyStruct ()
          > } // public struct MyStruct
          > [STAThread]
          > static void Main(string[] args)
          > {
          > MyStruct MyTestData = new MyStruct(1);
          >
          > TestDataPassing Entry(ref MyTestData, 64);
          > Console.WriteLi ne(MyTestData.a bMyData[0]);
          > Console.WriteLi ne("{0,8:X}",My TestData.iMyLen gth);
          > } // public static void Main ()
          > } // public class TestDataPassing
          > } // namespace TestingDataProj ect
          >
          > The following is the C++ DLL
          >
          > #include <stdio.h>
          >
          > extern "C" __declspec(dlle xport) void TestDataPassing Entry
          >
          > (unsigned char * pabData,
          > int iDataLength)
          >
          > {
          > printf("Hello world\n");
          > for (int iNdx = 0; iNdx < 10; iNdx++)
          > *(pabData + iNdx) = 48 + iNdx;
          > *((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
          > } // extern "C" __declspec(dlle xport) void TestDataPassing Entry ()
          >
          > HIH,
          >
          > Pat
          >
          > "Willy Denoyette [MVP]" wrote:
          >[color=green]
          >>
          >> "Simon Jefferies" <simon@cooltool sonline.co.uk> wrote in message
          >> news:eu4wB9tVFH A.612@TK2MSFTNG P12.phx.gbl...[color=darkred]
          >> > Hello,
          >> >
          >> > How do I use a C++ library in a C# project? Can it be done under 2005?
          >> > Do
          >> > I need to anything special, I remember reading somewhere that they
          >> > included this feature in the latest .NET.
          >> >
          >> > Cheers,
          >> > Simon.
          >> >[/color]
          >>
          >> Please be more explicit, what do you mean with library, if it's a dll
          >> then
          >> you can call exported C style functions, anything else can't be used
          >> directly.
          >>
          >> Willy.
          >>
          >>
          >>[/color][/color]


          Comment

          • XP 64 Exchange screen problems

            #6
            Re: Using a C++ library.

            Simon,

            This is a C++ DLL built with Visual Studio 6.0, so the C++ code is NOT managed

            HTH,

            Pat

            "Simon Jefferies" wrote:
            [color=blue]
            > Its a standard C++ library (.lib). The only way I have seen that I can do it
            > is to create a managed C++ project and link to the .lib this way. Is this
            > the only way with .lib as it is?
            >
            > Cheers,
            > Simon.
            >
            > "XP 64 Exchange screen problems"
            > <XP64Exchangesc reenproblems@di scussions.micro soft.com> wrote in message
            > news:D57463BA-25D4-43D1-9402-1B42501E77DC@mi crosoft.com...[color=green]
            > > The issues of connecting .NET to a C++ library are many
            > >
            > > 1) C++ by default uses name mangling for its entry points
            > >
            > > 2) Do you have access to the C++ source?
            > >
            > > 3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?
            > >
            > > The following .NET program is a sample caller of a C++ DLL.
            > >
            > > using System;
            > > using System.Runtime. InteropServices ;
            > > namespace TestingDataProj ect
            > > {
            > > public class TestDataPassing
            > > {
            > > [DllImport("Test DataPassingDLL. dll")]
            > > public static extern void TestDataPassing Entry(ref MyStruct TestData, int
            > > iDataLength);
            > > [StructLayout(La youtKind.Sequen tial)]
            > > public struct MyStruct
            > > {
            > > [MarshalAs(Unman agedType.ByValA rray,SizeConst= 64)]
            > > public byte [] abMyData;
            > > public int iMyLength;
            > > public MyStruct (int iDummy)
            > > {
            > > abMyData = new byte[64];
            > > iMyLength = 64;
            > > } // public MyStruct ()
            > > } // public struct MyStruct
            > > [STAThread]
            > > static void Main(string[] args)
            > > {
            > > MyStruct MyTestData = new MyStruct(1);
            > >
            > > TestDataPassing Entry(ref MyTestData, 64);
            > > Console.WriteLi ne(MyTestData.a bMyData[0]);
            > > Console.WriteLi ne("{0,8:X}",My TestData.iMyLen gth);
            > > } // public static void Main ()
            > > } // public class TestDataPassing
            > > } // namespace TestingDataProj ect
            > >
            > > The following is the C++ DLL
            > >
            > > #include <stdio.h>
            > >
            > > extern "C" __declspec(dlle xport) void TestDataPassing Entry
            > >
            > > (unsigned char * pabData,
            > > int iDataLength)
            > >
            > > {
            > > printf("Hello world\n");
            > > for (int iNdx = 0; iNdx < 10; iNdx++)
            > > *(pabData + iNdx) = 48 + iNdx;
            > > *((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
            > > } // extern "C" __declspec(dlle xport) void TestDataPassing Entry ()
            > >
            > > HIH,
            > >
            > > Pat
            > >
            > > "Willy Denoyette [MVP]" wrote:
            > >[color=darkred]
            > >>
            > >> "Simon Jefferies" <simon@cooltool sonline.co.uk> wrote in message
            > >> news:eu4wB9tVFH A.612@TK2MSFTNG P12.phx.gbl...
            > >> > Hello,
            > >> >
            > >> > How do I use a C++ library in a C# project? Can it be done under 2005?
            > >> > Do
            > >> > I need to anything special, I remember reading somewhere that they
            > >> > included this feature in the latest .NET.
            > >> >
            > >> > Cheers,
            > >> > Simon.
            > >> >
            > >>
            > >> Please be more explicit, what do you mean with library, if it's a dll
            > >> then
            > >> you can call exported C style functions, anything else can't be used
            > >> directly.
            > >>
            > >> Willy.
            > >>
            > >>
            > >>[/color][/color]
            >
            >
            >[/color]

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: Using a C++ library.


              "Simon Jefferies" <simon@cooltool sonline.co.uk> wrote in message
              news:uuwWY3vVFH A.1508@tk2msftn gp13.phx.gbl...[color=blue]
              > Its a standard C++ library (.lib). The only way I have seen that I can do
              > it is to create a managed C++ project and link to the .lib this way. Is
              > this the only way with .lib as it is?
              >
              > Cheers,
              > Simon.
              >[/color]

              Yes, you can only bind dynamically and call exported C style functions from
              C#.
              Static libraries can only be bound by using a linker.

              Willy.


              Comment

              • Willy Denoyette [MVP]

                #8
                Re: Using a C++ library.


                "XP 64 Exchange screen problems"
                <XP64Exchangesc reenproblems@di scussions.micro soft.com> wrote in message
                news:D57463BA-25D4-43D1-9402-1B42501E77DC@mi crosoft.com...[color=blue]
                > The issues of connecting .NET to a C++ library are many
                >
                > 1) C++ by default uses name mangling for its entry points
                >
                > 2) Do you have access to the C++ source?
                >
                > 3) Is the DLL a COM type object (COM, COM+, DCOM, etc.)?
                >[/color]

                None of these are real issue's as long as you have a DLL that exports C
                style functions or exposes a COM interface.

                Willy.



                Comment

                Working...