C#调用外部C++生成DLL

【生成dll C++文件部分内容】

[.h文件] //外部调用函数声明
#pragma once
#ifndef KINECTDATAGENERATOR_H
#define KINECTDATAGENERATOR_H

// initialize Kinect
extern "C" _declspec ( dllexport ) void KinectInitial();
//Get Skeleton Joint Position
extern "C" _declspec ( dllexport ) void GetSkeletonJointPosition(float XNAPointX[],float XNAPointY[],float XNAPointZ[]);
//Get Depth & Color image data
extern "C" _declspec ( dllexport ) void GetDepthData(unsigned char data[]);
extern "C" _declspec ( dllexport ) void GetRGBaCameraData(unsigned char data[]);

#endif

[.cpp文件] //函数实现
void KinectInitial()
{
 HANDLE hThreadSource=NULL;
 //DWORD WINAPI KinectDataGenerator(LPVOID lpParameter)
 hThreadSource=CreateThread(NULL,0,KinectDataGenerator,NULL,0,NULL);
 CloseHandle(hThreadSource); 
}

void GetSkeleJointPostion(float JointPositionX[], float JointPositionY[], float JointPositionZ[])
{
 for(int i = 0; i < 15; i++)
 {
  JointPositionX[i] = bodyJointPosition[i].position.X;
  JointPositionY[i] = bodyJointPosition[i].position.Y;
  JointPositionZ[i] = bodyJointPosition[i].position.Z;
 }
}

void GetRGBaCameraData(uchar Data[])
{
 memcpy(Data, imageRGBaData, 640 * 480 * 4 * sizeof(uchar));
}

void GetDepthData(uchar Data[])
{
 memcpy(Data, DepthimageData, 640 * 480 * sizeof(uchar));
}


【C#中对dll文件的调用】
//Read DLL and Fetch Data From it
 public class KinectDataGeneratorDLL
    {


        //Read DLL and Fetch Data From it
        const string DataGeneratorDllName = "KinectDataGeneratorDLL.dll";

        [DllImport(DataGeneratorDllName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern void KinectInitial();

        [DllImport(DataGeneratorDllName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetDepthData(byte[] data);

        [DllImport(DataGeneratorDllName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetRGBaCameraData(byte[] data);
       
        [DllImport(DataGeneratorDllName, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern void GetSkeletonJointPosition(ref float[] X, ref float[] Y, ref float[] Z);

        //Construct Function
        public KinectDataGeneratorDLL()
        {
       
        }

    }

        KinectDataGeneratorDLL DataGeneratorDLL;
        DataGeneratorDLL = new KinectDataGeneratorDLL();
然后就是函数调用部分了


 

你可能感兴趣的:(C#调用外部C++生成DLL)