ControlNode


I'm looking for a way of determining if a local printer is shared. I've looked into APRINTERS(), ENUMPRINTERCONNECTIONS of WSCRIPT.NETWORK and a couple other places/things and not found a way of doing this. I know there has to be a way of checking, but again time limits are preventing me from looking for an extended period for a fix and again I turn to asking.



Re: How to check if a local printer is shared in VFP at runtime.

dni


For that you may need to check shared resouces of your system. You may use:

http://news2news.com/vfp/ function=-1&example=351







Re: How to check if a local printer is shared in VFP at runtime.

Naomi Nosonovsky

Perhaps you may get this info somehow exploring this route:

Code Snippet

objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
colInstalledPrinters = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer") && you can select your own printer using = or like clause

For Each objPrinter in colInstalledPrinters

objPrinter._Path.Server

objPrinter.Path_.Security_.Privileges.Count (you may try to loop through collection)


"Name: " , objPrinter.Name
"Location: " , objPrinter.Location
"Server Name: " , objPrinter.ServerName
"Share Name: " , objPrinter.ShareName
Next

next

I bet the information if the printer is local or shared can be found if you dig some documentation on WMI.

I also found Win32_Share class which is may be better. http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/sharedfolders/






Re: How to check if a local printer is shared in VFP at runtime.

ControlNode

A friend of mine informed me of where the data is stored in the registry and I've found the following code to work so far. I'll test some code based on this sample on a machine that I have VERY limited permissions on and make sure it still works.

Code Snippet

&& list network and shared local printers

iPrinterCount = APRINTERS(aPrinterList,1)

oShell = NEWOBJECT("WScript.Shell")

FOR iVar = 1 TO iPrinterCount

IF LEFT(aPrinterList[m.iVar, 1], 2) = "\\" OR printerIsShared(aPrinterList[m.iVar, 1])

aPrinterList[m.iVar, 1]

ENDIF

ENDFOR

oShell = .null.

RETURN

FUNCTION printerIsShared

LPARAMETERS lcPrinterName

LOCAL llIsShared

TRY

cShareName = oShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\" + m.lcPrinterName + "\Share Name")

llIsShared = !EMPTY(cShareName)

CATCH TO oErr_24Q0SZNX6

=MESSAGEBOX("Error(" + TRANSFORM(oErr_24Q0SZNX6.errorno) + ") when getting share settings for printer(" + m.lcPrinterName + ").", 0+48, "printerIsShared function")

ENDTRY

RETURN m.llIsShared

ENDFUNC






Re: How to check if a local printer is shared in VFP at runtime.

dni

Yes it is, but you may need to have admin rights to access then.




Re: How to check if a local printer is shared in VFP at runtime.

Naomi Nosonovsky

Yes, it's going to work as well. But AFAIK WMI is cooler Smile You get ShareName and don't need to check the registry.

See my sample above, you just need to use

Code Snippet

#define CRLF CHR(13) + CHR(10)
strComputer = "."
CREATE CURSOR tprinstatus (name c(80),location c(20),status c(20), Sharename C(100) NULL)
objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
colInstalledPrinters = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")

For Each objPrinter in colInstalledPrinters
m.name= objPrinter.Name
m.Location=TRANSFORM(objPrinter.Location)
m.status=''
m.ShareName = objPrinter.ShareName
DO Case &&objPrinter.PrinterStatus
Case objPrinter.PrinterStatus=1
m.status="Other"
Case objPrinter.PrinterStatus=2
m.status="Unknown"
Case objPrinter.PrinterStatus=3
m.status="Idle"
Case objPrinter.PrinterStatus=4
m.status="Printing"
Case objPrinter.PrinterStatus=5
m.status="Warmup"
Endcase
INSERT INTO tprinstatus FROM memvar
NEXT
SELECT tprinstatus
BROWSE

BTW, status is not really determined by this method, but I just showed it here.





Re: How to check if a local printer is shared in VFP at runtime.

ControlNode

I guess I should have refreshed the window before posting my progress. If I run into any permissions issues I'll look more into the WMI route. Right now any method that works is cool with me and I've already coded it with the oShell. Thanks alot though, still good info that I may use the next time I have to do this or if my current route bites me...




Re: How to check if a local printer is shared in VFP at runtime.

Naomi Nosonovsky

No prob, I learnt something new as well, and I also got all great scripts from http://www.microsoft.com/downloads/thankyou.aspx familyId=b4cb2678-dafb-4e30-b2da-b8814fe2da5a&displayLang=en



Re: How to check if a local printer is shared in VFP at runtime.

ControlNode

Hahaha, I just got bite by both the oShell and WMI ways for doing this. I had no problem detecting shared local printers with the oShell code as a basic user that can't even add a printer to the computer. But both methods look at the "share name", well this can be populated on a printer that is no longer shared. Basicly if you add a local printer and shared it, then decide to not share it and don't clear the share name it still has a share name and both my ideas on how to use oShell or WMI assumed that if the "share name" is not empty or .null it was a shared printer. In most cases this is not an issue, but could be a little "gotcha" that could prove a problem if not documented.




Re: How to check if a local printer is shared in VFP at runtime.

ControlNode

OK, I got time to play with this in my "free time" over the weekend and found that Naomi's mention of win32_share is the key to a "real" list of currently shared printers. When used with the aprinters it is fairly easy to get a list of printers being shared from the local computer. The win32_printer and oshell.regread ideas were only providing share names, I found this to not a good test of wheather a printer IS shared.

Thank you.






Re: How to check if a local printer is shared in VFP at runtime.

Naomi Nosonovsky

What is the code you end up with



Re: How to check if a local printer is shared in VFP at runtime.

ControlNode

Here is the code that I was using for testing the idea on a couple computers. I may have been able to use oShare.caption instead of oShare.path but felt more comfortable using path. NOTE: the reason for the first CASE is that my original requirement was a list of known network printers. I had not problem detecting network printers of other computers with the first CASE and the question I asked here revolved around getting the results that the second CASE provides.

Code Snippet

cComputer = "."

cComputerName = GETWORDNUM(SYS(0), 1)

CREATE CURSOR curSharedPrinters (printername c(100))

CREATE CURSOR curShares (cname c(80), cpath c(100))

oWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + cComputer + "\root\cimv2")

colShares = oWMIService.ExecQuery ("SELECT * FROM Win32_share")

FOR EACH oShare IN colShares

INSERT INTO 'curShares' (cname, cpath) VALUES (oShare.Name, oShare.path)

ENDFOR

SELECT 'curShares'

INDEX ON ALLTRIM(cpath) TAG cpath

iPrinters = APRINTERS(aKnownPrinters)

FOR iVar = 1 TO iPrinters

cPrinterName = aKnownPrinters[iVar, 1]

DO CASE

CASE LEFT(m.cPrinterName, 2) = "\\"

&& network printer

INSERT INTO 'curSharedPrinters' VALUES (m.cPrinterName)

CASE SEEK(m.cPrinterName + ",LocalsplOnly", 'curShares', 'cpath')

&& shared local printer

INSERT INTO 'curSharedPrinters' VALUES ("\\" + m.cComputerName + "\" + m.cPrinterName) && add local sysID to name

OTHERWISE

&& most likey not a network or local shared printer

ENDCASE

ENDFOR

SELECT curSharedPrinters

BROWSE