所以我正在研究这个名为 cROS 的 c 库,但是我想从 C# 检索一个 byte[],以便我可以使用该库共享它,但我在将 byte[] 传递给 char* 时遇到了麻烦.
C#代码
[DllImport("aavatar")] private static extern void unity_reg_get_camera_screenshot_callback(unity_get_camera_screenshot_callback callback_fn);
private static byte get_camera_screenshot(int resWidth, int resHeight)
{
...
byte[] bytes = screenShot.EncodeToPNG();
return bytes;
}
void Start(){
...
unity_reg_get_camera_screenshot_callback(new unity_get_camera_screenshot_callback(get_camera_screenshot));
}
C代码
void unity_reg_get_obj_coordinate_callback(unity_get_obj_coordinate_callback callback_fn)
{
Unity_get_obj_coordinate_fn= callback_fn;
}
CallbackResponse callback_srv_get_cam_screenshot(cRosMessage *request, cRosMessage *response, void* data_context)
{
...
char *ret_cam_screenshot;
ret_cam_screenshot = Unity_get_cam_screenshot_fn((int) scr_width32,(int)scr_height32);
}
最佳答案
如果已声明返回类型为 byte,则无法返回 byte[] 数组。我不知道该函数的签名应该是什么(即“unity_get_camera_screenshot_callback”的定义是什么?),但也许您可以将其更改为
private static byte[] get_camera_screenshot(int resWidth, int resHeight)
{
...
byte[] bytes = screenShot.EncodeToPNG();
return bytes;
}
关于c# - 将 C# byte[] 传递给 C char 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086247/