use copymemory api in vb.net

R

RaviAmbani

I am facing a problem in copymemory api in vb.net .
the problem is that i have used copymemory api extensively in my
project originally written in vb6.0 ,
now when i'm migrating my project to vb.net "as any"is not supported
over here.
so my code having copymemory is not upgraded.
i can't overload copymemory api as i mainly use copymemory api to copy
user defined structures to byte array and vice versa and i have lot
many of these structures.
so please provide me with some way out.
 
T

Tom Shelton

I am facing a problem in copymemory api in vb.net .
the problem is that i have used copymemory api extensively in my
project originally written in vb6.0 ,
now when i'm migrating my project to vb.net "as any"is not supported
over here.
so my code having copymemory is not upgraded.
i can't overload copymemory api as i mainly use copymemory api to copy
user defined structures to byte array and vice versa and i have lot
many of these structures.
so please provide me with some way out.

This is sort of a general answer - since I'm not sure exactly what
circumstances you were using CopyMemory in the past - but in general
there are ways of accomplishing this sort of thing with in the
framework, without resorting to using CopyMemory. Maybe, if you were to
post a sinipit of VB.CLASSIC code that you are trying to convert
(including relevant structure definitions), we might be able to come up
with a more specific solution.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (RaviAmbani) scripsit:
I am facing a problem in copymemory api in vb.net .
the problem is that i have used copymemory api extensively in my
project originally written in vb6.0 ,
now when i'm migrating my project to vb.net "as any"is not supported
over here.
so my code having copymemory is not upgraded.
i can't overload copymemory api as i mainly use copymemory api to copy
user defined structures to byte array and vice versa and i have lot
many of these structures.

It depends on the situation. Sometimes you can replace the calls with
calls to 'System.Runtime.InteropServices.Marshal.*'. It's easier to
help if you post some code.
 
R

RaviAmbani

I m sending you some sample code that i want to convert from VB to
VB.NET...
that uses CopyMemory API.
------------------------------
Private Sub Command1_Click()
Dim lretval As Long
Dim udtdate As SYSTEMTIME
Dim udtdate1 As SYSTEMTIME
Dim btarray() As Byte
Dim sfinaldate As String * 20
Dim sfinaldate1 As String * 20
udtdate.wMonth = 12
udtdate.wDay = 12
udtdate.wYear = 2003
lretval = GetDateFormatA(1033, 0, udtdate, Replace("mm/dd/yyyy", "m",
"M"), sfinaldate, 20)


ReDim btarray(0)
Call CopyMemory(btarray(0), udtdate, Len(udtdate))
Call CopyMemory(udtdate1, btarray(0), Len(udtdate1))
MsgBox "d", vbInformation
End Sub
 
A

Armin Zingler

RaviAmbani said:
I m sending you some sample code that i want to convert from VB to
VB.NET...
that uses CopyMemory API.
------------------------------
Private Sub Command1_Click()
Dim lretval As Long
Dim udtdate As SYSTEMTIME
Dim udtdate1 As SYSTEMTIME
Dim btarray() As Byte
Dim sfinaldate As String * 20
Dim sfinaldate1 As String * 20
udtdate.wMonth = 12
udtdate.wDay = 12
udtdate.wYear = 2003
lretval = GetDateFormatA(1033, 0, udtdate, Replace("mm/dd/yyyy",
"m", "M"), sfinaldate, 20)


ReDim btarray(0)
Call CopyMemory(btarray(0), udtdate, Len(udtdate))
Call CopyMemory(udtdate1, btarray(0), Len(udtdate1))
MsgBox "d", vbInformation
End Sub

Untested:
Have a look at the ToString function of a DateTime object. The function is
overloaded and you can pass an object implementing System.IFormatProvider.
Create a new CultureInfo object using the destination culture you want the
string to be formatted for. It's DateTimeFormat property returns a
System.Globalization.DateTimeFormatInfo object. As it implements
IFormatProvider, you can pass it to the ToString function of a DateTime
object.
 
M

Mattias Sjögren

Ravi,
I m sending you some sample code that i want to convert from VB to
VB.NET...

Better rewrite it, the code is pretty bad.

lretval = GetDateFormatA(1033, 0, udtdate, Replace("mm/dd/yyyy", "m",
"M"), sfinaldate, 20)

I don't see any reason to call GetDateFormat in VB.NET. As Armin said,
the DateTime type can do this formatting for you.

And what's with the Replace call? Why not type "MM/dd/yyyy" directly?

ReDim btarray(0)
Call CopyMemory(btarray(0), udtdate, Len(udtdate))
Call CopyMemory(udtdate1, btarray(0), Len(udtdate1))

I'm surprised that this code doesn't crash. What you're doing is to
copy a 16-byte SYSTEMTIME structure into a 1-byte array, an ugly
buffer overflow bug. Plus, the CopyMemory calls seem unnecessary,
since you just as well could do

udtdate1 = udtdate

MsgBox "d", vbInformation

Now that's useful. "d"!



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top