• 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 was thinking, and was going to post, almost exactly what @Brandon Hjust posted!   WARNING: possible spoilers I also love these videos she makes:   
    • The Witcher 4 tech demo shows off the RPG in action on a base PS5 at 60FPS by Pulasthi Ariyasinghe Epic Games' The State of Unreal 2025 keynote happened today, giving a look at its latest improvements to the Unreal Engine, and other developments and partnerships that the Fortnite maker is involved in. The show kicked things off with a bang, giving fans a live on-stage presentation of The Witcher 4 alongside developer CD Projekt RED. Seen below, the tech demo of the hugely anticipated RPG features the new trilogy's protagonist, Ciri, as she explores Kovir, the new mainland that CD Projekt is letting players explore. Following a monster trail, Ciri and her horse Kelpie go through mountains and forests before reaching the town of Valdrest. Surprisingly, the demo is said to be running on a base PlayStation 5, and it even manages to stay at 60 frames per second despite the stunning visuals. The studio said that the technologies it is using will benefit all platforms that it is targeting for the RPG. The Witcher 4 is coming to PC, Xbox Series X|S, and PlayStation 5 consoles, though a release date has not been revealed yet. "We started our partnership with Epic Games to push open-world game technology forward. To show this early look at the work we’ve been doing using Unreal Engine running at 60 FPS on PlayStation 5, is a significant milestone — and a testament of the great cooperation between our teams," said joint CEO of CD Projekt RED Michał Nowakowski. "But we're far from finished. I look forward to seeing more advancements and inspiring technology from this partnership as development of The Witcher 4 on Unreal Engine 5 continues." The demo showcases tech like Unreal Animation Framework, Nanite Foliage rendering, MetaHuman technology with Mass AI crowd scaling, and more from Epic Games' latest engine developments. Unreal Engine 5.6 is said to include many of these improvements, letting any developer use the tools on their games for better visuals and optimizations.
    • The same guy who promised AGI in a year, and later he said: yes, but no really.
    • Microsoft and security. 🤣 One day, I received a big bill from Microsoft. I checked the log, and the log (not Azure Premium) only lasted a few weeks, so I was unable to find any information about what happened. I checked the bill, and it showed me that somebody (no information) created a big virtual machine for many weeks, then finally deleted it. I contacted support, and they told me that I will get my money back. 8 months later, it never happened. However, the invoices show some weird movements. They indeed did the reverse, but only in the system, but my CC never got the money back. I had a fight with my bank, and my bank finally showed me records that Microsoft never tried to return my money. What happened, who hacked it, and how? Microsoft was unable to answer. My account had a DFA. Conclusion: I should have reported it to my CC.
  • Recent Achievements

    • Week One Done
      Adam Todd earned a badge
      Week One Done
    • Contributor
      Ed B went up a rank
      Contributor
    • One Month Later
      moporcho earned a badge
      One Month Later
    • One Month Later
      Parotel earned a badge
      One Month Later
    • Reacting Well
      Cryptecks earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      202
    2. 2
      snowy owl
      144
    3. 3
      ATLien_0
      133
    4. 4
      Xenon
      120
    5. 5
      +FloatingFatMan
      106
  • Tell a friend

    Love Neowin? Tell a friend!