简体   繁体   English

如何通过 char** 类型的方法形参返回 base64 解码的图像字节

[英]How to return base64 decoded image bytes through a methods formal parameter of type char**

Being new to C++, I am still struggling with pointers-to-pointers and I am not sure if my method below is returning decoded image bytes properly.作为 C++ 的新手,我仍然在为指针到指针而苦苦挣扎,我不确定我下面的方法是否正确返回解码的图像字节。

This method gets a base64 encoded image string from API.此方法从 API 获取 base64 编码的图像字符串。 The method has to follow this signature as it is part of legacy code that is not allowed to abbreviate from the way it was written originally.该方法必须遵循此签名,因为它是遗留代码的一部分,不允许从最初编写的方式中缩写。 So the signature has to stay the same.所以签名必须保持不变。 Also, I have omitted here async calls and continuations, exceptions etc for code simplicity.此外,为了代码简单,我在这里省略了异步调用和延续、异常等。

int __declspec(dllexport) GetInfoAndPicture(CString uid, char **image, long *imageSize)
{
    CString request = "";
    request.Format(url); 

    http_client httpClient(url);
    http_request msg(methods::POST);

    ...

    http_response httpResponse;
    httpResponse = httpClient.request(msg).get();  //blocking
    web::json::value jsonValue = httpResponse.extract_json().get();

    if (jsonValue.has_string_field(L"img"))
    {
        web::json::value base64EncodedImageValue = jsonValue.at(L"img");
        utility::string_t imageString = base64EncodedImageValue.as_string();  
        std::vector<unsigned char> imageBytes = utility::conversions::from_base64(imageString);
        image = (char**)&imageBytes;  //Is this the way to pass image bytes back? 
    *imageSize = imageBytes.size();
    }

    ...
}

The caller calls this method like so:调用者像这样调用这个方法:

char mUid[64];
char* mImage;
long mImageSize;
...
resultCode = GetInfoAndPicture(mUid, &mImage, &mImageSize);

//process image given its data and its size

I know what pointer to pointer is, my question is specific to this line我知道什么是指向指针的指针,我的问题是针对这一行的

image = (char**)&imageBytes;

Is this the correct way to return the image decoded from base64 into the calling code via the char** image formal parameter given the above method signature and method call?给定上述方法签名和方法调用,这是通过char** image参将从base64解码的图像返回到调用代码中的正确方法吗?

I do get error "Program .... File: minkernel\\crts\\ucrt\\src\\appcrt\\convert\\isctype.cpp ... "Expression c >= -1 && c <= 255"" which I believe is related to the fact that this line is not correctly passing data back.我确实收到错误“Program .... File: minkernel\\crts\\ucrt\\src\\appcrt\\convert\\isctype.cpp ...“Expression c >= -1 && c <= 255”” ,我认为这与这一行没有正确地将数据传回这一事实。

Give the requirements there isn't any way to avoid allocating more memory and copying the bytes.根据要求,没有任何方法可以避免分配更多内存和复制字节。 You cannot use the vector directly because that is local to the GetInfoAndPicture function and will be destroyed when that function exits.您不能直接使用向量,因为它是GetInfoAndPicture函数的本地函数,并且会在该函数退出时被销毁。

If I understand the API correctly then this is what you need to do如果我正确理解 API,那么这就是您需要做的

//*image = new char[imageBytes.size()];  //use this if caller calls delete[] to deallocate memory
*image = (char*)malloc(imageBytes.size());  //use this if caller calls free(image) to deallocate memory
std::copy(imageBytes.begin(), imageBytes.end(), *image);
*imageSize = imageBytes.size();

Maybe there is some way in your utility::conversions functions of decoding directly to a character array instead of to a vector, but only you would know about that.也许在您的utility::conversions函数中有某种方法可以直接解码为字符数组而不是向量,但只有您知道这一点。

The problem is with allocating (and freeing) memory for that image;问题在于为该图像分配(和释放)内存; who is responsible for that?谁负责?

You can't (shouldn't) allocate memory in one module and free it in another.您不能(不应该)在一个模块中分配内存并在另一个模块中释放它。

Your two options are:您的两个选择是:

  1. Allocate large enough buffer on the caller side, and have DLL use it utility::conversions::from_base64() .在调用方分配足够大的缓冲区,并让 DLL 使用它utility::conversions::from_base64() The issue here is: what is large enough?这里的问题是:什么足够大? Some Win APIs provide an additional method to query the required size.一些 Win API 提供了一种额外的方法来查询所需的大小。 Doesn't fit this scenario as the DLL would either have to get that image for the second time, or hold it (indefinitely) until you ask for it.不适合这种情况,因为 DLL 要么必须第二次获取该图像,要么(无限期地)保留它,直到您要求它为止。
  2. Allocate required buffer in the DLL and return a pointer to it.在 DLL 中分配所需的缓冲区并返回指向它的指针。 You need to ensure that it won't be freed until the caller request to free it (in a separate API).您需要确保在调用者请求释放它之前不会释放它(在单独的 API 中)。

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

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