基于GDAL提取地物,并生成png,最后加载到网页上(二)

C# 调用 C++ DLL  的struct 传递

C++  .h

#pragma once
typedef struct
{
	int nAge;
	bool bUse;
	double nDistance;
	char name[200];
}LIDBATATYPE;

typedef struct
{
	int progress;
	char msg[100];
}OUTMSG;

extern "C" _declspec(dllexport) int _stdcall PassStruct(LIDBATATYPE *mdata); //指针
extern "C" _declspec(dllexport) int _stdcall sendMsg(OUTMSG &msg);           //引用

C++ cpp

int _stdcall PassStruct(LIDBATATYPE *mdata)
{
	
	int age = mdata->nAge;
	bool use = mdata->bUse;
	double distance = mdata->nDistance;
	std::string n = mdata->name;
	mdata->nAge = 101;
	int a = age + 1;
	return a;
	
}
int _stdcall sendMsg(OUTMSG &msg)
{
	int pro = msg.progress;
	msg.progress = 123;
	//msg.msg = "cppmsg";
	std::string str = "cppmsg";
	//strcpy(msg.msg, str.c_str());
	//str.copy(msg.msg, 7, 0);
	int i;
	for ( i = 0; i < str.length(); i++)
	{
		msg.msg[i] = str[i];
	}
	msg.msg[i] = '\0';
	//printf("%s\n", p);
	return msg.progress;

}

  

[StructLayout(LayoutKind.Sequential,CharSet =CharSet.Ansi)]
public  struct OUTMSG
{
    public int progress;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
    public string msg;
}
public struct toCppData
{
    public int nAge;
    public bool bUse;
    public double nDistance;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
    public string name;
}

       [DllImport(@"../../../Debug/CreateDLL.dll", EntryPoint = "test01", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        extern static int test01(int a, int b, int c);

        [DllImport(@"../../../Debug/CreateDLL.dll", EntryPoint = "test02", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        extern static int test02(int a, int b);
      
        [DllImport(@"../../../Debug/CreateDLL.dll", EntryPoint = "ReadRaster", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        extern static int ReadRaster();
       
        [DllImport(@"../../../Debug/CreateDLL.dll", EntryPoint = "PassStruct", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        extern static int PassStruct(ref toCppData _data);

        [DllImport(@"../../../Debug/CreateDLL.dll", EntryPoint = "sendMsg", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        extern static int sendMsg(ref OUTMSG msg);

        static void Main(string[] args)
        {
            //int r1 = test01(1, 2, 3);
            //int r2 = test02(5, 2);
            // int r3 = ReadRaster();
            // Console.WriteLine("test01结果:" + r3.ToString());
            //Console.WriteLine("test02结果:" + r2.ToString());

            toCppData data = new toCppData();
            data.nAge = 100;
            data.bUse = false;
            data.nDistance = 3.1415926;
            data.name = "abcd";
            int v = PassStruct(ref data);

            OUTMSG mMsg = new OUTMSG();
            mMsg.progress = 999;
            int ret = sendMsg(ref mMsg);
            int progress = mMsg.progress;
            Console.WriteLine("结果:" +mMsg.msg.ToString());


            Console.ReadKey();
        }

  

 

转载于:https://www.cnblogs.com/marky/p/11417129.html

你可能感兴趣的:(基于GDAL提取地物,并生成png,最后加载到网页上(二))