Using c code with c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aliche
    New Member
    • Sep 2008
    • 1

    Using c code with c#

    Hello

    I would like to know how to import a c or c++ code using c# without using a dll.
    I m using visual studio 2005

    Thank you
  • NIKHILUNNIKRISHNAN
    New Member
    • Jun 2008
    • 22

    #2
    Hai,

    Why do you want to import it directly without using a dll ? How will the system understand the location of your code ? If its a dll,it will be having all the necessary informations required. else how will you ensure that its complete by its own ?

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Here's a tutorial that describes the process:

      Comment

      • NIKHILUNNIKRISHNAN
        New Member
        • Jun 2008
        • 22

        #4
        I'm going to create a C# application that uses C++/CLI to call into native C++ code without using C#'s unsafe language extensions. Let's take a look at the simple C# application in Listing 1, which consists mostly of the code generated for me by the new C# Console Application project wizard:

        Listing 1: Program.cs
        using System;
        using System.Collecti ons.Generic;
        using System.Text;
        using MyInteropCode;

        namespace ConsoleApplicat ion1
        {
        class Program
        {
        static void Main(string[] args)
        {
        String s = CPPClass.CallCP PFcn();
        Console.WriteLi ne(s);
        }
        }
        }

        Two things worth noting here: this module is referencing a namespace called MyInteropCode, which I'll talk about in a moment, and it's calling a C++ function called CPPClass.CallCP PFcn(). Listing 2 contains the C++/CLI interop code that is being referenced by Program.cs. Listing 2: clrcode.cpp
        #include "nativecode .h"

        using namespace System;

        namespace MyInteropCode
        {

        ref class CPPClass
        {
        public:
        static String^ CallCPPFcn()
        {
        wchar_t c[32];
        GetStringFromNa tive(c, sizeof(c) / sizeof(c[0]));
        return gcnew String(c);
        }
        };

        }


        clrcode.cpp defines the MyInteropCode namespace, which contains a managed class called CPPClass. It is in this class (or some collection of similar classes) that I would place my interop functions, which leverage IJW interop technology to offer CLS-compliant interfaces and call native C++ within their implementation. In this case, I'm surfacing one static function called CallCPPFcn(), that returns a System::String. Within CallCPPFcn(), I create a local character array, pass that array to a native C++ function to be populated, and then construct a new System::String from that that array, which is returned to the C# caller in Program.cs. Listing 3 shows nativecode.cpp, which as you might guess contains the GetStringFromNa tive() function called from clrcode.cpp.

        Listing 3: nativecode.cpp
        #include "string.h"
        #include "nativecode .h"

        void GetStringFromNa tive(wchar_t* c, int num)
        {
        wchar_t* s = L"Hello from native C++!";
        wcsncpy_s(c, num, s, wcslen(s));
        }

        GetStringFromNa tive()'s job is to populate the array passed in argument c with a locally defined string. The two C++ files share a common header, shown in Listing 4, which simply contains the prototype for GetStringFromNa tive().

        Listing 3: nativecode.h
        void GetStringFromNa tive(wchar_t* c, int num);


        Hope this is what you want.

        with regards from,
        Nikhil Unnikrishnan.

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          Way to rip off the code from the link I posted... at least post credits to the original author if you're going to do that...
          Last edited by Curtis Rutland; Sep 16 '08, 03:56 PM. Reason: removed unnecessary quote

          Comment

          Working...