سلام
چطور میتونم یه Pointer توی VB.net تعریف کنم که به آدرس یه متغیر اشاره کنه؟
ممنون از جوابتون
Dim x As Single = 12.5
Dim gc As System.Runtime.InteropServices.GCHandle
gc = System.Runtime.InteropServices.GCHandle.Alloc(x, Runtime.InteropServices.GCHandleType.Pinned)
MessageBox.Show(gc.AddrOfPinnedObject())
Dim p As IntPtr = gc.AddrOfPinnedObject
gc.Free()
Imports System.Runtime.InteropServices
.
.
.
Dim x As Single = 12.5
Dim gc As GCHandle
gc = GCHandle.Alloc(x, GCHandleType.Pinned)
MessageBox.Show(gc.AddrOfPinnedObject())
gc.Free()
[COLOR=black][FONT=Consolas] [/FONT][/COLOR]
[COLOR=black][FONT=Consolas] [/FONT][/COLOR][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][COLOR=black][FONT=Consolas] x [/FONT][/COLOR][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Single[/FONT][/COLOR][COLOR=black][FONT=Consolas] = 12.5
[/FONT][/COLOR][COLOR=blue]Dim[/COLOR] gc [COLOR=blue]As[/COLOR] [COLOR=#2b91af]GCHandle[/COLOR] [/LEFT]
[COLOR=blue]Dim[/COLOR] p [COLOR=blue]As[/COLOR] [COLOR=#2b91af]IntPtr[/COLOR]
gc = [COLOR=#2b91af]GCHandle[/COLOR].Alloc(x, [COLOR=#2b91af]GCHandleType[/COLOR].Pinned)
p = gc.AddrOfPinnedObject()
MsgBox(p)
x = 5
MsgBox(p)
[LEFT][COLOR=black][FONT=Consolas]gc.Free()
[/FONT][/COLOR][/LEFT]
سلام
خیلی ممنون از جوابتون
اما کد زیر نتیجه ای را که میخواهم به من نمی دهد
نمی دانم مشکل کجاست؟
در MsgBox اول باید عدد 12.5 و در دومی باید 5 نمایش داده شود. درسته؟
کد:[COLOR=black][FONT=Consolas] [/FONT][/COLOR] [COLOR=black][FONT=Consolas] [/FONT][/COLOR][COLOR=blue][FONT=Consolas]Dim[/FONT][/COLOR][COLOR=black][FONT=Consolas] x [/FONT][/COLOR][COLOR=blue][FONT=Consolas]As[/FONT][/COLOR][COLOR=blue][FONT=Consolas]Single[/FONT][/COLOR][COLOR=black][FONT=Consolas] = 12.5 [/FONT][/COLOR][COLOR=blue]Dim[/COLOR] gc [COLOR=blue]As[/COLOR] [COLOR=#2b91af]GCHandle[/COLOR] [/LEFT] [COLOR=blue]Dim[/COLOR] p [COLOR=blue]As[/COLOR] [COLOR=#2b91af]IntPtr[/COLOR] gc = [COLOR=#2b91af]GCHandle[/COLOR].Alloc(x, [COLOR=#2b91af]GCHandleType[/COLOR].Pinned) p = gc.AddrOfPinnedObject() MsgBox(p) x = 5 MsgBox(p) [LEFT][COLOR=black][FONT=Consolas]gc.Free() [/FONT][/COLOR][/LEFT]
خب چطور محتویات آدرسی که p بهش اشاره میکنه رو نمایش بدم؟
Dim a(0 To 0) As Single
Marshal.Copy(p, a, 0, 1)
Private Property PtrValue(ByVal ptr As IntPtr) As Single
Get
Dim a(0 To 0) As Single
Marshal.Copy(ptr, a, 0, 1)
Return a(0)
End Get
Set(ByVal value As Single)
Dim a() As Single = {value}
Marshal.Copy(a, 0, ptr, 1)
End Set
End Property
PtrValue(p) = 12.5
Dim n As Single = PtrValue(p)
Dim x As Single = 12.5
Dim gc As GCHandle = GCHandle.Alloc(x, GCHandleType.Pinned)
Dim p As IntPtr = gc.AddrOfPinnedObject
Dim n As Single = PtrValue(p)
x += 7.5
MessageBox.Show(n.ToString() + ", " + PtrValue(p).ToString())
gc.Free()
Dim p As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Single)))
PtrValue(p) = 12.5
Dim n As Single = PtrValue(p)
PtrValue(p) += 7.5
MessageBox.Show(n.ToString() + ", " + PtrValue(p).ToString())
Marshal.FreeHGlobal(p)
چاره اش اینه که بجای حافظه مدیریت شده متغیری مثل x با Marshal.AllocHGlobal حافظه مدیریت نشده تخصیص بدهید :
کد:Dim p As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(Single))) PtrValue(p) = 12.5 Dim n As Single = PtrValue(p) PtrValue(p) += 7.5 MessageBox.Show(n.ToString() + ", " + PtrValue(p).ToString()) Marshal.FreeHGlobal(p)
سلام خیلی خیلی ممنون از حوابتون
اینجا باید با تغییر مقدار متغیر P مقدار n هم تغییر کنه، درسته؟
چون هر دوی آنها دارند به یک مکان از حافظه اشاره میکنند
اما با تغییر P مقدار n همان 12.5 باقی می ماند
من میخواهم در صورت تغییر مقدار P متغیر n هم تغییر کند یا برعکس
بازهم ممنونم از جوابهای ارزنده و بدرد بخورتون