简体   繁体   中英

Returning a byte array from activex to javascript

I've built a small activex control using c#. I've got a function that returns an array of bytes. From some reason when I try to consue the returned array in js I receive an undefined value. Why is this happenning? Is there anyway to solve it?

Here's a simple demonstration of my code:

Activex:

[ComVisible(true)]
        public byte[] Close()
        {
            try
            {
                MessageBox.Show("called from activex Close");
                return Stop();
            }
            catch (Exception e)
            {
                //ExceptionHandling.AppException(e);
                throw e;
            }
        }

Javascript Call:

function CloseActiveX(){
var myRslt = document.OurActiveX.Close();
}

You haven't shown what the Stop() method contains. If Stop() returns null, you should expect to see what you're seeing.

As it stands, however, it looks like your ActiveX control is written in .NET. This is a bad idea for myriad reasons, not the least of which that performance will be low, and you will encounter problems if there are other controls or extensions running in the browser that want a different version of the framework.

Beyond that problem, the likely issue is that the byte[] isn't being marshalled back to the caller in a way that allows its use. You need to return a VARIANT with the following properties: ARRAY, BYREF, U1.

Default Marshaling for Arrays may be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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