Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String-Arrays and CopyMemory

Posted on 2002-05-23
8
Medium Priority
?
856 Views
Last Modified: 2010-08-05
Hi.
Please have look at the following snippet.
It inserts a value into an array of integers:

private declare sub copymemory ....

Dim intData() as Integer
Redim intData(4)
intData(0)=1:intData(1)=2:intData(2)=3:intData(3)=4:intData(4)=5
' intData is 1,2,3,4,5


' The important thing:
Redim Preserve intData(5)
CopyMemory intData(3), intData(2), Clng(3*2)
intData(2) = 99

' intData is now 1,2,99,3,4,5

Now the Question:
How can i do the same trick with an array of strings(BSTRING) of fixed or variable length.

Regards, holli
0
Comment
Question by:holli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 17

Expert Comment

by:inthedark
ID: 7030124
If noboby comes up with an answer you could do it this way:

Create your string array and also create a pointer to the string array.  Whenever you refer to the string array you always go through the pointer array.  When you add a new element always add it to the last + 1 element in the string array but move the pointer array in the way you used above.

e.g.

a$(0)="Ark"
P(0)=0
a$(1)="Cath"
P(1)=1

Now insert "Bert"

a$(2)="Bert"
P(1) is moved to P(2)
P(1) = 1
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7030125
So to loop through you array:

For c = 0 to Ubound(p)
  msgbox A$(p(c))
Next c
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7030126
Woops....
a$(2)="Bert"
P(1) is moved to P(2)
P(1) = 2 ' got this pointer wrong

 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Author Comment

by:holli
ID: 7032735
thanks so far.

but this is a bit to less for 200 points. but for the excellent and modern programmer you are, it should be not problem for you to clarify that a bit? how about a working sample along with the neccessary declarations?

holli
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7033433
holli, I don't know that copymemory works top down, if it takes a copy of the source memory first it would be better to write the shift in assembler.

Using machine code you can make some space in the middle of an array with just 2 machine code instructions.  So if you app requires speed copymemory may not be the way to go.  What are you tring to acheive?
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7033469
Move memory would be slow, copy memory would not work.

As I thought see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/kmarch/hh/kmarch/k109_0w8i.asp

And:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/kmarch/hh/kmarch/k109_0zzm.asp

So the solution is to poke some machine code into a string and the execute it.

From memory the machine code you would need would look like this:


PUSH SI
Move SI, PosOfLastByte
PUSH DI
Move DI, SI
ADD DI, WordLength
Move CX, BytesToCopy
MOVD
REPZ
POP DI
POP SI

MOVD would move the first word from SI to DI, then  decrement SI, DI and CX

REPZ would repeate the last instruction until CX was zero.

You would also have to encapsulate the code in a disable/enable interrupts instructions.  
0
 
LVL 17

Accepted Solution

by:
inthedark earned 800 total points
ID: 7033494
But using MoveMemory:

Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)


Private Sub Command4_Click()

ReDim Pointers(1000) As Long ' start big for speed
ReDim Strings(1000) As String ' start big for speed
Dim Elements As Long
Dim c As Long

Elements = 0 ' to start there are no elements

' put first element in pos 0
InsertElement "Able", 0, Strings(), Pointers(), Elements

' put next element in pos 1
InsertElement "Charlie", 1, Strings(), Pointers(), Elements

' put next element in pos 1
InsertElement "Bert", 1, Strings(), Pointers(), Elements


' Now recall elements
Dim m$
m$ = ""
For c = 0 To Elements - 1
    m$ = m$ + Strings(Pointers(c)) + vbCrLf
Next c

MsgBox m$

End Sub

Sub InsertElement(StringData As String, InsertPoint As Long, Strings() _
    As String, Pointers() As Long, Elements As Long)

Dim BytesToCopy As Long
Dim WordLength As Long

If Elements > UBound(Pointers) Then
    ReDim Preserve Pointers(Elements + 1000)
    ReDim Preserve Strings(Elements + 1000)
End If

If Elements > 0 And InsertPoint < Elements Then
    WordLength = 4
    BytesToCopy = (Elements - InsertPoint) * WordLength
    Call MoveMemory(Pointers(InsertPoint + 1), Pointers(InsertPoint), BytesToCopy)
End If

Strings(Elements) = StringData ' put this on the end
Pointers(InsertPoint) = Elements

Elements = Elements + 1

End Sub

0
 
LVL 6

Author Comment

by:holli
ID: 7046581
inthedark, please draw your attention to the related Question at

http://experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20306514.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question