C#调用C++ ---参数传递

C++中函数定义:

extern "C" __declspec(dllexport) void CharArray(char* cArray)
{
    printf("%s", cArray);
}

extern "C" __declspec(dllexport) void IntRef(int & iRef)
{
    iRef = 999;
}

extern "C" __declspec(dllexport) void FloatArray(float* fArray,int size)
{
    for (int i=0;i<size;++i)
    {
        fArray[i] = 6.6f;
    }
}

C#中调用:

    class Program
    {
      
        [DllImport("foo.dll",EntryPoint = "CharArray", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern void CharArray([MarshalAs(UnmanagedType.LPStr)]string cArray);

        [DllImport("foo.dll", EntryPoint = "IntRef", CallingConvention = CallingConvention.Cdecl)]
        public static extern void IntRef(ref int iRef);

        [DllImport("foo.dll", EntryPoint = "FloatArray", CallingConvention = CallingConvention.Cdecl)]
        public static extern void FloatArray(float[] fArray, int size);

        static void Main()
        {
            int counter = 0;
            float[] farr =new float[6];
            string path = "c:\\foo\\bar";
            CharArray(path);
            FloatArray(farr, 6);
            IntRef(ref counter);
            Console.WriteLine(counter);
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(farr[i]);
            }
        }
    }

 

posted @ 2017-06-14 21:08  fndefbwefsowpvqfx  阅读(2874)  评论(0编辑  收藏  举报