一、C#调用C++ dll文件
1)非托管方式:
C++函数定义:
extern "C" __declspec(dllexport)void MyChangeDetection(const char * filepath1,const char *filepath2,int DImethod,int DIAnalysis,const char *outpath)
{
......
}
C#中,函数声明部分:
[DllImport("CDdll.dll",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)] //,CharSet=CharSet.Ansi
public static extern void MyChangeDetection(IntPtr filepath1, IntPtr filepath2, int DImethod, int DIAnalysis, IntPtr outpath);
函数使用部分:
MyChangeDetection(Intptr1, Intptr2, Dimethod1, DiAnalysismethod2, Intptr3);
注意C++与C#中类型转换:
public string pathname1, pathname2;
public string Dimethod, DiAnalysismethod;
public string outPath = Directory.GetCurrentDirectory()+"\\"+"CDmap.png";
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = ".";
file.Filter = "所有文件(*.*)|*.*";
file.ShowDialog();
if (file.FileName != string.Empty)
{
try
{
pathname1 = file.FileName; //点击获取图片路径
this.pictureBox1.Load(pathname1);
// pictureBox1.Image.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
IntPtr Intptr1 = Marshal.StringToHGlobalAnsi(pathname1); //string ->IntPtr
IntPtr Intptr2 = Marshal.StringToHGlobalAnsi(pathname2);
IntPtr Intptr3 = Marshal.StringToHGlobalAnsi(outPath);
MyChangeDetection(Intptr1, Intptr2, Dimethod1, DiAnalysismethod2, Intptr3);
2)托管方式:
https://www.cnblogs.com/GIScore/p/5872565.html
二、C#调用C++ .exe文件
(1)C++文件正常写,,需要传递参数部分:
#include"header.h"
int main(int argc, char * argv[])
{
const char* filepath1 = argv[1];
const char* filepath2 = argv[2];
int DImethod = atoi(argv[3]);
int DIAnalysismethod = atoi(argv[4]);
const char* filepath3 = argv[5];
Mat img1 = imread(filepath1,0);
Mat img2 = imread(filepath2, 0);
...
return 0;
}
在调试C++代码的过程中,,可以在 项目属性->配置属性->调试->命令参数
在命令参数里面输入 参数
(2) C#文件:
C#调用的地方:
public string exepath = "M:\\MyCD\\CDWindow\\x64\\Debug\\CDexe.exe";
string args = string.Format("{0} {1} {2} {3} {4} ", pathname1, pathname2, Dimethod1, DiAnalysismethod2, outPath);
//args = args.Replace("\\", "/");
// StartCallbackProcess(@"M:\CDWindow\x64\Debug\CDexe.exe", args);
//StartCallbackProcess(@"M:\MyCD\CDWindow\x64\Debug\CDexe.exe", args);
StartCallbackProcess(exepath, args);
StartCallbackProcess()函数定义如下:
private void StartCallbackProcess(string exe_file, string save_fp, string dat_fp, string out_fp)
{
throw new NotImplementedException();
}
// int returnValue;
private void StartCallbackProcess(string exefilename, string arg)
{
// string save_fp = saveText.Text;
try
{
using (Process myprocess = new Process())
{
Control.CheckForIllegalCrossThreadCalls = false;
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(exefilename, arg) { WindowStyle = ProcessWindowStyle.Hidden };
myprocess.StartInfo = myProcessStartInfo;
myprocess.StartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardInput = true;
myProcessStartInfo.RedirectStandardOutput = true;
myprocess.StartInfo.CreateNoWindow = true;
//myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myprocess.OutputDataReceived += new DataReceivedEventHandler(myprocess_OutputDataReceived); //接收cmd数据
myprocess.Start();
myprocess.StandardInput.WriteLine(textBox1.Text);
myprocess.BeginOutputReadLine();
while (!myprocess.HasExited)
{
myprocess.WaitForExit();
}
// returnValue = myprocess.ExitCode;
}
//if (returnValue == 1)
//{
// MessageBox.Show("变化检测已完成!");
//}
//else
//{
// MessageBox.Show("变化检测失败!");
//}
}
catch (Exception ex)
{
MessageBox.Show("启动应用程序出错!原因:" + ex.Message);
}
}
myprocess_OutputDataReceived()函数定义如下:
该函数实现将cmd数据实时地显示在textBox1上
private void myprocess_OutputDataReceived(object sender, DataReceivedEventArgs outLine)
{
// this.BeginInvoke(new Action(() => { textBox1.Text += "\r\n" + outLine.Data; }));
if (!String.IsNullOrEmpty(outLine.Data))
{
StringBuilder sb = new StringBuilder(this.textBox1.Text);
this.textBox1.Text = sb.AppendLine(outLine.Data).ToString();
this.textBox1.SelectionStart = this.textBox1.Text.Length;
this.textBox1.ScrollToCaret();
}
}