Pass char ** via c # link to unmanaged C ++

This is C # code.

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

An unmanaged method is this.

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

The method writes to the passed array of strings. Is there a way to call this method from C # and have unmanaged code to write to an array of strings?

+5
source share
1 answer

This worked thanks to Remus Rusan that the link had the answer. Everything that I had was decorated [In, Out] by lst parameter.

 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); } } 
+1
source

Source: https://habr.com/ru/post/1233545/


All Articles