473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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("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

Feb 27 '06 #1
3 2089
>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.
Feb 27 '06 #2
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" <ma********************@mvps.org> wrote in message
news:eq**************@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.

Mar 1 '06 #3
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.
"Delfim Da Costa" <de************@arcelor.com> escribió en el mensaje news:dt**********@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

Mar 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.