使用node-ffi处理char *

时间:2017-11-07 22:34:35

标签: c++ node.js ffi node-ffi

我使用node-ffi加载库并调用一个返回char *的函数,指针在共享库中创建。

我是否需要创建可以删除缓冲区的匹配函数? e.g。

的main.cpp

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])

{

    //imagepicker.allowsEditing = false
    if let pickedImage = info[UIImagePickerControllerOriginalImage] {

        print("image is:\(pickedImage)")



         imageView.contentMode = .scaleAspectFit
        imageView.image = pickedImage as? UIImage

        hideLabel.isHidden = true

        if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
            let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL as URL], options: nil)


            let fileNameObj = result.lastObject?.value(forKey: "filename") as? String ?? "Unknown"
            UserDefaults.standard.set(fileNameObj, forKey: "key4")


            self.fileNameLabel.text = fileNameObj
            picker.dismiss(animated: true, completion: nil)


            print("filename is:\(fileNameObj)")

        }
    }


  //  let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    if let videoUrl =  info[UIImagePickerControllerMediaURL] as? URL {


        strUrl = videoUrl.absoluteString

        last4 = strUrl.substring(from:strUrl.index(strUrl.endIndex, offsetBy: -10))


        self.fileNameLabel.text = last4

        picker.dismiss(animated: true, completion: nil)
        print("videourl is:\(String(describing: strUrl))")
        print("last4 is:\(String(describing: last4))")

    }




}




func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
    //var body = Data();
    var body = Data()

    if parameters != nil {
        for (key, value) in parameters! {
            body.appendString(string: "--\(boundary)\r\n")
            body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendString(string: "\(value)\r\n")
        }
    }


    let imgObj = fileNameLabel.text
    let videoObj = fileNameLabel.text

    let newImg = UserDefaults.standard.string(forKey: "key4")


    if imgObj == newImg{


        //let newObj = "manu.jpg"
        //let manuObj = fileNameLabel.text
        body.appendString(string: "--\(boundary)\r\n")

        let mimetype = "image/jpg"

        //let defFileName = fileNameObj



        let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
        print("defFileName is :\(String(describing: imageData))")

        let fnamed = fileNameLabel.text
        print("fnamed is:\(fnamed!)")
        body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(fnamed!)\"\r\n")

        print("njsc is:\( body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(fnamed!)\"\r\n"))")

        body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
        body.append(imageData!)
        body.appendString(string: "\r\n")
        body.appendString(string: "--\(boundary)--\r\n")


    }
    else {
        let kBoundary = "Boundary-\(UUID().uuidString)"
        let kStartTag = "--%@\r\n"
        let kEndTag = "\r\n"
        let kContent = "Content-Disposition: form-data; name=\"%@\"\r\n\r\n"



        body.append(String(format: kStartTag, boundary).data(using: String.Encoding.utf8)!)
        body.append(String(format: "Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", "file", last4!).data(using: String.Encoding.utf8)!)
        body.append("Content-Type: video/mp4\r\n\r\n".data(using: String.Encoding.utf8)!)
        body.appendString(string: strUrl)
        body.append(String(format: kEndTag).data(using: String.Encoding.utf8)!)

        // close form
        body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
    }
    return body as NSData
}

test.js

char* get_error()
{
    char* buf = new char[25];
    buf[24] = '\0';
    return buf;
}

void delete_buffer( char* buf )
{
    delete[] buf;
}

当我尝试这个时,我遇到了一个段错误。 node-ffi会自动创建副本吗?我该怎么做呢?

0 个答案:

没有答案