如何在C#.NET中实现CopyMemory(VB6)?

时间:2014-01-20 12:24:37

标签: c# vb6

我有一行代码:

Dim buf(1 To 255) As Byte

          a$ = "hello"   

Call CopyMemory(buf(1), ByVal a$, Len(a$))

我想在C#.NET中执行它。 C#.NET中上述行的替代方法是什么?

2 个答案:

答案 0 :(得分:1)

string aString = "hello";
byte[] theBytes = Encoding.Default.GetBytes(aString);

请参阅Encoding.GetBytesEncoding.Default

答案 1 :(得分:0)

我设法解决了这个问题: -

string aString = text;
            byte[] theBytes = System.Text.Encoding.Default.GetBytes(aString);
//to copy to memory use the following:-
            // Marshal the managed struct to a native block of memory.
            int myStructSize = theBytes.Length;
            IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize);
            try
            {
                Marshal.Copy(theBytes, 0, pMyStruct, myStructSize);

...........
}

然后可以通过其他应用程序从内存中获取它。

相关问题