Use CopyMemory with DotNet??

A

active

Has anyone used CopyMemory with DotNet?
In this instance I define it as:
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (ByRef
hpvDest As DEVMODE, ByRef hpvSource As Byte, ByVal cbCopy As Integer)

Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (ByRef
hpvDest As Byte, ByRef hpvSource As DEVMODE, ByVal cbCopy As Integer)

and use it with

Call CopyMemory(pDevMode, aDevMode(1), Len(pDevMode))



where

Dim pDevMode As New DEVMODE

Dim aDevMode() As Byte


and



Private Structure DEVMODE

<VBFixedArray(CCHDEVICENAME)> Dim dmDeviceName() As Byte

Dim dmSpecVersion As Short

Dim dmDriverVersion As Short

Dim dmSize As Short

Dim dmDriverExtra As Short

Dim dmFields As Integer

Dim dmOrientation As Short

Dim dmPaperSize As Short

Dim dmPaperLength As Short

Dim dmPaperWidth As Short

Dim dmScale As Short

Dim dmCopies As Short

Dim dmDefaultSource As Short

Dim dmPrintQuality As Short

Dim dmColor As Short

Dim dmDuplex As Short

Dim dmYResolution As Short

Dim dmTTOption As Short

Dim dmCollate As Short

<VBFixedArray(CCHFORMNAME)> Dim dmFormName() As Byte

Dim dmUnusedPadding As Short

Dim dmBitsPerPel As Short

Dim dmPelsWidth As Integer

Dim dmPelsHeight As Integer

Dim dmDisplayFlags As Integer

Dim dmDisplayFrequency As Integer

End Structure



Plus I do

ReDim pDevMode.dmDeviceName(CCHDEVICENAME)

ReDim pDevMode.dmFormName(CCHFORMNAME)



Thanks,

CAl
 
A

active

I get a "Object reference not set to an instance of an object." at the
CopyMemory

If my Declares look OK maybe it's the structure that is giving me the
problem. It was generated by the conversion wizard.

Have you used DEVMODE?

I've read some about Marshaling and tried some things but probably need more
samples.

I'll look at the site you included and see if that helps.

Thanks for replying,
Cal
 
A

active

Your reference helpped a lot. I looked at Marshal in the past with regard
to Declare parameters and did not notice the Copy method.

I still have a stumbling block - I don't know how to create an IntPtr for
the Copy, that points to the DEVMODE variable.

Got a suggestion?

Cal
 
M

Mattias Sjögren

I still have a stumbling block - I don't know how to create an IntPtr for
the Copy, that points to the DEVMODE variable.

You should probably look at the PtrToStructure and StructureToPtr
methods rather than Copy.

Where do you get the source data (aDevMode) from?


For a discussion on how to properly declare DEVMODE (in C#, but the
same applies to VB.NET) see

http://blogs.gotdotnet.com/anathan/permalink.aspx/ef29eac5-a456-43b7-a427-9e63033ec112

Specifically, you should add <MarshalAs(UnmanagedType.ByValArray,
SizeConst:=TheArraySize)> to the array members.



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