Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to overwrite VB.NET memory pointer

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to rewrite VB.NET memory pointer", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to rewrite VB.NET memory pointer" this article.

Recently, on a whim, I spent a lot of money to buy a Dopda smartphone, the system is Microsoft's Smartphone2003, because of the familiarity with the "plague" operating system, I soon got on the right track. After installing a lot of pictures and programs, I began to give full play to my program expertise and try to write a simple program of resource manager for my love machine.

Of course, the programming tool I choose is Visual Basic NET2003. To be honest, the features provided by Microsoft .NET Framework SDK are not flattering. Microsoft seems to be dragging all the .NET programmers on the bright road into the dark age of API, with pointers, handles, and addresses. The most annoying thing is that I didn't make the API statement of WinCE for the VB.NET program (I miss VB6's API browser). I soon got stuck in the code on how to start the application on my phone. I was suffering from Net's irresponsible practice of not providing Process objects for WinCE.

At the beginning of the declaration, I always glued the statements of ShellExecuteEx and CreateProcess, which are no longer familiar with Win32 in VB6, directly into the program, only changing the long of VB6 to the int32 of NET, resulting in always reporting errors. After looking up the Ntian information on the Internet, I found that the core API of Win CE was all in a coredll.dll file, which made me almost angry to death! Another point is that lpFile in the SHELLEXECUTEINFO structure points to string constants, because the character encoding problem can not be simply declared as String type in Win32 programming, but must be declared as Intptr, so the problem arises. To use the ShellExecuteEx function, you must use the pointer operation that Microsoft does not provide directly in VB.NET, so I have to rewrite a wrapper class for VB.NET memory pointers according to the information on the Internet. The detailed code and explanation are as follows (create a new module, paste the following code into it and use it):

Imports System.Runtime.InteropServices' citation and namespaces supported for underlying operations in the Net framework

Public Class clsDAMSMobileMarshal 'memory management class I wrote

# Region "memory-related API declaration"

REM the following is the memory-related mobile device API

Public Declare Function LocalAlloc Lib "coredll.dll" Alias "LocalAlloc"

(ByVal wFlags As Int32, _ ByVal wBytes As Int32) As IntPtr

Public Declare Function LocalFree Lib "coredll.dll" Alias "LocalFree"

(ByVal hMem As Int32) As Int32

Public Declare Function LocalLock Lib "coredll.dll" Alias "LocalLock"

(ByVal hMem As Int32) As Int32

Public Declare Function LocalReAlloc Lib "coredll.dll" Alias "LocalReAlloc"

(ByVal hMem As IntPtr, _ ByVal wBytes As Int32, ByVal wFlags As Int32) As IntPtr

# End Region

# Region "API constant declaration"

Public Const LMEM_FIXED = 0

Public Const LMEM_MOVEABLE = & H2

Public Const LMEM_ZEROINIT = & H40

Public Const LPTR = LMEM_FIXED Or LMEM_ZEROINIT

# End Region

Public Shared Function fnAllocHLocal (ByVal ni_i32Size As Int32) As IntPtr

'request local memory and return a pointer to the memory block

Return LocalAlloc (LPTR, ni_i32Size)

End Function

Public Shared Function fnFreeHLocal (ByRef ni_pLocal As IntPtr) As Int32

REM releases the specified memory block handle

Dim ti32FunctionReturnValue As Int32

If ni_pLocal.Equals (IntPtr.Zero) = False Then

Ti32FunctionReturnValue = (LocalFree (ni_pLocal.ToInt32))

If ti32FunctionReturnValue = 0 Then

Ni_pLocal = IntPtr.Zero

End If

End If

Return (ti32FunctionReturnValue)

End Function

Public Shared Function fnReAllocHLocal

(ByVal ni_pIn As IntPtr, ByVal ni_i32Size As Int32) As IntPtr

'Resize the specified memory block

Return LocalReAlloc (ni_pIn, ni_i32Size, LMEM_MOVEABLE)

End Function

Public Shared Function fnStringToHLocalUni (ByVal ni_strIn As String) As IntPtr

'copy the specified string to a memory block and return a pointer to that memory block, which must be released using the fnFreeHLocal function

Dim ti32StringBufLength As Int32

Dim tpTempA As IntPtr

If Not (ni_strIn Is Nothing) Then

If ni_strIn.Length = 0 Then

Return IntPtr.Zero

Else

Ti32StringBufLength = (ni_strIn.Length + 1) * 2 'including * a stop character

TpTempA = fnAllocHLocal (ti32StringBufLength)

If tpTempA.Equals (IntPtr.Zero) = False Then 'successfully applied for memory

Marshal.Copy (ni_strIn.ToCharArray, 0, tpTempA, ni_strIn.Length)

Return tpTempA

End If

End If

End If

End Function

End Class

The above is all the contents of the article "how to rewrite VB.NET memory pointers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report