ptr to string ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Delfim Da Costa

    #1

    ptr to string ?



    Hi,

    I have some problems to use GetPrinterData Win32 API. in fact my problem is
    : how can get the in a string a buffer known by it's ptr ?
    Can anybody help me ?

    Thanks,

    Delfim.

    this a part of my code :



    <DllImport("win spool.Drv", CharSet:=CharSe t.Ansi,
    EntryPoint:="Ge tPrinterDataA", _
    SetLastError:=T rue, ExactSpelling:= True, _
    CallingConventi on:=CallingConv ention.StdCall) > _
    Private Shared Function GetPrinterData( _
    ByVal hPrinter As IntPtr, _
    <MarshalAs(Unma nagedType.LPStr )> ByVal pValueName As String, _
    ByRef pType As Integer, _
    ByRef pData As IntPtr, _
    ByVal nSize As Integer, _
    ByRef pcbNeeded As Integer) As Integer
    End Function

    Public Function GetPrinterData( ByVal PrinterName As String, ByVal ValueName
    As String) As Object
    Dim hPrinter As IntPtr
    Dim pData As IntPtr = IntPtr.Zero
    Dim cBuf As Integer
    Dim pcbNeeded As Integer
    Dim pType As Integer
    Dim ret As Integer
    Dim oData As Object

    hPrinter = OpenPrinter(Pri nterName)
    ret = GetPrinterData( hPrinter, ValueName, pType, IntPtr.Zero, 0,
    pcbNeeded)
    If pcbNeeded <= 0 Then
    Throw (New System.Exceptio n("Unable to allocate memory"))
    End If

    pData = Marshal.AllocHG lobal(pcbNeeded )
    ret = GetPrinterData( hPrinter, ValueName, pType, pData, pcbNeeded, cBuf)
    If ret <> 0 Then
    Throw New Win32Exception( Marshal.GetLast Win32Error())
    End If

    Select Case pType
    Case REG_DWORD
    oData = pData.ToInt32

    Case REG_SZ
    oData = Marshal.PtrToSt ringAuto(pData)

    Case Else
    oData = pData
    End Select

    ClosePrinter(hP rinter)

    Return oData
    End Function



  • Mattias Sjögren

    #2
    Re: ptr to string ?

    >I have some problems to use GetPrinterData Win32 API. in fact my problem is[color=blue]
    >: how can get the in a string a buffer known by it's ptr ?
    >Can anybody help me ?[/color]

    Since you're explicitly calling the ANSI version of the API I guess
    you should use Marshal.PtrToSt ringAnsi (not Auto) to read the string.
    And don't forget to free the memory allocated with AllocHGlobal.


    Mattias

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

    Comment

    • Delfim Da Costa

      #3
      Re: ptr to string ?

      I have tried this but it's not working.
      I have made some debug session and maybe there is a problem in second call
      to GetPrinterData, but i don't find what is it


      Delfim.

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:eqPoUO8OGH A.1216@TK2MSFTN GP14.phx.gbl...[color=blue][color=green]
      > >I have some problems to use GetPrinterData Win32 API. in fact my problem
      > >is
      >>: how can get the in a string a buffer known by it's ptr ?
      >>Can anybody help me ?[/color]
      >
      > Since you're explicitly calling the ANSI version of the API I guess
      > you should use Marshal.PtrToSt ringAnsi (not Auto) to read the string.
      > And don't forget to free the memory allocated with AllocHGlobal.
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [C# MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      > Please reply only to the newsgroup.[/color]


      Comment

      • José Manuel Agüero

        #4
        Re: ptr to string ?

        Hello Delfim,

        Why don't you use a byte array as a buffer for the function call?:
        <DllImport("win spool.Drv", CharSet:=CharSe t.Ansi, _
        EntryPoint:="Ge tPrinterDataA", _
        SetLastError:=T rue, ExactSpelling:= True, _
        CallingConventi on:=CallingConv ention.StdCall) > _
        Private Shared Function GetPrinterData( _
        ByVal hPrinter As IntPtr, _
        ByVal pValueName As String, _
        ByRef pType As Integer, _
        [Out] ByVal pData() As Byte, _
        ByVal nSize As Integer, _
        ByRef pcbNeeded As Integer) As Integer
        End Function

        Now you have the bytes in an array, only need to use the bitconverter class to transform into a string or a numeric type.

        Regards.


        "Delfim Da Costa" <delfim.dacosta @arcelor.com> escribió en el mensaje news:dtufsc$c0p $1@s1.news.olea ne.net...
        |
        |
        | Hi,
        |
        | I have some problems to use GetPrinterData Win32 API. in fact my problem is
        | : how can get the in a string a buffer known by it's ptr ?
        | Can anybody help me ?
        |
        | Thanks,
        |
        | Delfim.
        |
        | this a part of my code :
        |
        |
        |
        | <DllImport("win spool.Drv", CharSet:=CharSe t.Ansi,
        | EntryPoint:="Ge tPrinterDataA", _
        | SetLastError:=T rue, ExactSpelling:= True, _
        | CallingConventi on:=CallingConv ention.StdCall) > _
        | Private Shared Function GetPrinterData( _
        | ByVal hPrinter As IntPtr, _
        | <MarshalAs(Unma nagedType.LPStr )> ByVal pValueName As String, _
        | ByRef pType As Integer, _
        | ByRef pData As IntPtr, _
        | ByVal nSize As Integer, _
        | ByRef pcbNeeded As Integer) As Integer
        | End Function
        |
        | Public Function GetPrinterData( ByVal PrinterName As String, ByVal ValueName
        | As String) As Object
        | Dim hPrinter As IntPtr
        | Dim pData As IntPtr = IntPtr.Zero
        | Dim cBuf As Integer
        | Dim pcbNeeded As Integer
        | Dim pType As Integer
        | Dim ret As Integer
        | Dim oData As Object
        |
        | hPrinter = OpenPrinter(Pri nterName)
        | ret = GetPrinterData( hPrinter, ValueName, pType, IntPtr.Zero, 0,
        | pcbNeeded)
        | If pcbNeeded <= 0 Then
        | Throw (New System.Exceptio n("Unable to allocate memory"))
        | End If
        |
        | pData = Marshal.AllocHG lobal(pcbNeeded )
        | ret = GetPrinterData( hPrinter, ValueName, pType, pData, pcbNeeded, cBuf)
        | If ret <> 0 Then
        | Throw New Win32Exception( Marshal.GetLast Win32Error())
        | End If
        |
        | Select Case pType
        | Case REG_DWORD
        | oData = pData.ToInt32
        |
        | Case REG_SZ
        | oData = Marshal.PtrToSt ringAuto(pData)
        |
        | Case Else
        | oData = pData
        | End Select
        |
        | ClosePrinter(hP rinter)
        |
        | Return oData
        | End Function

        Comment

        Working...