• 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

    • WinToUSB 9.9 by Razvan Serea WinToUSB allows you to install and run a fully-functional Windows on external hard drive, USB flash drive or Thunderbolt drive. It is so easy and efficient, with just 3 steps and a few minutes, you can create your first portable Windows 11/10/8/7 or Windows Server directly from an ISO, WIM, ESD, SWM, VHD, VHDX file or CD/DVD drive, or you can clone currently running Windows installation to USB or Thunderbolt drive as portable Windows. WinToUSB also supports creating Windows installation USB drive from Windows 11/10/8/7 and Windows Server installation ISO, with it you can install Windows from the USB drive easily. Note: The WinToUSB Free Edition is solely intended for non-commercial, private, and personal use on home computers. It should be noted that technical support is not available for the free edition. Use of WinToUSB Free Edition within any organization or for commercial purpose is strictly prohibited. WinToUSB key features include: Creation of Windows To Go from ISO, WIM, ESD, SWM, VHD(X) or DVD drive.Improved Clone Windows 11/10/8/7 to USB/Thunderbolt drive as portable Windows. Creation of Windows To Go on Non-Certified Windows To Go USB drive. Encrypt Windows To Go with BitLocker to keep your data safe. Creation of Windows installation and bootable WinPE USB drive with BIOS & UEFI support. Download Official Windows 11/10/8.1 ISO file from Microsoft. Use any edition of Windows 11/10/8/7 and Windows Server 2022/2019/2016/2012/2010 to create Windows To Go USB drive. Windows To Go (Portable Windows) Creator WinToUSB allows you to install & run fully-functional Windows on an external HDD/SSD, USB flash drive or Thunderbolt drive, which means you can carry the portable Windows drive to anywhere and use it on any computer. Faster installation and cloning speed compared to competing products Support any edition of Windows 11/10/8/7 and Windows Server Creation of Windows To Go from ISO, WIM, ESD, SWM, VHD(X) or CD/DVD drive Clone currently running Windows to USB/Thunderbolt drive Creation of Windows To Go on Non-Certified Windows To Go drive Create BitLocker encrypted Windows To Go Workspace Create portable Windows for Intel-based Mac computers Support for creating VHD(X)-based Windows To Go Windows Installation USB Creator WinToUSB releases a feature called "Windows Installation USB Creator" which allows you to create a Windows installation USB drive from a Windows 11/10/8/7/vista/2022/2019/2016/2012/2008 installation ISO file with a few simple steps, with this feature you can create a bootable Windows installation USB drive to install Windows on both Traditional BIOS and UEFI computers by using the same USB drive. Bypass Windows 11 system requirements (TPM 2.0, Secure Boot, Minimum hardware and Microsoft account) Install Windows on both BIOS and UEFI computers by using the same USB drive Windows PE Bootable USB Creator This feature allows you to create a bootable Windows PE USB drive, it can help you transfer the contents of a Windows PE ISO file to a USB drive and make the USB drive bootable, and this feature supports the creation of a bootable WinPE USB driver that supports both Traditional BIOS and UEFI computers. WinToUSB 9.9 changelog: Added option to disable BitLocker automatic drive encryption when creating Windows installation USBs Fixed bug: setup.exe cannot bypass the Windows 11 system requirements Fixed bug: Cloned Windows ARM64 cannot start properly Fix other minor bugs Download: WinToUSB 9.9.0 | 28.7 MB (Freeware) Links: Home Page | Free vs Pro Comparison | Screnshots Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Maybe you don't realize this...but everything you said agreed with me. Yes, many tech outlets reported on Ryzen 9000 issues prior to 24H2, which I already addressed, and as I already said, that issue only existed for a few short months. Ryzen 9000 was released the same quarter of 2024 as 24H2. So again...months, not years. I also already said 24H2 showed some minor improvements on older Ryzen CPU. The article you posted agrees with me, and even says the improvements were only 3-5%, which is even more harsh a statement than what I said. If you want to fuss on the 3-5% numbers, then yes, I will grant you that was an issue for an extended amount of time. In my opinion, that is such a small amount it isn't worth fussing over, but you are welcome to a different option. However, if that was your point, then you didn't make that point in good faith, because you highlighted Ryzen 9000 so much, which had a FAR bigger and FAR shorter issue, it's really a very different conversation.
    • The vast majority of users do not care which iOS version they're using. They don't even know or bother updating to the latest version, unless they see the prompt. The version numbers is more for the power users and I don't think Apple renamed their OS for them.
    • No operating system works well with programs that modify its interface. There will always be bugs, whether it's MacOS or Linux. Windows too, you'll only suffer from bugs using this crap software.
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      snowy owl
      251
    3. 3
      +FloatingFatMan
      251
    4. 4
      ATLien_0
      228
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!