如题

解决方案 »

  1.   

    c++
    void CTestDllApp::GetChars(char* pData)
    {
    int a = 10;
        CopyMemory(pData , &a ,4 );
    }c#
        [DllImport(@"C:\Inetpub\wwwroot\Test2005\Bin\TestDll.dll", CharSet = CharSet.Ansi)]
        public static extern void GetChars(ref byte[] pData);
       byte[] pData = new byte[1024];        GetChars(ref pData);运行的时候总是提示尝试写入受保护的内存
      

  2.   

    try
    [DllImport(@"C:\Inetpub\wwwroot\Test2005\Bin\TestDll.dll", CharSet = CharSet.Ansi)]
        public static extern void GetChars( System.IntPtr pData );
       System.IntPtr data = Marshal.AllocHGlobal(4);
       GetChars( data );
       int n =  Marshal.ReadInt32(data);