473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Marshal.Copy

Hello.... I have an MFC ActiveX and creates a BYTE* buffer(as a
CDialog Class member) and I've wrote a function that gets the pointer
to that buffer and it's size:
void CMainDialog::GetPointers(int *p, int *size)
{
if(buffer != NULL)
{
delete buffer;
buffer = NULL;
}

buffer = new BYTE[10];
memset(buffer, 1, 10);

*p = (int)buffer;
*size = 10;
}
After this method is called, I fire an event to notify the web to get
my pointers.

void CMyActiveXCtrl::LoadParameter(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_MainDialog.GetPointers(&this->p, &this->size);

// Fire an event to notify web page
FireParameterLoaded();
}

In my JavaScipt code, I send via Ajax.NET the values to server side.
On server side I receive them very well... but when I try to make them
managed, the system fails.

If I use:
byte[] buffer = new byte[size];
Marshal.Copy((IntPtr)pointer, signature, 0, size); I get an
exception... an ugly one :
"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt." and the LastWin32Error is
1008:
ERROR_NO_TOKEN
1008 An attempt was made to reference a token that does not exist.
this makes me thinks that the buffer is deleted from the unmanaged
side memory...

I want to know... How can I send a BYTE* buffer from unmanged code to
managed? The above solution is with pointes.
One other solution is to use CString class.. but if I have a 0
characher inside the buffer, I won't get the whole buffer(I've tried
this solution only on non-zero buffers.. and it works).

Thanks

Aug 20 '07 #1
1 4491
You have some problems here.

First, when you are returning memory from COM methods, you are supposed
to use the COM allocator, either through CoTaskMemAlloc, or by obtaining the
allocator interface (IMalloc, CoTaskMemAlloc actually gets this interface to
allocate memory). In calling new, you are violating the memory model of
COM, and you are actually creating a memory leak, as you have no way of
calling delete to release the pointer in memory.

Instead, if you want COM to handle this for you, and also want it to be
available to Automation clients (as well as make it easy to use in .NET),
you should use a SAFEARRAY and pass that back.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"eusebiu" <Ma**********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
Hello.... I have an MFC ActiveX and creates a BYTE* buffer(as a
CDialog Class member) and I've wrote a function that gets the pointer
to that buffer and it's size:
void CMainDialog::GetPointers(int *p, int *size)
{
if(buffer != NULL)
{
delete buffer;
buffer = NULL;
}

buffer = new BYTE[10];
memset(buffer, 1, 10);

*p = (int)buffer;
*size = 10;
}
After this method is called, I fire an event to notify the web to get
my pointers.

void CMyActiveXCtrl::LoadParameter(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_MainDialog.GetPointers(&this->p, &this->size);

// Fire an event to notify web page
FireParameterLoaded();
}

In my JavaScipt code, I send via Ajax.NET the values to server side.
On server side I receive them very well... but when I try to make them
managed, the system fails.

If I use:
byte[] buffer = new byte[size];
Marshal.Copy((IntPtr)pointer, signature, 0, size); I get an
exception... an ugly one :
"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt." and the LastWin32Error is
1008:
ERROR_NO_TOKEN
1008 An attempt was made to reference a token that does not exist.
this makes me thinks that the buffer is deleted from the unmanaged
side memory...

I want to know... How can I send a BYTE* buffer from unmanged code to
managed? The above solution is with pointes.
One other solution is to use CString class.. but if I have a 0
characher inside the buffer, I won't get the whole buffer(I've tried
this solution only on non-zero buffers.. and it works).

Thanks

Aug 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
3
by: Tom | last post by:
I think I'm still a little rough on the principle and understanding of Marshal by value and Marshal by reference after reading various materials. my understanding of Marshal by value is that the...
3
by: kevin | last post by:
Hi I have a C struct that is of the following typedef struct{ DWORD num_conversions; ... short *sample_values; ....
2
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of...
1
by: dast | last post by:
I would need a function to copy unsigned short values from a IntPtr to a ushort-array, but the Marshal.Copy-function only support the short-array! At the moment I do it the following way: ......
4
by: Rainer Queck | last post by:
Hi NG, is there a way to copy a buffer pointed to by a IntPtr directly into a two dimensional byte-array? I tried this, what obviously doesn't work: byte image = new byte; IntPtr p =...
2
by: O.B. | last post by:
I have a structure named EntityState with an explicit layout. The following two operations exist within the class to return a byte array representing the current object. Upon executing them each a...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
0
by: yogiam | last post by:
I have a server listening for connections. Client connects and both start streaming webcam images. This works fine as long as there is only one client connected(running as a Thread). When another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.