繁体   English   中英

通过c#引用char **到非托管C ++

[英]Pass char** by reference from c# to unmanaged C++

这是C#代码。

namespace CameraTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] lst = new string[10];
            for (int i = 0; i < 10; i++)
            {
                lst[i] = new string(' ', 33);
            }
            bool sync = true;
            bool ret = CameraCalls.CAM_EnumCameraEx(sync, lst, 10, 33);

        }
    }

    public static class CameraCalls
    {
        [DllImport("CamDriver64.dll")]
        public static extern bool CAM_EnumCameraEx(bool sync,
                                                   [MarshalAs(UnmanagedType.LPArray)]
                                                   string[] lst, 
                                                   long maxCam, 
                                                   long maxChar);
    }
}

非托管方法就是这样。

BOOL WINAPI CAM_EnumCameraEx(BOOL bSynchronized, char **ppCameraList, long lMaxCamera, long lMaxCharacter);

该方法写入传入的字符串数组。 有没有办法从c#调用此方法并让非托管代码能够写入字符串数组?

感谢Remus Rusanu,链接有了答案。 我所有的东西都是用lst参数的[In,Out]装饰的。

namespace CameraTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int maxCam = 10,
                 maxChar = 33;

            var lst = (new object[maxCam]).Select(o => new string(' ', maxChar)).ToArray();
            bool sync = true;
            bool ret = CameraCalls.CAM_EnumCameraEx(sync, lst, maxCam, maxChar);

        }
    }

    public static class CameraCalls
    {
        [DllImport("CamDriver64.dll")]
        public static extern bool CAM_EnumCameraEx(bool sync,
                                                   [In, Out]
                                                   string[] lst, 
                                                   long maxCam, 
                                                   long maxChar);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM