CreateDC Fails when not fed 'properly' 
Author Message
 CreateDC Fails when not fed 'properly'

I am exploring GDI with the eventual goal of printing, and I have run in to
an 'odd' problem. Basically the CreateDC call in failing to return anything
other than 0, unless I feed it with the DeviceName and DriverName from the
default printer.
For example

CreateDC(Printer.DriverName, Printer.DeviceName, vbNullString, vbNull)

returns a valid device context, but

CreateDC(Printers(0).DriverName, Printers(0).DeviceName, vbNullString,
vbNull)

always returns 0

further
DevName = Printer.DeviceName
DrvName = Printer.DriverName
CreateDC(DrvName, DevName, vbNullString, vbNull)

also always returns 0

Ideas?

Dale



Sat, 01 Jan 2005 23:09:30 GMT  
 CreateDC Fails when not fed 'properly'
I seem to have sorted something out, that seems to work, basically by
changing the declaration of CreateDC to

Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal
lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As Long,
lpInitData As Any) As Long
And passing 0s to lpszOutput and Clng(0) by Val to lpInitData. Anyone know
why this is required?

http://216.26.168.92/vbapi/ref/c/createdc.html

Dale


Quote:
> I am exploring GDI with the eventual goal of printing, and I have run in
to
> an 'odd' problem. Basically the CreateDC call in failing to return
anything
> other than 0, unless I feed it with the DeviceName and DriverName from the
> default printer.
> For example

> CreateDC(Printer.DriverName, Printer.DeviceName, vbNullString, vbNull)

> returns a valid device context, but

> CreateDC(Printers(0).DriverName, Printers(0).DeviceName, vbNullString,
> vbNull)

> always returns 0

> further
> DevName = Printer.DeviceName
> DrvName = Printer.DriverName
> CreateDC(DrvName, DevName, vbNullString, vbNull)

> also always returns 0

> Ideas?

> Dale



Sat, 01 Jan 2005 23:34:17 GMT  
 CreateDC Fails when not fed 'properly'
The problem is vbNull is not what you think it is (it's a Variant that
contains a special value indicating null, or something like that).
vbNullString was the right choice for the unused third parameter though.

I recommend the following declarations:

Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal
lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As
String, ByRef lpInitData As DEVMODE) As Long

Dim hDC As Long
Dim tDevMode As DEVMODE
hDC = CreateDC("DriverName", "DeviceName", vbNullString, tDevMode)

Use this if you don't want to use the last parameter:

Declare Function CreateDCNull Lib "gdi32.dll" Alias "CreateDCA" (ByVal
lpszDriver As String, ByVal lpszDevice As String, Optional ByVal lpszOutput
As String = vbNullString, Optional ByVal lpInitData As Long = 0&) As Long

Dim hDC As Long
hDC = CreateDCNull("DriverName", "DeviceName")


Quote:
> I seem to have sorted something out, that seems to work, basically by
> changing the declaration of CreateDC to

> Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal
> lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As
Long,
> lpInitData As Any) As Long
> And passing 0s to lpszOutput and Clng(0) by Val to lpInitData. Anyone know
> why this is required?

> http://216.26.168.92/vbapi/ref/c/createdc.html

> Dale



> > I am exploring GDI with the eventual goal of printing, and I have run in
> to
> > an 'odd' problem. Basically the CreateDC call in failing to return
> anything
> > other than 0, unless I feed it with the DeviceName and DriverName from
the
> > default printer.
> > For example

> > CreateDC(Printer.DriverName, Printer.DeviceName, vbNullString, vbNull)

> > returns a valid device context, but

> > CreateDC(Printers(0).DriverName, Printers(0).DeviceName, vbNullString,
> > vbNull)

> > always returns 0

> > further
> > DevName = Printer.DeviceName
> > DrvName = Printer.DriverName
> > CreateDC(DrvName, DevName, vbNullString, vbNull)

> > also always returns 0

> > Ideas?

> > Dale



Sun, 02 Jan 2005 01:57:32 GMT  
 CreateDC Fails when not fed 'properly'

Quote:
> The problem is vbNull is not what you think it is (it's a Variant that
> contains a special value indicating null, or something like that).
> vbNullString was the right choice for the unused third parameter though.

I thought it might not be, but then why did it work the first way?

Dale



Sun, 02 Jan 2005 02:26:33 GMT  
 CreateDC Fails when not fed 'properly'
Luck?  The docs say it's a constant for the value 1 and is used with the
VarType function.  It indicates the variable passed to VarType "contains no
valid data" (something I believe only a Variant can do).

Now, I would guess that CreateDC checks the the lpInitData parameter against
NULL (0).  Since you passed vbNull, I assume it then attempts to access a
DEVMODE structure at address 0x00000001, which should throw an access
violation.  Since you didn't report this happening, I'm at a loss to explain
it.  Maybe CreateDC is smarter than I think...


Quote:
> > The problem is vbNull is not what you think it is (it's a Variant that
> > contains a special value indicating null, or something like that).
> > vbNullString was the right choice for the unused third parameter though.

> I thought it might not be, but then why did it work the first way?

> Dale



Sun, 02 Jan 2005 03:06:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Visual Basic 5.0's DBGrid not refreshing properly

2. Form doesn't display properly on user's PC

3. ADO Connection fails with 'sa'

4. Method 'Result' of object FormField failed

5. Method 'Result' of Formfield failed

6. Login failed for user 'XYZ1492\IUSR_XYZ1492'

7. ODBC call failed on linked table '???'

8. ODBC Failed with 'UNION' in query

9. GetObject fails with error '800401e4'

10. Failed to activate control 'VB.UserControl'.

11. method 'value' of object dmsacal70 failed

12. 'DiscardSavedData' Failed

 

 
Powered by phpBB® Forum Software