2014-01-20 115 views
1

我有一行代碼:如何在C#.NET中實現CopyMemory(VB6)?

Dim buf(1 To 255) As Byte 

      a$ = "hello" 

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

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

+1

乘坐看[System.Text.Encoding.GetBytes()](http://msdn.microsoft.com/en-us/library/ds4kkd55%28v=vs.110%29.aspx)查看示例 – Heslacher

+0

您能否告訴我我如何ob使用VarPtr()內部函數保留字節數組的地址,並將字節數組的地址和長度複製到C#.NET中的COPYDATASTRUCT結構中。 – variable

+2

爲什麼在'COPYDATASTRUCT'中需要它? 'CopyMemory'與此無關。或者真的是你的問題「我如何使用C#中的WM_COPYDATA?」? –

回答

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

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

然後可以從其他應用程序內存拿起..