• 0

[VB.NET] Memory Address


Question

9 answers to this question

Recommended Posts

  • 0

There was something known as the VarPtr function in VB (actually, in the BASIC language itself), but after some quick Googling, it isn't found in VB.NET, most likely due to the fact that it is "unmanaged". You might try digging something up along the lines of a VB.NET VarPtr replacement, however. For example, I found something with System.Runtime.InteropServices.GCHandle as well as something called BitConverter. I'm out of my element when it comes to .NET, but I figured that might help to get you started at least. Those last two seem wrong for memory, but I didn't look them up so they might be able to manipulate memory, despite their names.

  • 0

I wouldn't suggest messing with a .NET process' memory (due to CRL and other unpredictable aspects) so you should probably stick with other dummy processes and I'm not sure if .NET offers memory reading APIs either, so you'd probably be best using WinAPI in this:

Just look into OpenProcess, ReadProcessMemory, WriteProcessMemory, etc.

  • 0
  x0r said:
I wouldn't suggest messing with a .NET process' memory (due to CRL and other unpredictable aspects) so you should probably stick with other dummy processes and I'm not sure if .NET offers memory reading APIs either, so you'd probably be best using WinAPI in this:

Just look into OpenProcess, ReadProcessMemory, WriteProcessMemory, etc.

ReadProcessMemory, how would I do that?

Maybe something like this?

Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Sub ReadMem()
Dim myProcesses As Process = Process.GetProcessesByName(pinball)
Dim memRead as String = myProcesses.ReadProcessMemory

Anything close to what your thinking?

  • 0

VB.NET doesn't natively supported memory pointers and allocations, but this can be accomplished through .NET Framework classes like IntPtr, Marshal and GCHandle. There are probably other types as well, but I've never used them.

IntPtr is used as a pointer to a platform-specific integer. Marshal can read and write data at a specific memory location (leveraging IntPtr's).

Example:

Dim pointer as IntPtr
Dim number as Integer = 20
pointer = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(pointer, number)

Example:

Dim anotherInteger as Integer = Marshal.ReadInt32(pointer)

Example:

Dim message as String = "I know, I'm awesome..."
Dim pointer as IntPtr = Marshal.StringToHGlobalAuto(message)

Example:

Dim anotherMessage as String = Marshal.PtrToStringAuto(pointer)

You can see that the Marshal class acts as a broker between the program code and the memory operation.

You can also use the Marshal class on VB.NET Structures:

Example:

Public Structure MyStruct
	Dim x as Integer
	Dim y as Integer
End Structure

Dim structure1 as MyStruct
structure1.x = 1
structure1.y = 2

Dim pointer = Marshal.AllocHGlobal(Marshal.SizeOf(structure1))
Marshal.StructureToPtr(structure1, pointer, True)

Dim structure2 as MyStruct = Marshal.PtrToStructure(pointer, New MyStruct().GetType)

Objects are handled differently, because of their Garbage Handling requirements (they are Managed Types). You can use a GCHandle to read and write objects.

Example:

Public Class MyClass
	Public Name As String
	Public Age as Integer
End Class

Dim handle as GCHandle
Dim person as MyClass
person.Name = "Matthew Abbott"
person.Age = 24

handle = GCHandle.Alloc(person)

Example:

Dim person2 as MyClass = handle.Target
handle.Free()

You should check out the System.Runtime.InteropServices namespace on MSDN and see what you can come up with.

Hope that helps.

Adapted from: DotNetBips.com

  • 0

The WINAPI functionality is accessed in a similar way to the C# alternative. This is known as P/Invoke (Platform Invoke), which allows managed code to invoke unmanaged code residing in libraries on the Host computer.

In VB.NET, to declare a WINAPI function, you do the following (e.g. CopyMemory):

Public Static Class WINAPI
	Declare Static Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDst As IntPtr, ByVal pSrc As String, ByVal length As Long)
End Class

In the above example, I have declared a static class, so we can access our WINAPI functions from a single location, without instantiation, ie., you would call WINAPI.CopyMemory(...).

You should look into P/Invoke for more information on using external unmanaged code from .NET.

Also, check out http://www.pinvoke.net for more information.

  • 0

Does this code works for two different applications? I mean if I alloc one object in the first application, pass the memory address to the other, is the other able to start another object (same instance) pointing to the same object. Can we use the same object in different applications? If not what would be the best way to do so? Thank you very much in advance, it's been a long time I'm trying to make it work, thanks again.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I'm old I guess, first thing I think of is just regular input/output.
    • Now, kids, Dan O'Dowd is what we call a professional hater.
    • Billionaire slams 'Tesla Cultists' for praising Robotaxi, says it's 5+ years behind Waymo by David Uzondu Image via Depositphotos.com The Tesla Robotaxi program has kicked off in Austin, Texas, and reactions are pouring in from all corners of the internet. A select group of investors and influencers have been invited to try the service, which operates within a limited area of South Austin for a price of $4.20. While the vehicles are operating without anyone in the driver's seat, the program has specific rules for this pilot phase, including a human "safety monitor" who rides along in the passenger seat just in case things go sideways. Of course, the launch did not go unnoticed by Tesla's most vocal and well-funded critic, Dan O'Dowd. O'Dowd is the billionaire founder of a group called The Dawn Project, which has dedicated itself to highlighting what it calls critical safety failures in Tesla's Full Self-Driving software. He refers to himself as an expert in creating "unhackable" software for military and aerospace clients, and ran for U.S. Senate back in 2022 on a single-issue platform: to "make computers safe for humanity" by banning Tesla's FSD. In 2023, He was banned from advertising on X after He made promoted posts that show Tesla FSD among other things, failing to stop at Stop signs. Last year, his group, The Dawn Project, paid for a Super Bowl ad, where a Tesla equipped with FSD did not act on a child-sized mannequin in the road. That commercial ends with a message, urging parents to "boycott Tesla to keep your kids safe." Today, O'Dowd took to X to slam the launch of the Robotaxi service, saying the "Tesla Cultists are celebrating victory" over a system he believes is years behind the competition (especially Waymo). He pointed out that with only fourteen cars operating for half the day, the system was already making significant errors, a rate he claims is consistent with community-tracked FSD data. The videos shared by the creators (Rob Maurer and Ed Niedermeyer), O'Dowd mentioned in his post, appear questionable, depending on your perspective. In Maurer's video, a trip that was otherwise smooth had a few unnerving seconds of the vehicle slightly swerving into the wrong lane, correcting itself, swerving again, correcting itself, and then finally settling. The other video from Ed Niedermeyer shows something entirely different. Niedermeyer captured a Tesla Robotaxi approaching an "extensive crime scene" with multiple police vehicles parked on the side of the road. On his personal Bluesky account (Ed stopped posting on X late last year, in protest of Musk), He claims the Tesla braked hard twice for no clear reason. In his commentary, Niedermeyer argued the car "shouldn't react to any of these police vehicles," and that it was concerning how it reacted to some but not others, before stopping in the "middle of the road instead of defaulting to a minimal risk condition."
    • Arch is now also using Wayland as the default session for Plasma 6.4, with X11 session becoming optional (so upgrading to Plasma 6.4 on X11 Arch might need manual intervention). It's been well over a decade in making, but I guess the time for Wayland to be the default is finally upon us.
  • Recent Achievements

    • Week One Done
      ravenmanNE earned a badge
      Week One Done
    • Conversation Starter
      Brett76 earned a badge
      Conversation Starter
    • One Month Later
      Miguel Batista earned a badge
      One Month Later
    • Dedicated
      moojay67 earned a badge
      Dedicated
    • One Month Later
      Jim Dugan earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      662
    2. 2
      Michael Scrip
      229
    3. 3
      ATLien_0
      216
    4. 4
      Steven P.
      146
    5. 5
      Xenon
      141
  • Tell a friend

    Love Neowin? Tell a friend!