[英]How to read a C++ SAFEARRAY** that is a Result from a COM interop where the C# return value was byte[]?
我创建了一个 C# DLL,它使用 Zebra Crossing Nuget 包 (ZXing.Net.Bindings.CoreCompat.System.Drawing) 来创建 QR 图像。
C#方法的签名是:
public interface IWriter
{
byte[] CreateQrCode(string content, int width, int height, string imageFormat);
};
我已经成功地在 C# 控制台应用程序中使用它来创建各种二维码,其中我将byte[]
返回值作为 png 图像文件写入磁盘。
自然地,为了可以从 C++ 调用它,我选中了库的属性/构建屏幕上的“注册 COM 互操作”复选框,使用强文件名密钥(无密码)对程序集进行签名,并创建以下 C++ 应用程序作为演示如何使用它的概念证明:
#include <iostream>
#import "C:\Users\[PATH TO C# BUILD]\ImageGenerator\bin\Debug\ImageGenerator.tlb" raw_interfaces_only
using namespace ImageGenerator;
int main()
{
HRESULT hr = CoInitialize(NULL);
IWriterPtr pICalc(__uuidof(Writer));
BSTR content = SysAllocString(L"http://www.google.com/");
BSTR format = SysAllocString(L"png");
const LONG width = 100;
const LONG height = 100;
const LONG count = width * height;
SAFEARRAY** myArray = NULL;
pICalc->CreateQrCode(content, width, height, format, myArray);
}
如何读取myArray
的结果,将其作为文件保存到磁盘?
C# byte[]
数组的长度为count
。
C#库代码如下:
using System;
using ZXing;
using System.Drawing;
using ZXing.QrCode;
using ZXing.CoreCompat.System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using BarcodeReader = ZXing.CoreCompat.System.Drawing.BarcodeReader;
namespace ImageGenerator
{
public interface IWriter
{
byte[] CreateQrCode(string content, int width, int height, string imageFormat);
};
/// <summary>
/// An Image Writer class that creates QR code images in a variety of image formats.
/// </summary>
public class Writer : IWriter
{
public Writer()
{
}
/// <summary>
/// Creates a QR Code in a specified image format, of width and height, returning it as byte[].
/// </summary>
/// <param name="content">The content that is to be represented by the QR Code.</param>
/// <param name="width">The width of the image.</param>
/// <param name="height">The Height of the image.</param>
/// <param name="imageFormat">A text string representing the format of the image, options are png, bmp, emf, exif, gif, icon, jpeg, memorybmp, tiff, and wmf.</param>
/// <returns></returns>
public byte[] CreateQrCode(string content, int width, int height, string imageFormat)
{
ImageFormat format = ImageFormat.Png;
switch(imageFormat.ToLower())
{
case "png":
format = ImageFormat.Png;
break;
case "bmp":
format = ImageFormat.Bmp;
break;
case "emf":
format = ImageFormat.Emf;
break;
case "exif":
format = ImageFormat.Exif;
break;
case "gif":
format = ImageFormat.Gif;
break;
case "icon":
format = ImageFormat.Icon;
break;
case "jpeg":
format = ImageFormat.Jpeg;
break;
case "memorybmp":
format = ImageFormat.MemoryBmp;
break;
case "tiff":
format = ImageFormat.Tiff;
break;
case "wmf":
format = ImageFormat.Wmf;
break;
}
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Width = width,
Height = height,
}
};
var qrCodeImage = writer.Write(content); // BOOM!!
using (var stream = new MemoryStream())
{
qrCodeImage.Save(stream, format);
return stream.ToArray();
}
}
}
只需使用SafeArrayAccessData
来获取返回的数据的 prointer。 检查类型,应该是VT_BYTE
。
你得到大小(尺寸) SafeArrayGetDim
、 SafeArrayGetLBound
、 SafeArrayGetUBound
在我看来,这里有两个主要问题:
您不能在 COM 互操作方案中使用 ZXing.Net.Bindings.CoreCompat.System.Drawing,因为 .Net Standard 2.0 的 CoreCompat.System.Drawing V2 程序集未签名。 如果您检查调用 pICalc->CreateQrCode(...) 的 HRESULT 返回值,您将看到值 0x80131044。 如果您真的想使用 CoreCompat.System.Drawing,则必须使用自己的密钥对其进行签名,并针对您的 CoreCompat.System.Drawing 版本构建新版本的 ZXing.Net.Bindings.CoreCompat.System.Drawing。 或者,您可以针对 .Net Standard 1.3 定位您的程序集,因为旧版本 CoreCompat.System.Drawing V1 已签名。 但我没有尝试。 就我而言,我使用完整框架 4.6.1 和本机框架的位图功能直接针对 ZXing.Net 检查了您的代码。
我必须按以下方式修改您的代码:
... SAFEARRAY* myArray = NULL; pICalc->CreateQrCode(content, width, height, format, &myArray); }
这就是在这种情况下如何使用指向指针的指针的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.