473,407 Members | 2,629 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,407 software developers and data experts.

Byte Pointer in VB

Hello,

I have come across this bit of code in a C# project and want to try and
replicate the behaviour in VB:

System.IntPtr ptrScan0 = objBMData.Scan0;
System.IntPtr ptrSrcScan0 = objBMSrc.Scan0;

byte * pOrig = (byte *)(void *)ptrScan0;
byte * pSrc = (byte *)(void *)ptrSrcScan0;

I know that VB does not support byte pointers but there must be a way
of doing this. The code then goes on to do this:

for(int y=0;y < yVal;++y)
{
for(int x=0; x < xVal; ++x )
{
pOrig[0] = pSrc[newVal1];
pOrig[1] = pSrc[newVal2];
pOrig[2] = pSrc[newVal3];
pOrig += 3;
}
pOrig += nOffset;
}

I may be wrong (i've never been a C programmer) but this looks like we
are setting three values in memory based on the current position of the
pointer and then shifting the start position of the pointer by three
through each iteration of the inner for loop. Again, I would need to
know how to do this in VB.NET.

Your help is much appreciated.

IAN KEVAN

Nov 21 '05 #1
2 8644

You can use Marshal.Copy to copy the data from the memory pointed to
by the IntPtr to a local byte array, do your manipulations, and then
copy it back.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
Hi Mattias,

Thanks for that. I had already tried Marshal.Copy but it hadn't worked
because I was setting the byte array size incorrectly. After your
message I figured I would try again and this time I have got it to
work. The VB code now looks like this:

Dim ptrScan0 As IntPtr = objBMData.Scan0
Dim ptrSrcScan0 As IntPtr = objBMSrc.Scan0

Dim pOrig(CorrectArrayLength) As Byte
Call Marshal.Copy(ptrScan0, pOrig, 0, CorrectArrayLength)
Dim pSrc(CorrectArrayLength) As Byte
Call Marshal.Copy(ptrSrcScan0, pSrc, 0, CorrectArrayLength)

Dim intCount As Integer = 0
For intY As Integer = 0 To intHeight - 1
For intX As Integer = 0 To intWidth - 1
p(intCount) = pSrc(intNewVal)
p(intCount + 1) = pSrc(intNewVal + 1)
p(intCount + 2) = pSrc(intNewVal + 2)
intCount = intCount + 3
Next
intCount = intCount + intOffset
Next

Call Marshal.Copy(pOrig, 0, ptrScan0, CorrectArrayLength)

It is considerably slower that the unsafe C# code but faster than
altering the pixels using SetPixel and GetPixel.

Thanks again,

IAN

Nov 21 '05 #3

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

Similar topics

289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
5
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
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...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
3
by: tash.robinson | last post by:
I have been pulling my hair out with this and have searched and found some solutions but I can't seem to get them to work as expected. I have a structure, which I have declared explicitly using...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.