469,282 Members | 1,939 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

How do I convert IntPrt data to byte[]?

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);
--
Thanks.
Jun 15 '07 #1
6 4239
Lit

System.Runtime.InteropServices.Marshal.Copy(IntPtr source, byte[]
destination, int start, int length)
"Pucca" <Pu***@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
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);
--
Thanks.

Jun 15 '07 #2
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" wrote:
>
System.Runtime.InteropServices.Marshal.Copy(IntPtr source, byte[]
destination, int start, int length)
"Pucca" <Pu***@discussions.microsoft.comwrote in message
news:B1**********************************@microsof t.com...
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);
--
Thanks.


Jun 15 '07 #3
Pucca wrote:
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
Jun 16 '07 #4
"Pucca" <Pu***@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
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.
Isn't nLength or nSize not the size of pData?

Willy.

Jun 16 '07 #5
Yes, that gives me the size of pData. Thanks.
--
Thanks.
"Arne Vajhøj" wrote:
Pucca wrote:
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
Jun 18 '07 #6
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.
--
Thanks.
"Willy Denoyette [MVP]" wrote:
"Pucca" <Pu***@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
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.

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

Willy.

Jun 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Swarup | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
65 posts views Thread by kyle.tk | last post: by
10 posts views Thread by =?Utf-8?B?Um95?= | last post: by
6 posts views Thread by Bob Altman | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.