如何转换VB.net的不安全指针



如何将此代码从C#传输到VB.net?

这是来自C#

if (local > 0)
{ //Local patches set offsets to data located elsewhere in this section 
IntPtr start = data + section->_localPatchesOffset; 
LocalPatch* patch = (LocalPatch*)start; 
while ((int)patch - (int)start < local && patch->_dataOffset >= 0) 
{ //Make the pointer offset relative to itself so it's self-contained 
int ptrOffset = patch->_pointerOffset; 
int* ptr = (int*)(data + ptrOffset); 
*ptr = patch->_dataOffset - ptrOffset; 
patch++; 
} 
}

或者我有这个来自C:的样品

它在C 中的(字节*(到底意味着什么

例如,我有一些字节数组(比如从孔文件加载的MemoryStream…(

Dim arr As Byte() = New Byte() {1, 2, 33, 4, 55, 6, 7, 8, 9, 10, 10, 114, .....}

例如,33是(在偏移3处(,114是(在过偏移12处(我想这不是。。。Arr(from offset(=Arr(tooffset(

这是来自C:

*(byte**)(SectionStart + LF->fromOffset) = SectionStart + LF->toOffset;

正如jmc所指出的,你将无法将该代码转换为vb然后编译它。我认为你唯一的选择是制作一个c#DLL项目,将该代码(以及它需要的任何额外代码(作为一个方法放入其中,然后编译c#

完成此操作后,您可以引用DLL,或者甚至可以将C#项目添加到解决方案中,继续使用VB项,并引用该项目。所有的程序都会编译,甚至调试器也会很高兴地从你的VB.NET代码进入c#,然后再返回;当编译时,这并不是两种独立的语言——你可以认为每种语言只是IL的一大堆语法糖;但由于VB根本没有糖来处理不安全的东西,你必须在C#中进行

(byte*(是指向函数的指针,该函数的参数是指向byte类型指针的指针。

关于不安全的指针,以下是我的《增强Visual Basic》一书的节选:

关于恢复VarPtr、ObjPtr、StrPtr、VarPtrArray和VarPtrStringArray的注意事项。

如果升级后的VB6代码需要"未记录"的VB6函数VarPtr、ObjPtr、StrPtr、VarPtrArray或VarPtrStringArray,请使用以下一个VB.NET函数来复制所有函数:

Imports System.Runtime.InteropServices
'--------------------------------------------------------------------------------------
' This Module Provides the memory addresses of Numeric Variables, Objects, and Strings.
'--------------------------------------------------------------------------------------
Module modVarPtr
'VB.NET version of VB6 VarPtr, ObjPtr, StrPtr, etc. (ALL of them are now supported by this one function).
'----------------------------------------------------------------------------------------------------------------------
'NOTES: Strings are not BSTR, so this returns the text address, not a BSTR pointer to the address pointing to it. This
'       provides C# 'Unsafe Pointers' for VB.NET! Use the returned address right away before the Garbage Collector
'       tries to change it! If we must hold it a short time, do not execute the GC.Free() instruction until done.
'       We can break this code out in-line, using GC.Free() once we are done, but do not go on Break in the meantime.
'       Garbage Collection will be held up until we GC.Free(), until then preventing the release of unused objects.
'----------------------------------------------------------------------------------------------------------------------
Public Function VarPtr(ByVal o As Object) As IntPtr             'Object 'catch all' (Return can also be 'As Integer').
Dim GC As GCHandle = GCHandle.Alloc(o, GCHandleType.Pinned) 'Get a trackable handle and pin (lock) the o address.
VarPtr = GC.AddrOfPinnedObject                              'Get an IntPtr to the pinned o (var's data address).
GC.Free()                                                   'free the allocated space used and unlock o address.
End Function 'Be aware IntPtr is 32-bit only on x86 compiles, otherwise it is 64-bit on 64-bit systems.
End Module

我感到惊讶的是,在1999年提出的VB.NET中,许多VB6用户要求一个完整的VBOOPL环境,而绝对不牺牲OOPL功能或数据安全,并威胁说,如果他们的要求得不到满足,就要放弃VB。然而,当他们明白这一点时,他们竟然厚颜无耻地抱怨失去了不安全的VarPtr,它不符合OOPL或数据安全,但威胁到了用户要求的规范。结果如何?他们再次威胁要放弃VB。有一个有趣的轶事可以描述这样的人,但我怀疑他们会感激。我?我喜欢拿自己开玩笑。我是一个极客,我对极客的笑话是无情的!

最后,请注意,VB.NETVarptr再次允许我们使用CopyMemoryPinvoke直接复制结构对象数据。然而,尽管我们也可以获取类对象内存地址,但由于侵犯了受保护的内存,我们不能使用CopyMemory从中复制数据或向中复制数据。尽管一些有创意的开发人员已经成功地使用中间内存区域实现了这一点,但我们实际上可以绕过使用中间缓冲区,使用他们推荐的.NET方法,即StructToPtr和PtrToStructure编组方法,直接将数据复制到本地内存数据和类对象中。即便如此,由于这些不安全指针是OOPL规则的允许扩展,Microsoft应该考虑向VB.NET.添加不安全指针

最新更新