access memory within unmanaged dll
-
hi, i'm stuck. :( i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like
int foo(void *ppMessage);
the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two _p_s in the name) in c++ it would be something like
byte *result;
foo(&result);
(*result)[0] //is the first byte of the resulti have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged :~ thx in advance :wq
-
hi, i'm stuck. :( i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like
int foo(void *ppMessage);
the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two _p_s in the name) in c++ it would be something like
byte *result;
foo(&result);
(*result)[0] //is the first byte of the resulti have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged :~ thx in advance :wq
-
hi, i'm stuck. :( i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like
int foo(void *ppMessage);
the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two _p_s in the name) in c++ it would be something like
byte *result;
foo(&result);
(*result)[0] //is the first byte of the resulti have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged :~ thx in advance :wq
First of all, are you sure about the signature of your C method ? Isn't it more like this :
int foo(void **ppMessage);
To expose this C function to managed code, you need to declare it as :
[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
public static extern int foo(out IntPtr ppMessage);where
user32.dll
must be replaced with the actual DLL name.
And I swallow a small raisin.
-
First of all, are you sure about the signature of your C method ? Isn't it more like this :
int foo(void **ppMessage);
To expose this C function to managed code, you need to declare it as :
[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
public static extern int foo(out IntPtr ppMessage);where
user32.dll
must be replaced with the actual DLL name.
And I swallow a small raisin.
erm no, i looked it up in the header-file and there's only one star. i'm a bit confused too, but it seems you have to know, that you get a pointer in the specified location. actually i have solved the thing like this:
[DllImport("theold.dll")]
public static extern Int32 foo(IntPtr [] messagePtr);...
IntPtr [] messagePtr = new IntPtr[1];
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr[0]);
byte [] message = new byte[size];
Marshal.Copy(messagePtr[0],message,0,size);i know (from the specification) that the first 2 bytes contain the size of the result in bytes. :wq
-
erm no, i looked it up in the header-file and there's only one star. i'm a bit confused too, but it seems you have to know, that you get a pointer in the specified location. actually i have solved the thing like this:
[DllImport("theold.dll")]
public static extern Int32 foo(IntPtr [] messagePtr);...
IntPtr [] messagePtr = new IntPtr[1];
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr[0]);
byte [] message = new byte[size];
Marshal.Copy(messagePtr[0],message,0,size);i know (from the specification) that the first 2 bytes contain the size of the result in bytes. :wq
Hi, it seems you should be able to remove the array like:
[DllImport("theold.dll")]public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,2,size); //remmeber that is only the size, and it is not included in byte[]I'm interested, does this work too? MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
-
Hi, it seems you should be able to remove the array like:
[DllImport("theold.dll")]public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,2,size); //remmeber that is only the size, and it is not included in byte[]I'm interested, does this work too? MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
1. yep. out works indeed. good idea. but in the foo-call is to be inserted uswell. 2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes) finally:
[DllImport("theold.dll")]
public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(out messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,0,size);thx again for your help an ideas :rose: :wq
-
1. yep. out works indeed. good idea. but in the foo-call is to be inserted uswell. 2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes) finally:
[DllImport("theold.dll")]
public static extern Int32 foo(out IntPtr messagePtr);
...
IntPtr messagePtr;
Int32 ret = foo(out messagePtr);
Int16 size = Marshal.ReadInt16(messagePtr);
byte [] message = new byte[size];
Marshal.Copy(messagePtr,message,0,size);thx again for your help an ideas :rose: :wq
Rüpel wrote: but in the foo-call is to be inserted uswell. oops :), happens when u write the code out of your head straight into the textbox :) Rüpel wrote: 2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes) But wont the fist 2 byte values (being a size value) affect the byte[]? (oops another typo :), meant to do this)
byte [] message = new byte[size - 2];
Marshal.Copy(messagePtr,message,2,size);or
byte [] message = new byte[size - 2];
Marshal.Copy(new IntPtr(messagePtr.ToInt32() + Marshal.SizeOf(typeof(byte))*2),message,0,size - 2);I'm just interested :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D