47
150
CDIntfEx.Lock Method
The Lockmethod can be used in multi-threading situations to avoid conflicts between multiple applications or multiple threads
requesting simultaneous access to the Converter products. The CDIntf library uses the registry to interact with the printer drivers.
This can cause conflicts when multiple applications use CDIntf to access the printer drivers.
Syntax
Function Lock(szLockName As String) As Long
Parameters
szLockName
[in] Lock identifier, this should be the document title as it appears in the print spooler when printing any
document.
Return Value
The return value is 0 if the function is successfulor a Windows specific error code if the function fails.
Remarks
The Lock method is only needed for applications using CDIntf to set the destination file name and options. The technical notes on
www.amyuni.com provide alternative ways for using the Document Converter products in multi-threading situations without the
need for CDIntf or the Lock functions.
When the Lock function is used, the output file name and options are set using the SetDocFileProps function and not the more
common DefaultFileName and FileNameOptions properties.
Job locking is needed in the following scenario:
1. Application or Thread A uses DefaultFileName or DefaultDirectory to set the output file name before starting to print
2. Application B also also uses DefaultFileName or DefaultDirectory to set the output file name
3. Application A starts to print, but prints to the file set by application B and not application A
To solve this issue, the printer driver should be locked for the few microseconds it takes for application A from the time it sets the
output file name, to the time it starts to print. As soon as the Application A starts to print, the lock can be released to allow
Application B to print in parallel.
Example
Private Sub PrintWord_Click()
' this method opens and prints a Word document using Automation
' it uses the Locking mechanism of CDIntf to avoid multi-threading conflicts
Dim fileName As String
' the lock name in the case of MS Word is the same as the file name
Dim LockName As String
LockName = "test.doc"
Dim wordApp As New Word.Application
Dim documents As Word.documents
On Error GoTo unlock_printer
' open a Word document in Read-only mode
Set documents = wordApp.documents
documents.Open "c:\wutemp\test.doc", False, True
' set the ActivePrinter to ours
wordApp.ActivePrinter = PrinterName
' lock printer before starting to print
cdi.Lock LockName
fileName = "c:\test" & index & ".pdf"
index = index + 1
cdi.SetDocFileProps LockName, 1 + 2, "", fileName