C ++ DLL调用从C# [英] C++ DLL call from C#

查看:157
本文介绍了C ++ DLL调用从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#的C ++ DLL。 C ++ DLL是win32控制台应用程序。我已成功调用它,并希望处理我从c ++在c#中的数据。然而,C#应用程序退出后,执行DLL,即这行: GetArrayFromDLL();



到C#和可视化C ++。有人可以提供一些建议吗?



感谢

 命名空间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());

}
}
}

我写的win32应用程序的DLL。

  externC
{
__declspec(dllexport)void GetArrayFromDLL()
{

///从文件中读取十六进制文件

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 ++)

b $ b char buffer [3];

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

sprintf(缓冲区,%d,array_data [i]);

printf(我读过:%s \\\
\\\
,buffer);

array_data1 [i] = atoi(buffer);

//找到角度

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 \\\
\\\
,afx [i],afy [i]);

f = fopen(file_write,a +);

if(f!= NULL);

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

fclose(f);

}
}
else
{

printf(Error opening fail);

}

fclose(pFile);
}

}


解决方案<

在c#中使用原生c ++时,我不得不面对的主要问题是封送问题。检查,以确保你没有任何这些。并且既然你已经告诉dll执行罚款,那么可能出现的唯一的问题是从函数返回类型。 C代码可能不是罪魁祸首,它必须是dll。



还要尝试指定调用约定和入口点,同时导入dll



指向dll导入问题的链接



链接到非托管代码Inter OP


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 i.e 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

这篇关于C ++ DLL调用从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆