简体   繁体   中英

C++ DLL call from C#

I want to use a C++ DLL from C#. The C++ DLL is win32 console application. I have successfully called it and want to process the data that I have from c++ in c#. The C# application exits, however, after executing the DLL ie this line: GetArrayFromDLL();

I am new to C# and visual C++. Can someone provide some suggestions?

Thanks

namespace ConsoleApplication1
{
    class Program
    {

        [DllImport("Lidar_DataCal_CDLL.dll")]

        public static extern void GetArrayFromDLL();



        static void Main(string[] args)
        {
            Console.WriteLine("This is C# program");
            GetArrayFromDLL();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }
    }
}

This is the DLL I written in win32 application.

extern "C"
{
  __declspec(dllexport) void GetArrayFromDLL()
  {

/// Reading hex from a file 

  FILE * pFile;

  FILE * f;

  signed int array_data[81],array_data1[81],i;

  double fangle,fsin,fcos,fangle_rad;

  double afx[81],afy[81];

  int px,py;

  char file_read[20] = "scandataset1.txt";

  char file_write[20] = "xycordinates1.txt";


  pFile = fopen ( file_read,"a+");

  if (pFile != NULL)

      {
  for (i=0;i<80;i++)

      {
       char buffer[3];

       fscanf (pFile, "%x",&array_data[i]);

      sprintf ( buffer ,"%d", array_data[i]);

      printf ( "I have read: %s \n\n", buffer);

      array_data1[i] = atoi(buffer);

       // finding angle 

       fangle = 20 -( (i+1-1)*0.5);

       fangle_rad = (PI*fangle/180);

       fsin = -sin(fangle_rad);

       fcos =  cos(fangle_rad);

       afx[i] = array_data[i] * fsin;

       afy[i] = array_data[i] * fcos;

      // printf ("X: %lf  and Y: %lf  \n\n",afx[i],afy[i]);

       f = fopen(file_write,"a+");

       if(f != NULL);

       fprintf(f,"%lf %lf\n",afx[i],afy[i]);

       fclose (f);

      } 
      }
  else
      {

      printf("Error opening fail");

      }

  fclose (pFile);
      }

    }

the main issues i had to face while using native c++ in c# is marshaling issues. check to make sure you do not have any of those. and since you have told that the dll executes fine then the only problem that could arise is from the functions return type. C code might not be the culprit, it must be the dll.

Also try specifying calling convention and entry point while importing a dll

link to issues of dll import

Link to Unmanaged Code Inter OP

Basically there are only two straightforward approaches to calling of C++ methods. Either create C-callable wrappers (functions) around your object-oriented interface, which makes it language independent but not always exactly beautiful or maintainable, or you can use an object oriented glue language between C# and C++.

In .NET 1.1, the glue language was Managed C++ (it is gone these days).

Its successor language that is probably the best choice for you, is C++/CLI. This language is best used just for a relatively thin layer between your managed and unmanaged worlds which it is able to unify quite nicely.

(Practically speaking: In case you are using VS2010 and targetting .NET older than 4 any reason, adding C++/CLI code will force you to install also VS 2008 on your system, but you can still fully use the VS2010 IDE for all three languages.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM