OVH Cloud OVH Cloud

ptr to string ?

2 réponses
Avatar
Delfim Da Costa
Bonjour,

J'ai un Problème d'utilisation de l' API GetPrinterData, en fait j'ai un
problème pour récupérer dans un string un buffer dont j'ai le pointeur et la
taille.
J'utilise la fonction "Marshal.PtrToStringAuto" qui ne me retourne
malheureusement rien.

Ci-dessous une partie de mon code,

Merci,

Delfim.







<DllImport("winspool.Drv", CharSet:=CharSet.Ansi,
EntryPoint:="GetPrinterDataA", _
SetLastError:=True, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function GetPrinterData( _
ByVal hPrinter As IntPtr, _
<MarshalAs(UnmanagedType.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(PrinterName)
ret = GetPrinterData(hPrinter, ValueName, pType, IntPtr.Zero, 0,
pcbNeeded)
If pcbNeeded <= 0 Then
Throw (New System.Exception("Unable to allocate memory"))
End If

pData = Marshal.AllocHGlobal(pcbNeeded)
ret = GetPrinterData(hPrinter, ValueName, pType, pData, pcbNeeded, cBuf)
If ret <> 0 Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If

Select Case pType
Case REG_DWORD
oData = pData.ToInt32

Case REG_SZ
oData = Marshal.PtrToStringAuto(pData)

Case Else
oData = pData
End Select

ClosePrinter(hPrinter)

Return oData
End Function

2 réponses

Avatar
TroXsA
Bonjour,

Avez vous fais les testes en mode debug ? souvent on trouve le probleme !


Cordialement,

Site : http://www.troxsa.info
Mail : http://cerbermail.com/?rIXgaw9Xsl




"Delfim Da Costa" a écrit dans le message de
news: dtudo7$arr$
Bonjour,

J'ai un Problème d'utilisation de l' API GetPrinterData, en fait j'ai un
problème pour récupérer dans un string un buffer dont j'ai le pointeur et
la taille.
J'utilise la fonction "Marshal.PtrToStringAuto" qui ne me retourne
malheureusement rien.

Ci-dessous une partie de mon code,

Merci,

Delfim.







<DllImport("winspool.Drv", CharSet:=CharSet.Ansi,
EntryPoint:="GetPrinterDataA", _
SetLastError:=True, ExactSpelling:=True, _
CallingConvention:ÊllingConvention.StdCall)> _
Private Shared Function GetPrinterData( _
ByVal hPrinter As IntPtr, _
<MarshalAs(UnmanagedType.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(PrinterName)
ret = GetPrinterData(hPrinter, ValueName, pType, IntPtr.Zero, 0,
pcbNeeded)
If pcbNeeded <= 0 Then
Throw (New System.Exception("Unable to allocate memory"))
End If

pData = Marshal.AllocHGlobal(pcbNeeded)
ret = GetPrinterData(hPrinter, ValueName, pType, pData, pcbNeeded,
cBuf)
If ret <> 0 Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If

Select Case pType
Case REG_DWORD
oData = pData.ToInt32

Case REG_SZ
oData = Marshal.PtrToStringAuto(pData)

Case Else
oData = pData
End Select

ClosePrinter(hPrinter)

Return oData
End Function




Avatar
Delfim Da Costa
Oui et je n'ai pas trouvé de solution.

Delfim.


"TroXsA" wrote in message
news:du1684$o4k$

Bonjour,

Avez vous fais les testes en mode debug ? souvent on trouve le probleme !


Cordialement,

Site : http://www.troxsa.info
Mail : http://cerbermail.com/?rIXgaw9Xsl




"Delfim Da Costa" a écrit dans le message de
news: dtudo7$arr$
Bonjour,

J'ai un Problème d'utilisation de l' API GetPrinterData, en fait j'ai un
problème pour récupérer dans un string un buffer dont j'ai le pointeur et
la taille.
J'utilise la fonction "Marshal.PtrToStringAuto" qui ne me retourne
malheureusement rien.

Ci-dessous une partie de mon code,

Merci,

Delfim.







<DllImport("winspool.Drv", CharSet:=CharSet.Ansi,
EntryPoint:="GetPrinterDataA", _
SetLastError:=True, ExactSpelling:=True, _
CallingConvention:ÊllingConvention.StdCall)> _
Private Shared Function GetPrinterData( _
ByVal hPrinter As IntPtr, _
<MarshalAs(UnmanagedType.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(PrinterName)
ret = GetPrinterData(hPrinter, ValueName, pType, IntPtr.Zero, 0,
pcbNeeded)
If pcbNeeded <= 0 Then
Throw (New System.Exception("Unable to allocate memory"))
End If

pData = Marshal.AllocHGlobal(pcbNeeded)
ret = GetPrinterData(hPrinter, ValueName, pType, pData, pcbNeeded,
cBuf)
If ret <> 0 Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If

Select Case pType
Case REG_DWORD
oData = pData.ToInt32

Case REG_SZ
oData = Marshal.PtrToStringAuto(pData)

Case Else
oData = pData
End Select

ClosePrinter(hPrinter)

Return oData
End Function