Read JPG as binary in MFC app and send to JavaScript function in HTML page.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Read JPG as binary in MFC app and send to JavaScript function in HTML page.

  1. #1
    Join Date
    Aug 2012
    Posts
    53

    Read JPG as binary in MFC app and send to JavaScript function in HTML page.

    Hey there

    I am writing an MFC app in Visual Studio 2012 that will open a JPG file as binary and read all the contents to a CString.

    I am able to read it to a std::Vector, but that doesn't help me much as i need to pass all the binary content as a MFC CString to another function.

    Can anyone help me with a sample code for that?

    Thanks in advance

    More Update:

    Let me explain the problem a little more deeper.

    I am trying to call a JavaScript (JS) function in a HTML page and then want to pass the binary date. The C++ function that calls the JS is given below.

    bool CallJavaScript(const CString strFunc,CComVariant* pVarResult);

    The 1st argument is the JS Function Name and 2nd is the one to pass the Binary Data. 1st argument works fine as i am able to call the JS fucntion called "LoadImage" without any problem. Problem is with 2nd argument that's supposed to take the Binary data of the JPG file.

    If i try to pass a std::Vector or std::string then it will give me an error.

    But it's happy if i pass CString. But then with CString there's a problem with NULL characters.

    Actually my plan is to pass the binary of a JPG to a JS function and let it display the JPG in the HTML page.

    Is there a better way of doing this?

    Can i typecast a Vector or std::string to a CComVariant*?

    Please help. Thanks

  2. #2
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Read JPG as binary in MFC app and send to JavaScript function in HTML page.

    The first question to you is why are you using high-level C++ classes, whether it is CString, std::string, or std::vector to a JavaScript function?

    Usually, you pass lower-level (i.e. pointers) to the data to such as functions. If the C++ user-code wants to store the data in a CString, vector, etc. that's its business. But eventually the user-code that calls JavaScript should have some way to take a pointer to that data, regardless of how it's stored, and pass to the JavaScript function.

    Second, those classes, CString, vector, std::string, are all implementation-specific to the compiler that is being used. In other words, a CString, vector, etc. in Visual Studio 2008 is different internally than one in VS 2010, VS 2012, etc. So the code you now have works for only one version of the compiler, and even a particular service pack. These C++ classes are only supposed to be used within a C++ module, and rarely, if ever, be used as parameters to non-C++ functions or to functions defined in other external modules.

    I would expect the JavaScript function to look like this:
    Code:
    bool CallJavaScript(char* imageData, LONG imageLength, CComVariant* pVarResilt).
    In other words, buffer where the data is stored is passed. You can wrap this buffer in a std::vector or CString (if it accepts NULL characters), a new[] block, etc. As long as whatever you store the data in is in contiguous memory in some way, all you need is to pass a pointer to the data. For example, for std::vector
    Code:
    std::vector<char> iV;
    //...
    bool CallJavaScript(&iV[0], iv.size(), LONG imageLength, CComVariant* pVarResilt).
    Using a safe, new[] block
    Code:
    struct newDeleter
    {
        char *newedptr;
        char newDeleter(char *p) : newedptr(p) {}
        ~newDeleter() { delete [] p; )
    };
    
    char *pData = new imageData[dataSize];
    newDeleter del(pData);
    bool CallJavaScript(pData, dataSize, LONG imageLength, CComVariant* pVarResilt).
    //...
    The newDeleter will destroy the memory safely without a memory leak occuring. It may look like overkill, but it guarantees that the memory is deallocated, regardless of how complex the function will become.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 6th, 2013 at 01:04 PM.

  3. #3
    Join Date
    Aug 2012
    Posts
    53

    Re: Read JPG as binary in MFC app and send to JavaScript function in HTML page.

    I think the MFC part of sending the binary data to JavaScript seems to be clear now.

    How about decoding that image Buffer to an actual image in JavaScript?

    Any ideas?

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,958

    Re: Read JPG as binary in MFC app and send to JavaScript function in HTML page.

    Quote Originally Posted by Don Guy View Post
    I am writing an MFC app in Visual Studio 2012 that will open a JPG file as binary and read all the contents to a CString.
    Quote Originally Posted by Don Guy View Post
    How about decoding that image Buffer to an actual image in JavaScript?
    It looks like you're unprepared to what you're trying to accomplish. There's no such a thing like "actual image in JavaScript." Image element belongs with HTML page. To change the displayed image you just change its src to point to the resource you want. Say it's a local file C:\Temp\1.jpg. Then you have to set src value to file:///C:/Temp/1.jpg, and that's it, HTML engine will render it without your "help". No buffers, no decoding, just proper HTML programming. And JavaScript here is just a dynamic glue between C++ and HTML parts.

    File URI scheme

    To program CDHtmlDialog you have to be skilled not only in C++ but JavaScript and HTML as well. Bad understanding of the very basic principles inevitably leads to a crippled architecture and design.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center