簡體   English   中英

如何在C#.NET中實現CopyMemory(VB6)?

[英]How do I implement CopyMemory (VB6) in C#.NET?

我有一行代碼:

Dim buf(1 To 255) As Byte

          a$ = "hello"   

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

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

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

請參見Encoding.GetBytesEncoding.Default

我設法解決了這個問題:

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);

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

然后可以由其他應用程序從內存中拾取它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM