LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVIXML makes PrintDlg/PrintDlgEx crash under Windows 7 (32 bit)

Solved!
Go to solution

- Load the XMLSample (toolbox\XMLSample.cws)

- Add [CVIDIR]\sdk\lib\msvc\ComDlg32.Lib

- Replace the XMLSample.uir and XMLSample.c with the ones from the attachement

 

This works under XP but not under Windows 7 (32 bit home professional):

When running click PRT (works), then load a XML, then try the PRT button again (crashes).

 

I first used PrintDlg when it crashed, but PrintDlgEx doesn't work either.

 

Can anyone verify this, please? I have only one Win 7 box at hand.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 1 of 5
(4,110 Views)
Solution
Accepted by topic author CVI-User

I was able to reproduce the crash. It's probably happening because the PrintDlg function uses COM and it must require the thread to be initialized as apartment-threaded. And by default, the XML driver (via the CVI ActiveX Library) will initialize it differently, unless it's already been initialized.

 

The crash goes away if you initialize the thread yourself, before calling any of the XML functions:

 

CA_InitActiveXThreadStyleForCurrentThread (NULL, COINIT_APARTMENTTHREADED);

 

Luis

Message 2 of 5
(4,089 Views)
I see, thanks a lot.

Since printing can be called from any thread in my real app, and CA_InitActiveXThreadStyleForCurrentThread (NULL, COINIT_APARTMENTTHREADED); only works for the current thread, it will be tricky (maybe next to impossible) to do...

Do you happen to know, why this has been changed in Windows 7? It works under XP, and I have skipped Vista.
-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 3 of 5
(4,064 Views)

Another interesting bit: if you use PrintDlg and pass hwndOwner=NULL it doesn't crash. This doesn't work with PrintDlgEx since it requires a valid hwndOwner.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 4 of 5
(4,061 Views)

1) I can't tell you why this changed in Windows 7. It has to do with the internals of the PrintDlg function, which I'm not privy to.

 

2) While it's true that CA_InitActiveXThreadStyleForCurrentThread only sets the threading model for the current thread, there's no reason why you can't also call that function from additional threads. You just have to make sure that each thread is initialized correctly. And note that you don't even have to call it from each thread that might print. You just have to call it from each thread that both calls the XML driver and prints.

 

Luis

0 Kudos
Message 5 of 5
(4,037 Views)