Monday, October 24, 2011

Convert external document files to PDF via ShellExecute()

Originally posted in www.junblogs.com

The easiest way to convert a report into a pdf file is via using a pdfprinter.  There are lots of pdf printers outside there which you can use.  You can check these sites:

to name a few.  Converting the report is as easy as these:

SET PRINTER TO NAME('PrimoPDF') && or something like that or a variable that contains the name of the PDF printer

REPORT FORM YourReport TO PRINTER NOCONSOLE


SET PRINTER TO DEFAULT && This is essential to instruct VFP to return back to its default printer

Friday, October 21, 2011

IEXPLORE.EXE virus Manual Removal


This is actually an old infection and this can be easily killed so I ignored sharing this one before.  However, some units here in our office has been again infected by this virus that is why I have to manually kill those again as our antivirus seems to not recognize this one.

What is IEXPLORE.EXE?

This is actually the executable filename of Microsoft Internet Explorer.  But since some virus propagator decides to be a little naughty, they named the process the same name.  If I am creating a virus, I might name a virus with a certain Microsoft processes as well so it can fool a lot of people thinking it is legal.

Since this virus has the same processes’ name with that of Internet Explorer, how will we know if what you are seeing in the Task Manager is the virus or the real Internet Explorer?  Simple, if there is no open Internet Explorer, then it is a virus.  If there is an open Internet Explorer, close it. After closing Internet Explorer and there still is an instance of IEXPLORE.EXE, then that is a virus.

What does this virus do?

Wednesday, October 19, 2011

Auto Run your exe on OS logon

While we are at it (Your exe and nothing else), I want to show now here the ways to make your exe sort of auto run during logon, the places where we can do that and the advantages and disadvantages if any.

There are 3 common places I would choose to ensure that my exe will auto-load so users will no longer need to double-click it when they open their computer unit.  I will start the sequence from the first to the last:

a.  Startup folder - this is the very first instance that the OS will load or try to run anything.  You can put the shortcut of your exe here.  Click Start button, All Programs (or simply Programs), then Startup folder.  Right-click it and choose Explore.  Paste your exe shortcut here. 

As I said, the OS will try to run "first" what is here before the other ways I will show below and this is a sort of advantage.  Its disadvantage is a user can delete that shortcut later removing that auto-run(load) effect you want your exe to have.

b.  You can create an entry inside registry in this:

Your exe and nothing else

Most POS developers says to themselves "Oooo!  I want to hide task bar and start button so users can not switch to other exe while using my POS system.  I also want to hide this, disable this and kill this, etc."  Although in reality most developers do not really say "Oooo!" and instead use stronger words but that is the way I want to paint that expression here for fun, LOL!

So they find ways to do that and of course those are always possible via playing with registry settings or in some cases via WinAPIs.

However, why go into the trouble of doing all those things when there is simpler and easier way to achieve that?  Is it possible to make your exe the only thing running and nothing else?  Sure.

A normal windows OS uses Explorer.exe as its shell.  There are other more 3rd party shells out there in the net and I tried some before but because of familiarity with Explorer, I always go back to the default.  What is a shell?

Shell, briefly explained:

A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel.   Its primary purpose is to invoke or "launch" another program; however, shells frequently have additional capabilities such as viewing the contents of directories. - wiki

So what has an OS shell go to do with what I am trying to say here?  When you use Explorer.exe as shell, then it will load the taskbar, it will load the desktop, it will load a lot of everything.... which in the end you wanted to kill or disable later anyway.  So why go into the trouble of disabling, hiding, or killing those things later..... when we can simply prevent those to be loaded in the first place?

What I am trying to say is use your exe as the OS shell so that is the only thing that will be loaded.... and nothing else.  Below is a simple snippet to do that.  I used scripting here but if you are not comfortable with it, you can use registry.prg that is shipped with VFP (HOME()+"samples\classes\registry.prg"):




**********************
* ShellSwitcher.prg
* Author: Jun Tangunan, October 19, 2011
**********************
Local oForm As Form
oForm = Createobject('ShellForm')
oForm.Show(1)
Return

Define Class ShellForm As Form
      AutoCenter = .T.
      Height = 100
      Width = 300
      Caption = "Sandstorm's Shell Switcher"
      MinButton = .F.
      MaxButton = .F.
      BorderStyle = 1

      Add Object label1 As Label With;
            top=35, Left= 10, Caption = "Please Select what Shell to use later",;
            AutoSize = .T., FontBold = .T.

      Add Object opgshell As OptionGroup With ;
            top = 50, Left = 10, ButtonCount = 2, Value = 1, ;
            AutoSize = .T., BorderStyle = 0

      Add Object cmdgo As CommandButton With;
            top=60, Left= 165, Caption = "Apply", Width = 100, Height = 30


      Procedure Init
            With Thisform.opgshell
                  .option1.Caption = "Explorer (Default)"
                  .option1.AutoSize = .T.
                  .option2.Caption = "This System"
                  .option2.AutoSize = .T.
            Endwith

      Function cmdgo.Click
            If Messagebox("Proceed?",4+32,"Switch Shell") = 6
                  Local loWSH As wscript.Shell
                  loWSH = Createobject("wscript.shell")
                  loWSH.RegWrite(;
                        "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell",;
                        Iif(Thisform.opgshell.Value=1,"Explorer.exe",;
                        "< your exe with full path >"))
                  Messagebox("A log off is necessary, click ok to log off now!",;
                        0+64,"System Restart Needed!")
                  loWSH.Run("CMD /C LOGOFF",2,.T.)
                  Thisform.Release
                  Return
            Else
                  Messagebox("Shell switching is aborted!",0+64,"Oppppssss!")
            Endif
      Endfunc

Enddefine


Here are some more things you need to know to be able to use this:
  1. You should be a member of an Administrator Account because it deals with tampering values of Local Machine in Registry 
  2. I have not killed Task Manager here so once you have tested your exe to be the Shell and would want to go back to the OS default (Explorer.exe), then you can press Ctrl+Alt+Del keys to active Task Manager, run Explorer.Exe, go to where this code snippet is, run it again and switch back to Explorer as the Shell.
 Tested under WindowsXP SP2.

You can incorporate the above codes as part of your project so that you can switch shells from within your exe without invoking the task manager. And once you are comfortable with it, then you can either proceed with disabling the task manager as well so it will really be your exe and nothing else.....


Enjoy!

Wednesday, October 12, 2011

Selectively Close Open files in Server

A question is raised recently in Foxite Forum on how to avoid an error during backup creation if a table is left open by users.  My suggestion is to use Computer Management Console in the server itself either by logging directly onto the server unit or remote controlling it via remote desktop:


However, that is the manual way of doing it.  A fellow foxiter named Yahia Aboudalal said he is using this command to close those files, i.e., OpenFiles.  That is great because I am not even aware that such command exists, LOL!

Anyway, this will need to know the ID of the open files so we can selectively close those.  Like in our office here, some engineers stays well beyond 5PM so I would not want to execute something that will suddenly close their files as well, won't we?  So here is a simple snippet I prepared today to test on our server.  The purpose is to selectively close only the files my app is using:


Create Cursor junkopen (xID I, xuser c(10), xsystem c(10), xfiles c(100))


Local oShell As wscript.Shell, lcTempFile
lcTempFile = Addbs(Getenv("TMP"))+Sys(3)+".txt"
oShell = Createobject("WScript.Shell")

* create a CSV list of open files in server
oShell.Run("cmd /c openfiles /query /S < your server name >"+;
      "/U administrator /FO CSV /NH > "+m.lcTempFile,2,.T.)

* Clean it up, remove those warnings above the list
Strtofile(Strextract(Filetostr(m.lcTempFile),;
      "again.."),(m.lcTempFile))

* add records to our cursor
Append From (m.lcTempFile) Type Csv

* Clean it up
Select xfiles, xID From junkopen Where ;
      INLIST(Upper(Justext(xfiles)),"DBF","CDX","APP","FPT") ;
      AND !Empty(Justext(xfiles)) Into Cursor junkopen

* Start closing remaining files on the list
Scan
      Wait Window "Attempting to close "+;
            ALLTRIM(junkopen.xfiles) Nowait
      oShell.Run("CMD /C OPENFILES /DISCONNECT /S "+;
            "< your server name > /ID "+Transform(junkopen.xID),2,.T.)
Endscan

Messagebox("Target Open Files has been closed!")

* Perform your backup routine




And as usual if I believe this will benefit others, then I post it here in my blog than inside the forum so it won't be lost easily.



Friday, October 7, 2011

ssClasses Library

ssClasses is now an open source and is part of CodePlex.  The library including the source codes will be made available for download on or before December 31, 2012.

http://vfpx.codeplex.com/wikipage?title=ssClasses

====================

This section is dedicated to show the list of ssClasses objects.  Info here will be updated from time to time as new sub-classes is added or existing ones are enhanced.  Right now, these are the list of what I wanted to include with my latest release of ssClasses objects:

  • ssExcelPivot - is the latest class which gives your app the power to easily create pivot reports inside excel even without having the knowledge to know how

  • ssAnchorSizer - When objects are resized properly via anchor properties, the insides (column widths)  of these 3 objects are left, i.e., grid, listbox, and combobox.  This class complements anchor resizing by resizing as well the insides of those 3 objects mentioned:

    • ssGridSorter - sorts your grid columns in ascending/descending order without the need for users to create their own indexes.  An arrow on the column shows which column has the current sorting and whether it is on ascending or descending order: 


      • ssToExcel - is a class that exports your cursor/table contents into excel, pdf or HTML; complete with headings, filterings, totals, formattings, etc.

        • ssTitleBar - Gives your form a very unique appearance with 6 colors and 9 themes or a total of 54 possible looks.  Colors can be switched anytime and is retained.
          • ssSkinner - a simple spinner class that we can use to control the colors of ssclasses objects like ssTitleBar, ssButton, etc.  This also gives us the ability to change colors of grid's highlightbackcolor, container bacground, etc.  This class is also embedded in ssTitleBar class.

            • ssClose - a very simple Close button on the left side of the screen that glows

            • ssDropCalc - a dropdown calculator class that can show either value or formula.  Transfers result to its own textbox for later easy retrieval.   

            Check it from here: http://sandstorm36.blogspot.com/2011/06/ssdropcalc-dropdown-calculator-class.html


              • ssPage - a unique looking tab page to replace the native VFP Pages of a pageframe

              • ssCal - a widget looking calendar that adds beauty to your form.  Can perform immediate commands when dates are changed, comes in 6 colors:

              • ssSwitch - a sort of switch that can be used to replace checkbox or optiongroup buttons.  Originally created named Switchbox, this remake of the old class is leaner and cleaner.

              • ssButton2 - The 2nd button class I made that have 63 possible cool looks:
              Update by MK Sharma (March 16, 2012) - now the button changes color on MouseEnter.

                  • ssButton3-  a Microsoft Office 2010 button like with 6 highlight colors:. 
                  • ssButton4 - this one is colored
                    And I realized that in some cases, combining different color groups things properly like this:


                  • ssMonthCal - yet another monthly calendar class. This one absolutely have no image, all are shapes manipulated.
                  • ssPolyClock - Original author is my cousin, Glen Villar.  An analog clock that runs with 100% VFP objects and codes.  Adjusted it to give you a cool looking native VFP clock, the way ssClasses are, now with 6 skins to choose from:

                      • ssGridLock - a class that dynamically locks the left columns of your grid.   



                          ssChkBox - a class we can use replace the boring appearance of a checkbox.  Works almost the same:



                          ssContainer - Is the descendant of my ssConfiner class.  This is just like a normal container class of VFP but with added twist that this is better looking and has a sort of title bar.  Have added another feature which is if you don't put the designed width of the bar, it will auto-compute based on caption:



                          ssTab - This is a new class I am working currently.  This is better than ssPage in that this supports the ability to have icons on the tabs.  Right now it have 4 colors, inactive and active.


                          ssContainer4 - the latest design on my containers.  This one doesn't have any image whatsoever and the shapes are manipulated only by polypoints.  Click the name of the class to see its post.

                          ssOptSwitch - is my version of OptionGroup, only that it uses ssSwitches.  Have 6 themes to expect of:



                          Try clicking a class name to see if a detailed post about it has already been made.


                          ssButton3 (new)

                          November 7 update:

                          ssButton2 & ssButton3

                          Originally, changing its Enabled status will not be visually reflected outright and it will take effect only once you move your mouse pointer on top of it via MouseEnter event. Fixed that now that when you issue Thisform.ssbutton3_1.Enabled = .F., it will be refelected immediately on all objects within the class like the caption and the button itself.  Same when you turn its Enabled property back to .T.

                          ===============

                          ssButton3 is the 3rd style of my command buttons sort of class.  ssButton is shared before in weblogs.com while  ssButton2 is shared somewhere inside Foxite forum.

                          For those who are new to ssButton classes, I designed it to replace the native commandbutton object of VFP.  ssButton3 is just like a normal button in almost every way now but with better GUI and hover effect.  If you have seen MS Office 2010 where a button changes from plain color to somewhat orange highlighting, then you can imagine how ssButton3 works.  It is just like that but with additional feature that it has more highlight colors to choose from (I made the default yellow) and you can choose between SpecialEffect of 3D or Hot Tracking (also known as Hover Effect by some).

                          Here is a sample image of that on one of my modules here.  Mouse pointer is over Search button and so the color has changed into yellow.  If you will take a closer look, ssbutton3 has a glow effect inside which makes the appearance better:


                          Its usage is very simple.  To set the appearance of the button, just double-click it and in its INIT event, place something like what is shown below.  Intellisense will aid you in knowing the proper settings and proper naming convention is used to guide users whether it is numeric, character, logical, etc.:


                          Right now, it has 6 possible hover color to choose from:

                          1 = red
                          2 = green
                          3 = yellow (default)
                          4 = orange (just like MS Office 2010)
                          5 = brown
                          6 = blue


                          One major improvement of this new class (as well as ssContainer2) over its ancestors is that the button is no longer a dull plain square.






                          ssContainer's curvature, I forgot to mention on its own blog, can be changed by the user.  I have added that now as well to ssButton3.  The hover effect on focus is running on default curvature while the ones above it I set to 40 just to show here how it will look like:


                          And for comparison, I will show ssButton2 & 3 side by side here:



                          For the current listing of ssClasses, click this link:  http://sandstorm36.blogspot.com/2011/10/ssclasses-components-list.html


                          Thursday, October 6, 2011

                          ssMonthCal class


                          This class is based on my ssCal sub-class.  The main differences are it does not contain a front page, it does not contain any outside image, it is easier for user to navigate between months and years, and it will not clutter your form where this class will be dragged with a lot of unnecessary objects that the class has.

                          This is an experiment how to make some of my classes pretty without the need for an outside image and I think I can safely say that the experiment is a success.  This class uses all native VFP objects like shapes, optiongroup, spinner and combobox.  This is neater and leaner both in objects and codes versus its ancestor ssCal classes.

                          Since ssClasses are known for its ability to provide the developer different colors as each of us has our own taste, right now this have 7 colors:




                          Its usage is really simple.  Just drag it in your form and it will work on default values which is Green (4) and current date.  If you want to specify the color or the date, then double-click it and on its INIT event, type:

                          This._settings(6,date()-5)  

                          That will mean color blue and date will be current date minus 5 days.  Colors are numbered 1 to 7:
                          1 = red
                          2 = orange
                          3 = yellow
                          4 = green (default)
                          5 = brown
                          6 = blue
                          7 = pink

                          This class can immediately perform any action when the date is changed.  Let us say that you have a grid on your form and that you wanted to repopulate it with new values based on the date you will select on the class.  So in the class' CLICK event, do something like this:

                          Click Event:
                          thisform._gridrefresh()    && a method that will repopulate the grid with an SQL SELECT...

                          To get its value is just like on any other native VFP objects.

                          ldDate = thisform.ssMonthCal1.Value
                          Messagebox(thisform.ssMonthCal1.Value)

                          Note:  The title bar you see here is ssTitleBar class and is using Theme 3 Blue.  ssTitleBar can change the color of the title bar on the spot via ssSpinner class embedded onto it (left side of the bar).

                          See a sample below (click it to see the actual size):


                          For the current listing of ssClasses, click this link:  http://sandstorm36.blogspot.com/2011/10/ssclasses-components-list.html





                          Monday, October 3, 2011

                          Password Protect Multiple Excel Files

                          Our Laboratory Manager came to my room this morning and asked me how to password protect an excel sheet.  That is so his department can comply with one of the local government's requirement to ensure that the worksheets won't be easily accessible to others in case they gain access to his office' files.  And I showed him how. 

                          He said that can I please assist him creating those passwords for his excel report files and I said "sure" without realizing the enormity of that simple request. 

                          So I started opening his excel file, saving it again this time with password protection, opening another and doing the same thing again........ until I reached the 20th file on the current folder (there are some more folders) where I started to get impatient.  So I stopped what I am doing and decided to see how many files I needed to protect, so, right-click  properties.... darn! 2,765 excel files!!!!  Darn again!

                          Anyway, I said to myself, why am I wasting my time doing it manually when we can automate it?  Sometimes it escapes my mind that we are developers, LOL!