|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ptr to string ?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("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 >I have some problems to use GetPrinterData Win32 API. in fact my problem is Since you're explicitly calling the ANSI version of the API I guess>: how can get the in a string a buffer known by it's ptr ? >Can anybody help me ? you should use Marshal.PtrToStringAnsi (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. 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. Show quoteHide quote "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message news:eqPoUO8OGHA.1216@TK2MSFTNGP14.phx.gbl... > >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 ? > > Since you're explicitly calling the ANSI version of the API I guess > you should use Marshal.PtrToStringAnsi (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. Hello Delfim,
Why don't you use a byte array as a buffer for the function call?: <DllImport("winspool.Drv", CharSet:=CharSet.Ansi, _ EntryPoint:="GetPrinterDataA", _ SetLastError:=True, ExactSpelling:=True, _ CallingConvention:=CallingConvention.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. Show quoteHide quote "Delfim Da Costa" <delfim.daco***@arcelor.com> escribió en el mensaje news:dtufsc$c0p$1@s1.news.oleane.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("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
Did we lose the RadioCheck property on menuitems
DateTimePicker + DataBinding + Null-Value: THE solution? Is there a simple way to email? What is Instrumentation? Webapplications quits without matter in ASP.NET 2.0 Help please Creating web page of links and content Ado.net to excel ? How to remove Volume Name on removable drive? Listbox controls Perfomance test |
|||||||||||||||||||||||