C#调用C++dll方法,char*类型之间的传递

时间:2022-08-31 09:15:08

char*类型之间的传递是关键,下面贴出来具体实现方法:

c++ dll中的函数导出如下:

extern "C" LIB_BASE_PROCESS_API bool _stdcall ExtractImgArea(const char* strSrcFilePath, const char* strOutShpFilePath, bool bReprojectToBL, bool bRemoveInnerBkv=true, double dfSimplifyValue=1); 


c#调用方法如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication1
{
    class Program
    {
        [DllImport(@"ImgBaseProcess.dll", EntryPoint = "ExtractImgArea",
            CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        extern unsafe static bool ExtractImgArea(StringBuilder src, StringBuilder shp, bool t, bool b, double d);


        static void Main(string[] args)
        {
            double s = 1.0;
            StringBuilder strSrc = new StringBuilder("D:\\src.img");
            StringBuilder strShp = new StringBuilder("D:\\src.shp");


            bool isok = ExtractImgArea(strSrc, strShp, true, true, s);
          
            Console.WriteLine(isok);
            Console.ReadKey();            
        }
        
    }
}

记录以便以后遇到类似问题,便于查询解决,也为广大爱好者提供参考。