Hi, how can i return arguments in my custom native dll like ReadProcessMemory api?

For example for readprocessmemory, byte arguments return readed value. Its mixed c# and c++ so if its wrong place to ask, sorry about that.

       public byte[] ByteOku(uint BellekAdresi, int PID)
        {
            IntPtr readHandle = NativeMethods.OpenProcess(0x10, false, (uint)PID);
            byte[] bytes = new byte[50];
            uint rw = 0;
            NativeMethods.ReadProcessMemory(readHandle, (IntPtr)BellekAdresi, bytes, (UIntPtr)50, ref rw);
            return bytes;
        }





[DllImport("test.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void method_4(byte[] buffer);


private void button1_Click(object sender, EventArgs e)
        {
            byte[] b = new byte[] { 0x5D, 0x0, 0x0, 0x0, 0x0 };
            method_4(b);
                 MessageBox.Show(b[0].ToString() + "   " + b[1].ToString() + "   " + b[2].ToString() +  "   " + b[3].ToString());

        }   

//in dll which is written in c++ mfc        

extern "C" __declspec(dllexport) void method_4(int buffer[])
{
        buffer[1] = 0x5;
        buffer[2] = 0x5;
        buffer[3] = 0x5;
        buffer[4] = 0x5;
}

but buffer array is not modified on button1_click

According to the P/Invoke tool, your DLLImport statement should be

 [System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="method_4")]
public static extern  void method_4(ref int buffer) ;

Of course you replace the <Unkown> with your dll name.

I need to return byte array from the function, in your dllimport statement, method_4 have one argument which has ref int type. Is it possible to create byte array with ref type? Also on readprocessmemory byte array argument defined without ref:

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
        byte[] lpBuffer, UIntPtr nSize, ref uint lpNumberOfBytesWritten);

Im little confused, also im not sure my problem is with my .net part or native part. I can succesfully call my native functions from my native dlls, however im not able to return arrays from them. Of course there is another methods to do that, what but i want to learn that way.

The parameter for the C++ is an int, so C# wants to pass an int. You can use BitConvert.GetBytes to get the bytes from the int. If you want to pass a byte array, change the C++ method to accept a byte array.

As for ReadProcessMemory, my resources says the signature is one of these

[DllImport("kernel32.dll", SetLastError = true)]
  static extern bool ReadProcessMemory(
    IntPtr hProcess,
    IntPtr lpBaseAddress,
    [Out] byte[] lpBuffer,
    int dwSize,
    out int lpNumberOfBytesRead
   );

   [DllImport("kernel32.dll", SetLastError = true)]
   static extern bool ReadProcessMemory(
    IntPtr hProcess,
    IntPtr lpBaseAddress,
    [Out, MarshalAs(UnmanagedType.AsAny)] object lpBuffer,
    int dwSize,
    out int lpNumberOfBytesRead
   );

   [DllImport("kernel32.dll", SetLastError = true)]
   static extern bool ReadProcessMemory(
    IntPtr hProcess,
    IntPtr lpBaseAddress,
    IntPtr lpBuffer,
    int dwSize,
    out int lpNumberOfBytesRead
   );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.