将结构数组编组到C#中的指针(Marshalling an array of structs to a pointer in C#)
我试图调用一个看起来像这样的非托管函数(DATA是我的C#结构体):
[DllImport("data.dll")] internal static unsafe extern int MyExternalFunction(DATA* pData, uint numElements);
这是我从C#调用函数的方式:
DATA[] data = new DATA[64]; fixed (DATA* pData = data ) { MyExternalFunction(pData, 64); } [StructLayout(LayoutKind.Sequential)] internal struct DATA { internal uint a; internal uint b; internal uint c; internal POINT pos; } [StructLayout(LayoutKind.Sequential)] internal struct POINT { internal int x; internal int y; }
不幸的是我得到这个错误:“不能编组'参数#1':指针不能引用编组结构。”
如果它有什么区别,我的数据结构在它里面嵌套了结构。 我无法控制这种外部方法的设计。 调用这个函数并接收一个结构数组的正确方法是什么?
I am trying to call an unmanaged function that looks like this (DATA is my C# struct):
[DllImport("data.dll")] internal static unsafe extern int MyExternalFunction(DATA* pData, uint numElements);
This is how I'm calling the function from C#:
DATA[] data = new DATA[64]; fixed (DATA* pData = data ) { MyExternalFunction(pData, 64); } [StructLayout(LayoutKind.Sequential)] internal struct DATA { internal uint a; internal uint b; internal uint c; internal POINT pos; } [StructLayout(LayoutKind.Sequential)] internal struct POINT { internal int x; internal int y; }
Unfortunately I get this error: "Cannot marshal 'parameter #1': Pointers cannot reference marshaled structures."
If it makes any difference, my DATA struct has nested structs inside it. I have no control over how this external method is designed. What is the correct way to call this function and receive an array of structs?
原文:https://stackoverflow.com/questions/7363821
最满意答案
试试这个:
- (void)saveImage: (UIImage*)image { if (image != nil) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"test.png" ]; NSData* data = UIImagePNGRepresentation(image); [data writeToFile:path atomically:YES]; } } - (UIImage*)loadImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"test.png" ]; UIImage* image = [UIImage imageWithContentsOfFile:path]; [self sendAction:path]; return image; }
Try this one:
- (void)saveImage: (UIImage*)image { if (image != nil) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"test.png" ]; NSData* data = UIImagePNGRepresentation(image); [data writeToFile:path atomically:YES]; } } - (UIImage*)loadImage { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* path = [documentsDirectory stringByAppendingPathComponent: @"test.png" ]; UIImage* image = [UIImage imageWithContentsOfFile:path]; [self sendAction:path]; return image; }
相关问答
更多-
在ALAssetsLibrary上使用的正确方法是writeImageToSavedPhotosAlbum:metadata:completionBlock:或writeImageToSavedPhotosAlbum:orientation:completionBlock: - 这些将导致您以后可以使用assetForURL:resultBlock:failureBlock:的资产URL assetForURL:resultBlock:failureBlock:获取对图像的引用。 The proper me ...
-
我终于找到了答案。 显然,UIImage方法剥离元数据,所以使用UIImageWriteToSavedPhotosAlbum是不错的。 然而在ios4中,苹果公司投入了一个新框架来处理名为ALAssetsLibrary的照片库。 首先,您需要右键单击目标,并在构建部分中,将AlAsset框架添加到项目中,左侧的小图标。 然后添加#import "AssetsLibrary/AssetsLibrary.h"; 到你的课程的头文件。 最后你可以使用以下代码: UIImage *viewImage = YOUR ...
-
图像已保存吗?(Has image been saved?)[2023-05-09]
我发现了一个班轮 alert( app.activeDocument.saved); 但是,根据进一步调查,它没有考虑新创建的文件。 I found out a one liner alert( app.activeDocument.saved); However, under further investigation it doesn't take into account a newly created document. -
%~dp0将为您提供bat文件的路径。 如果你还需要整个路径,包括bat文件本身的名称,请使用%~0 。 %~dp0 will give you teh path to the bat file. If you also need the whole path including the name of the bat file itself use %~0.
-
试试这个: - (void)saveImage: (UIImage*)image { if (image != nil) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docu ...
-
至少在我的情况下,我只是想要截图图像并没有真正关心它保存到手机。 所以我修改了截图插件,允许将base64字符串返回给定的函数。 https://github.com/l0c0luke/phonegap-plugins/commit/cb4c09ae2442b6bb8053d3708152bc0ffc334990 In my case at least, i just wanted the screenshot image and didn't really care for it to be saved ...
-
重要的一点是,您无法使用资产网址中显示的图片名称进行任何操作。 您必须存储asset-url而不是从照片库中检索图像。 I figured it out if anyone else is interested in doing the same thing. Credits to the link I posted in question I just translated it to Swift. let assetLib = ALAssetsLibrary() //Save image ...
-
您无法使用普通文件I / O访问照片库。 除了隐私问题之外,基础数据不一定存储在设备上。 您必须使用Photos框架和PHAsset / PHImageManager及相关类。 这篇文章展示了几个标准用例。 You cannot access the Photos library using normal File I/O. Beside the privacy concerns, the underlying data is not necessarily stored on the device. Yo ...
-
试试这个: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filepath = [documentsDirectory stringByAppendingPathComponent: @"tempImg.jpg"]; UIImag ...
-
存储图像的好地方是文档目录。 您可以保存到它,然后从中检索图像。 我使用这些UIImage扩展来保存和检索图像: extension UIImage { func save(fileName: String, type: String) { let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String if typ ...