How do I convert IntPrt data to byte[]?

G

Guest

Hi, I'm using VS2005, .net 2 and C# for windows application. I need to
convert a IntPtr to a byte[] to be able to add a meetingBlob data to the
meeting class object in Active Directory schema. I get a "unspecified error"
if I tried to add the data before converting to a byte[] in
"deNewContextObject.Properties["meetingBlob"].Add((object)blob.pData);".

public struct Blob
{
public IntPtr pData;
public int nLength;
public int nSize;
}

int nBytes = Marshal.SizeOf(typeof(CUnityDS.Blob));
IntPtr ptr = Marshal.AllocHGlobal(nBytes);

// create an instance of the Blob structure
CUnityDS.Blob blob = new CUnityDS.Blob();

// copy and pin the structure to that location
Marshal.StructureToPtr(blob, ptr, true);

// Pass it by reference
// OK, now it's time to "reconsitute" the structure
blob = (CUnityDS.Blob)Marshal.PtrToStructure(ptr,
typeof(CUnityDS.Blob));


CUnityDS.DE_ERRORS errcode = CUnityDS.DE_ERRORS.DE_MEMORY_ALLOCATION_FAILURE;
//EncodeAsnUser is a C unmanaged code that puts data from userContextData
struct to IntPtr
errcode = CUnityDS.LibWrap.EncodeAsnUser(ref blob, userContextData);
//Getting unspeficied error with the following line becuase it needs a
byte[] and not IntPtr
deNewContextObject.Properties["meetingBlob"].Add((object)blob.pData);
 
L

Lit

System.Runtime.InteropServices.Marshal.Copy(IntPtr source, byte[]
destination, int start, int length)
 
G

Guest

Thank yuou Lit. The blob is returned to me but the length is not set. How
can I get the length of the IntPrt for the parameter? Thank you.
--
Thanks.


Lit said:
System.Runtime.InteropServices.Marshal.Copy(IntPtr source, byte[]
destination, int start, int length)


Pucca said:
Hi, I'm using VS2005, .net 2 and C# for windows application. I need to
convert a IntPtr to a byte[] to be able to add a meetingBlob data to the
meeting class object in Active Directory schema. I get a "unspecified
error"
if I tried to add the data before converting to a byte[] in
"deNewContextObject.Properties["meetingBlob"].Add((object)blob.pData);".

public struct Blob
{
public IntPtr pData;
public int nLength;
public int nSize;
}

int nBytes = Marshal.SizeOf(typeof(CUnityDS.Blob));
IntPtr ptr = Marshal.AllocHGlobal(nBytes);

// create an instance of the Blob structure
CUnityDS.Blob blob = new CUnityDS.Blob();

// copy and pin the structure to that location
Marshal.StructureToPtr(blob, ptr, true);

// Pass it by reference
// OK, now it's time to "reconsitute" the structure
blob = (CUnityDS.Blob)Marshal.PtrToStructure(ptr,
typeof(CUnityDS.Blob));


CUnityDS.DE_ERRORS errcode =
CUnityDS.DE_ERRORS.DE_MEMORY_ALLOCATION_FAILURE;
//EncodeAsnUser is a C unmanaged code that puts data from userContextData
struct to IntPtr
errcode = CUnityDS.LibWrap.EncodeAsnUser(ref blob, userContextData);
//Getting unspeficied error with the following line becuase it needs a
byte[] and not IntPtr
deNewContextObject.Properties["meetingBlob"].Add((object)blob.pData);
 
G

Guest

Pucca said:
Thank yuou Lit. The blob is returned to me but the length is not set. How
can I get the length of the IntPrt for the parameter? Thank you.

Marshal.SizeOf maybe.

Arne
 
W

Willy Denoyette [MVP]

Pucca said:
Thank yuou Lit. The blob is returned to me but the length is not set.
How
can I get the length of the IntPrt for the parameter? Thank you.

Isn't nLength or nSize not the size of pData?

Willy.
 
G

Guest

Hi Willy,
I thought it is. My code is calling a fucntion to encode the user structure
data into a blob. Well, as it turns out. I'm able to call it successfully
but the data is not encoded correctly into a blob. I just posted that
problem. It will be great if you can take a look at that. Thank you. (my
title of the problem is "Need Help declaring parameter variables for
DllImport c-code dll"). Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top