• 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

    • If you're not getting the price you're expecting, would it be easier to offload if you sell the parts individually?
    • How to watch Apple's WWDC 2025 event online happening from June 9 to 13 by Steven Parker Apple officially shared the details of its upcoming Worldwide Developers Conference (WWDC) 2025 a couple of weeks ago. Notably, WWDC 2025, with the tagline "On the horizon," will be a week-long event from June 9 to June 13. It's an online event, free of charge, and will bring together developers from across the globe to learn about the latest Apple tools and technologies. A special in-person event will also take place at Apple Park on June 9, at which 1,000 developers and students will attend. Things will kick off on June 9 at 10 AM PDT with Apple Keynote, where viewers will witness the launch of new operating systems: iOS 19, iPadOS 19, WatchOS 12, macOS 16, and visionOS 3. The keynote can be viewed live on Apple's official website, the Apple TV app, and Apple's YouTube channel (below). Later today, at 1 PM PDT, Apple will stream the "Platforms State of the Union" session. In this, developers will learn about the new tools and features available across Apple operating systems. Apple has planned over 100 technical sessions over the week of the event, which will also be accessible online. Apple experts will discuss everything from app development to machine learning to game design. Interested developers can schedule one-to-one sessions and online Labs with Apple experts for guidance. For the second year in a row, Apple will also highlight young talent through the Swift Student Challenge, where 50 standout students will get a chance to visit Apple Park for a three-day experience. Apple is expected to introduce a slew of Apple Intelligence features, such as intelligent battery management and a virtual health assistant. According to Bloomberg's Mark Gurman, the upcoming visionOS 3 update is also speculated to be significant.
    • 18 years later, I still find it fresh and modern compared to all that is out there.
    • Treat yourself to an Office 2021 Professional offline license at up to 77% off by Steven Parker Get all these essential Microsoft apps for your Windows PC Today's deal comes via our Apps + Software section of the Neowin Deals store, where you can save up to 77% off on a lifetime license to Microsoft Office 2021 for Mac or Windows. This bundle is for families and small businesses who want classic Office apps and email (not the $69.99 per year Office 365 which disappears once you stop payuing for it). It includes Word, Excel, PowerPoint, Outlook, Teams, and OneNote. A one-time purchase installed on 1 Mac or Windows PC for use at home or work yours to keep forever!. Lifetime license for MS Word, Excel, PowerPoint, Outlook, Teams, & OneNote One-time purchase installed on 1 Windows PC for use at home or work Instant Delivery & Download – access your software license keys and download links instantly Free customer service – only the best support! Microsoft Office Home & Business 2021 for Mac includes: Microsoft Office Word Microsoft Office Excel Microsoft Office PowerPoint Microsoft Office Outlook Microsoft Office Teams Microsoft Office OneNote Microsoft Office Professional 2021 for Windows includes: Microsoft Office Word Microsoft Office Excel Microsoft Office PowerPoint Microsoft Office Outlook Microsoft Office Teams Microsoft Office OneNote Microsoft Office Publisher Microsoft Office Access No faffing about with subscriptions, just classic apps that don't expire.Good to Know ONE-TIME PURCHASE INSTALLED ON 1 DEVICE Redemption deadline: redeem your code within 30 days of purchase Access options: desktop Full versions No subscriptions – no monthly/annual fees Version: 2021 Updates included For example, a lifetime subscription to Microsoft Office normally costs $219.99, but this deal can be yours for just $49.97, that's a saving of $150. For full terms, specifications, and license info, click the link below. Get for Windows at $49.97, or for Mac at $69.97 See all discounted Neowin Deals on offer. This is a time-limited deal. Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
  • Recent Achievements

    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
    • Reacting Well
      eric79XXL earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      282
    3. 3
      ATLien_0
      253
    4. 4
      snowy owl
      202
    5. 5
      Edouard
      202
  • Tell a friend

    Love Neowin? Tell a friend!