简体   繁体   English

vb.net byte []转换为C ++ char *

[英]vb.net byte[] to C++ char*

I am calling an unmanaged C++ dll that expects a char* as one of its parameters and I want to push a byte[] into it. 我正在调用一个非托管的C ++ dll,它将char *作为其参数之一,我想将byte []推入其中。 The project is written in VB.NET. 该项目是用VB.NET编写的。

What type of marshalling will work for this? 哪种编组将对此起作用?

If you need to pin a managed structure in order to pass it as a parameter you can use the following code. 如果需要固定托管结构以将其作为参数传递,则可以使用以下代码。

    // (c) 2007 Marc Clifton
    /// <summary>
    /// A helper class for pinning a managed structure so that it is suitable for
    /// unmanaged calls. A pinned object will not be collected and will not be moved
    /// by the GC until explicitly freed.
    /// </summary>

    internal class PinnedObject<T> : IDisposable where T : struct
    {
        protected T managedObject;
        protected GCHandle handle;
        protected IntPtr ptr;
        protected bool disposed;

        public T ManangedObject
        {
            get
            {
                return (T)handle.Target;
            }
            set
            {
                Marshal.StructureToPtr(value, ptr, false);
            }
        }

        public IntPtr Pointer
        {
            get { return ptr; }
        }

        public int Size
        {
            get { return Marshal.SizeOf(managedObject); }
        }

        public PinnedObject()
        {
            managedObject = new T();
            handle = GCHandle.Alloc(managedObject, GCHandleType.Pinned);
            ptr = handle.AddrOfPinnedObject();
        }

        ~PinnedObject()
        {
            Dispose();
        }

        public void Dispose()
        {
            if (!disposed)
            {
                if (handle.IsAllocated)
                    handle.Free();
                ptr = IntPtr.Zero;
                disposed = true;
            }
        }
    }
}

You can then call the unmanaged code using PinnedObject.Pointer. 然后,您可以使用PinnedObject.Pointer调用非托管代码。 In your extern declaration, use IntPtr as the Type for that parameter. 在您的外部声明中,将IntPtr用作该参数的类型。

PinnedObject<BatteryQueryInformation> pinBatteryQueryInfo = new PinnedObject<BatteryQueryInformation>();
pinBatteryQueryInfo.ManangedObject = _structBatteryQueryInfo;
Unmanaged.Method(pinBatteryQueryInfo.Pointer);

In your PInvoke definition just declare the char* parameter as a byte[] and the standard marshaller will handle work. 在您的PInvoke定义中,只需将char *参数声明为byte [],标准编组将处理工作。

But this may or may not be the best idea. 但这可能不是最好的主意。 Is the C++ function expecting a string or is it expecting a buffer of data (C/C++ code often uses char* for a buffer, relying on the fact that a char is one byte)? C ++函数是期望字符串还是期望数据缓冲区(C / C ++代码通常使用char *作为缓冲区,这取决于char是一个字节的事实)?

If it is a buffer then a byte[] is certainly correct, but if it expects a string then it may be clearer if you declare the parameter as a string (to be explicit) and use Encoding.ASCII.GetString() to convert the byte[] to a string. 如果它是一个缓冲区,那么byte []当然是正确的,但是如果它期望一个字符串,那么如果将参数声明为字符串(是明确的)并使用Encoding.ASCII.GetString()来转换参数,则可能会更清楚。 byte []转换为字符串。

Also if it C++ function expects a string and you decide to declare the parameter as a byte[], be sure the byte array ends with a zero, since that is how C/C++ determines the end of the string. 同样,如果C ++函数需要一个字符串,并且您决定将参数声明为byte [],请确保字节数组以零结尾,因为这是C / C ++确定字符串结尾的方式。

I'm not a .net expert, but I've needed to do something similar recently. 我不是.net专家,但是最近我需要做类似的事情。

It is not just a matter of serialization, you also have to stop the garbage collector from cleaning up your byte array while it is being used in C++ land... 这不仅仅是序列化的问题,当在C ++领域使用字节数组时,还必须停止垃圾回收器清理字节数组。

The below snippet of C# should help. 下面的C#代码片段应该会有所帮助。

// pin the byte[] (byteArray)  
GCHandle handle = GCHandle.Alloc(byteArray, GCHandleType.Pinned);   
IntPtr address = handle.AddrOfPinnedObject();  
// Do your C++ stuff, using the address pointer.

// Cleanup 
handle.Free();

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

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