CreateFile problem in Win7 with a shared printer
-
Saturday, March 10, 2012 9:18 PMI have a VB2005 process in WinXP that prints to a local usb printer by using ' createfile' and 'safefilehandles'. When I run this program in Win7, or if I try to use a usb printer on a Win7 machine the CreateFile process fails, by not getting a file handle. The printer is usually on the local machine and it is shared and the CreateFile function uses the syntax "\\server\PrinterShareName". I have cannot find any method that sheds light on the problem. Any ideas? Thanks...
All Replies
-
Monday, March 12, 2012 3:08 AMModerator
Hi RobFischer,
Could you please show some code that use the local printer? And did you use try{}catch{} or GetLastError to check if your code throws the errors in the Windows 7 system? Then did you check if your code needes the administrator privileges, since Windows Vista and 7 is a UAC system, please try to run your code as administrator, and then try to use the manifest to change the UAC setting for your application: http://msdn.microsoft.com/en-us/library/bb756929.aspx
Other resource I found, http://support.microsoft.com/kb/154078/EN-US this KB introduce one way to send data to printer visa Win32 API. And .Net just provides the class to handle the Printer, such as: PrintDialog Class
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
Monday, March 12, 2012 8:17 PM
Here is the code for my test project, a Form1 and Module1 project. This project is able to load a file successfully ("c:\xfer\testfile.txt") and is also able to get a handle for a printer attached to a WinXP machine ("\\PIO-WS3-FH\LaserPrinter"). It cannot get a handle for a usb printer attached to the local Win7 pc and shared as "\\RWF-HP\\ZebraHP".
The error is that the handle.Isvalid is false
I must send raw code directly to a printer without using the printer drivers, so the printer executes its internal command structure. This is a Zebra label printer and I want to send CPCL language commands. So the printDialog class is not useful in this instance.
Thanks... I will try your suggestions concerning admin privileges and manifests.
-------------------------------------------------------------
Form1:
Imports Microsoft.Win32.SafeHandles
'
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s As SafeFileHandle
' s = New UnmanagedFileLoader("c:\xfer\testfile.txt").Handle
s = New UnmanagedFileLoader("\\RWF-HP\\ZebraHP").Handle
s = New UnmanagedFileLoader("\\RWF-HP\ZDesigner RW 420").Handle '\\\\.\\PhysicalDrive0"
s = New UnmanagedFileLoader("\\RWF-COMPAQ\HP Color Laserjet 1600").Handle
s = New UnmanagedFileLoader("\\PIO-WS3-FH\LaserPrinter").Handle 'OK!!!!!! it is WINXP !!!!!
End Sub
End Class
----------------------------------------------------------------------------
Module1:
Imports System
Imports Microsoft.Win32.SafeHandles
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Module SafeHandlesExample
Sub Main()
Try
Dim loader As New UnmanagedFileLoader("c:\xfer\testfile.txt")
Catch e As Exception
Console.WriteLine(e)
End Try
Console.ReadLine()
End Sub
End Module
Class UnmanagedFileLoader
Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80
Public Const INVALID_HANDLE_VALUE As Short = -1
Public Const GENERIC_READ As Long = &H80000000
Public Const GENERIC_WRITE As UInteger = &H40000000
Public Const CREATE_NEW As UInteger = 1
Public Const CREATE_ALWAYS As UInteger = 2
Public Const OPEN_EXISTING As UInteger = 3
Public Const RW As UInteger = &H1D000000
' Use interop to call the CreateFile function.
' For more information about CreateFile,
' see the unmanaged MSDN reference library.
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As System.UInt32, ByVal dwShareMode As System.UInt32, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As System.UInt32, ByVal dwFlagsAndAttributes As System.UInt32, ByVal hTemplateFile As IntPtr) As SafeFileHandle
End Function
Private handleValue As SafeFileHandle = Nothing
Public Sub New(ByVal Path As String)
Load(Path)
End Sub
Public Sub Load(ByVal Path As String)
If Path Is Nothing AndAlso Path.Length = 0 Then
Throw New ArgumentNullException("Path")
End If
' Try to open the file.
' handleValue = CreateFile(Path, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
handleValue = CreateFile(Path, RW, 0, Nothing, OPEN_EXISTING, 0, Nothing) 'doc says that last must be NULL, nothing in vb
' If the handle is invalid,
' get the last Win32 error
' and throw a Win32Exception.
If handleValue.IsInvalid Then
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error())
MsgBox("No Good")
End If
End Sub
Public ReadOnly Property Handle() As SafeFileHandle
Get
' If the handle is valid,
' return it.
If Not handleValue.IsInvalid Then
Return handleValue
Else
Return Nothing
End If
End Get
End Property
End Class
'End Module -
Monday, March 12, 2012 9:20 PMThis is a VB 2005 project I am working with. I am looking at the application manifest methods. Is this manifest process as complicated as it seems?
-
Tuesday, March 13, 2012 1:16 AMI have turned off UAC and the CreateFile problem remains. It appears to be related to a usb device attached to Win7, and adding a manifest to change UAC is unnecessary. Is there anything else to do? Thanks for your help thusfar.
-
Tuesday, March 13, 2012 3:05 AMModerator
Hmm, try to read this thread that has similar issue with you: http://social.msdn.microsoft.com/Forums/en-us/wdk/thread/d6f6d0a8-9c92-43e1-b9f9-82ec9cbc1a2f I know CreateFile can used to get the printer, I/O handle, but I still recommend you to use the .Net solution to control the printer, or via the wrapped Win32 API to connect the printer, such from http://support.microsoft.com/kb/154078/EN-US
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
Tuesday, March 13, 2012 8:02 PMI have tried the methods in those other threads and each takes me to a dead end. The core issue is this: CreateFile and SafeFileHandles are unable to open a valid handle on a usb device attached on a Win7 machine with all security and UAC disabled, and the device is shared. The same exact code works in WinXP, and on Win7 it works for a disk file and it works for a usb device attached to a remote WinXP machine. This sounds quite fundamental and perhaps the CreateFile just will not work in this case. Is there a proper place to request Microsoft look into this as a bug report? My other options for solving the problem (such as using a report generator and the windows printer drivers) are much less efficient . I greatly appreciate your time helping me with this, Bob.
-
Monday, March 19, 2012 9:11 AMModeratorTry Connect site for Microsoft Product feedback. And if you have the detailed report for this issue, you could share it on the site.
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
-
Thursday, March 22, 2012 8:33 PMThanks for your help Bob. I will pursue it on that site.
-
Wednesday, December 19, 2012 7:47 PMHey Rob did you ever get any useful information on this problem? I am having the exact same problem. I tried clicking over to the Connect site and searching but I couldn't find it.
-
Friday, March 22, 2013 10:30 AMStrange as it may seem, changing the dwCreationDisposition parameter of CreateFile from OPEN_EXISTING, which is suggested by the documentation, to OPEN_ALWAYS solved the problem for me.
- Edited by Anton Shepelev Friday, March 22, 2013 10:31 AM


