Use CopyMemory with DotNet??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • active

    Use CopyMemory with DotNet??

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

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

    and use it with

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



    where

    Dim pDevMode As New DEVMODE

    Dim aDevMode() As Byte


    and



    Private Structure DEVMODE

    <VBFixedArray(C CHDEVICENAME)> 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(C CHFORMNAME)> 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 dmDisplayFreque ncy As Integer

    End Structure



    Plus I do

    ReDim pDevMode.dmDevi ceName(CCHDEVIC ENAME)

    ReDim pDevMode.dmForm Name(CCHFORMNAM E)



    Thanks,

    CAl


  • Ken Tucker [MVP]

    #2
    Re: Use CopyMemory with DotNet??

    Hi,

    I have used copymemory with vb.net. I do prefer to use the
    Marshal class whenever possible. Is there a problem with your code?



    Ken
    -----------------
    " active" <active@REMOV Ea-znet.com> wrote in message
    news:uhg6gAP4DH A.1272@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Has anyone used CopyMemory with DotNet?
    > In this instance I define it as:
    > Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMem ory" (ByRef
    > hpvDest As DEVMODE, ByRef hpvSource As Byte, ByVal cbCopy As Integer)
    >
    > Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMem ory" (ByRef
    > hpvDest As Byte, ByRef hpvSource As DEVMODE, ByVal cbCopy As Integer)
    >
    > and use it with
    >
    > Call CopyMemory(pDev Mode, aDevMode(1), Len(pDevMode))
    >
    >
    >
    > where
    >
    > Dim pDevMode As New DEVMODE
    >
    > Dim aDevMode() As Byte
    >
    >
    > and
    >
    >
    >
    > Private Structure DEVMODE
    >
    > <VBFixedArray(C CHDEVICENAME)> 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(C CHFORMNAME)> 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 dmDisplayFreque ncy As Integer
    >
    > End Structure
    >
    >
    >
    > Plus I do
    >
    > ReDim pDevMode.dmDevi ceName(CCHDEVIC ENAME)
    >
    > ReDim pDevMode.dmForm Name(CCHFORMNAM E)
    >
    >
    >
    > Thanks,
    >
    > CAl
    >
    >[/color]


    Comment

    • active

      #3
      Re: Use CopyMemory with DotNet??

      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

      "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
      news:uQqJ5pP4DH A.2168@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Hi,
      >
      > I have used copymemory with vb.net. I do prefer to use[/color]
      the[color=blue]
      > Marshal class whenever possible. Is there a problem with your code?
      >
      >[/color]
      http://msdn.microsoft.com/library/de...mberstopic.asp[color=blue]
      >
      > Ken
      > -----------------
      > " active" <active@REMOV Ea-znet.com> wrote in message
      > news:uhg6gAP4DH A.1272@TK2MSFTN GP11.phx.gbl...[color=green]
      > > Has anyone used CopyMemory with DotNet?
      > > In this instance I define it as:
      > > Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMem ory"[/color][/color]
      (ByRef[color=blue][color=green]
      > > hpvDest As DEVMODE, ByRef hpvSource As Byte, ByVal cbCopy As Integer)
      > >
      > > Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMem ory"[/color][/color]
      (ByRef[color=blue][color=green]
      > > hpvDest As Byte, ByRef hpvSource As DEVMODE, ByVal cbCopy As Integer)
      > >
      > > and use it with
      > >
      > > Call CopyMemory(pDev Mode, aDevMode(1), Len(pDevMode))
      > >
      > >
      > >
      > > where
      > >
      > > Dim pDevMode As New DEVMODE
      > >
      > > Dim aDevMode() As Byte
      > >
      > >
      > > and
      > >
      > >
      > >
      > > Private Structure DEVMODE
      > >
      > > <VBFixedArray(C CHDEVICENAME)> 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(C CHFORMNAME)> 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 dmDisplayFreque ncy As Integer
      > >
      > > End Structure
      > >
      > >
      > >
      > > Plus I do
      > >
      > > ReDim pDevMode.dmDevi ceName(CCHDEVIC ENAME)
      > >
      > > ReDim pDevMode.dmForm Name(CCHFORMNAM E)
      > >
      > >
      > >
      > > Thanks,
      > >
      > > CAl
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • active

        #4
        Re: Use CopyMemory with DotNet??

        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
        [color=blue]
        >
        > 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?
        >[/color]


        Comment

        • Mattias Sjögren

          #5
          Re: Use CopyMemory with DotNet??

          [color=blue]
          >I still have a stumbling block - I don't know how to create an IntPtr for
          >the Copy, that points to the DEVMODE variable.[/color]

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

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

          [color=blue][color=green]
          >> Have you used DEVMODE?[/color][/color]

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



          Specifically, you should add <MarshalAs(Unma nagedType.ByVal Array,
          SizeConst:=TheA rraySize)> to the array members.



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org
          http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
          Please reply only to the newsgroup.

          Comment

          • active

            #6
            Re: Use CopyMemory with DotNet??

            Thanks, you've given me plenty to investigate,
            Cal
            "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
            news:OKC8nxS4DH A.1632@TK2MSFTN GP12.phx.gbl...[color=blue]
            >[color=green]
            > >I still have a stumbling block - I don't know how to create an IntPtr for
            > >the Copy, that points to the DEVMODE variable.[/color]
            >
            > You should probably look at the PtrToStructure and StructureToPtr
            > methods rather than Copy.
            >
            > Where do you get the source data (aDevMode) from?
            >
            >[color=green][color=darkred]
            > >> Have you used DEVMODE?[/color][/color]
            >
            > For a discussion on how to properly declare DEVMODE (in C#, but the
            > same applies to VB.NET) see
            >
            >[/color]
            http://blogs.gotdotnet.com/anathan/p...7-9e63033ec112[color=blue]
            >
            > Specifically, you should add <MarshalAs(Unma nagedType.ByVal Array,
            > SizeConst:=TheA rraySize)> to the array members.
            >
            >
            >
            > Mattias
            >
            > --
            > Mattias Sjögren [MVP] mattias @ mvps.org
            > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
            > Please reply only to the newsgroup.[/color]


            Comment

            Working...