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]);
            }
        }
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM