home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD1076510182000.psc / modContext.bas < prev    next >
Encoding:
BASIC Source File  |  2000-10-19  |  10.0 KB  |  232 lines

  1. Attribute VB_Name = "modContext"
  2. Option Explicit
  3.  
  4. ' Hello everybody who will ready this!
  5. ' This program is a DLL that adds to the context menu when right-clicking
  6. ' in explorer, but can also be used as a base to program onto the more
  7. ' demanding area of namepsace extention.
  8. ' These areas are very poorly documented, and mostly in C, making it very hard
  9. ' to do anything in VB. I did manage to find a bit of code written in VB to almost
  10. ' do exactly the same thing as this, but I have rewritten it for ease of use
  11. ' and understanding. This piece of code was very poorly commented, too.
  12. ' However, it did include two type library's that make interfacing to Explorer
  13. ' possible in this circumstance. I certainly would struggle a lot before
  14. ' actually writing a type library that worked!
  15. ' Well, here's the source code in its entirety. There should also be a .REG
  16. ' file that imports the registry settings to make this program work. The DLL
  17. ' file will need to be registered, too. Then right-click on a file, and
  18. ' see the new menu item thats added. Then try it with a group of files,
  19. ' even folders ;)
  20. ' I didn't include a bitmap as an icon in the menu, 'coz that's asking for trouble.
  21. ' I won't say that this DLL will actually be useful for anybody, but it shows
  22. ' how to do some really neat stuff that is otherwise untouched by the hands
  23. ' of VB Programmers.
  24. '
  25. ' I've worked hard to write this for you, and all of the other people out there,
  26. ' so I will invoke a [Greek, I think] proverb:
  27. ' "Give credit where credit is due!" - and I WANT CREDIT!
  28. '
  29. ' Likewise, I must stick to my own principles - Thanks to Andy Stotzer for the
  30. ' type libraries that he created/found that this project revolves upon!
  31. '
  32. ' So, Enjoy the code!
  33. '
  34. ' Jolyon Bloomfield   ICQ: 11084041    E-mail Jolyon_B@Hotmail.Com
  35. '
  36. ' P.S., Remember VERSION COMPATIBILITY for upgrades and stuff, because the GUID will
  37. ' change with every compile you do if you don't put on binary compatibility!
  38. '
  39. ' P.P.S., BTW, the resource file stores the icon for this dll ;)
  40.  
  41.  
  42. '
  43. ' These are the types necessary for transmitting data to the shell
  44. '
  45. Public Type STGMEDIUM
  46.   tymed               As Long
  47.   hGlobal             As Long
  48.   pUnkForRelease      As IUnknown
  49. End Type
  50.  
  51. Public Type FORMATETC
  52.   cfFormat            As Long
  53.   ptd                 As Long
  54.   dwAspect            As Long
  55.   lindex              As Long
  56.   tymed               As Long
  57. End Type
  58.  
  59. Public Type CMINVOKECOMMANDINFO
  60.     cbSize              As Long    ' sizeof(CMINVOKECOMMANDINFO)
  61.     fMask               As Long    ' any combination of CMIC_MASK_*
  62.     hWnd                As Long    ' might be NULL (indicating no owner window)
  63.     lpVerb              As Long    ' either a string or MAKEINTRESOURCE(idOffset)
  64.     lpParameters        As Long    ' might be NULL (indicating no parameter)
  65.     lpDirectory         As Long    ' might be NULL (indicating no specific directory)
  66.     nShow               As Long    ' one of SW_ values for ShowWindow() API
  67.     dwHotKey            As Long
  68.     hIcon               As Long
  69. End Type
  70.  
  71.  
  72. '
  73. ' The API calls...
  74. '
  75. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
  76. Public Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal HDROP As Long, ByVal pUINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long
  77. Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
  78. Public Declare Function ReleaseStgMedium Lib "ole32.dll" (pMedium As STGMEDIUM) As Long
  79. Public Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
  80. Public Declare Function VirtualProtect Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flNewProtect As Long, ByRef lpflOldProtect As Long) As Long
  81. Public Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long
  82. Public Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (lpString1 As Any, lpString2 As Any) As Long
  83. Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (lpstring As Any) As Long
  84. Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
  85.  
  86.  
  87. '
  88. ' And the constants
  89. '
  90. Public Const CF_HDROP = 15              ' For gettings the files
  91. Public Const DVASPECT_CONTENT = 1       '  "
  92. Public Const TYMED_HGLOBAL = 1          '  "
  93. Public Const REG_SZ = 1&                ' Registry access
  94. Public Const PAGE_EXECUTE_READWRITE = &H40&      ' Memory functioning
  95.  
  96. ' Menu flags for Add/Check/EnableMenuItem/etc
  97. Public Const MF_INSERT = &H0&
  98. Public Const MF_CHANGE = &H80&
  99. Public Const MF_APPEND = &H100&
  100. Public Const MF_DELETE = &H200&
  101. Public Const MF_REMOVE = &H1000&
  102. Public Const MF_BYCOMMAND = &H0&
  103. Public Const MF_BYPOSITION = &H400&
  104. Public Const MF_SEPARATOR = &H800&
  105. Public Const MF_ENABLED = &H0&
  106. Public Const MF_GRAYED = &H1&
  107. Public Const MF_DISABLED = &H2&
  108. Public Const MF_UNCHECKED = &H0&
  109. Public Const MF_CHECKED = &H8&
  110. Public Const MF_USECHECKBITMAPS = &H200&
  111. Public Const MF_STRING = &H0&
  112. Public Const MF_BITMAP = &H4&
  113. Public Const MF_OWNERDRAW = &H100&
  114. Public Const MF_POPUP = &H10&
  115. Public Const MF_MENUBARBREAK = &H20&
  116. Public Const MF_MENUBREAK = &H40&
  117. Public Const MF_UNHILITE = &H0&
  118. Public Const MF_HILITE = &H80&
  119. Public Const MF_DEFAULT = &H1000&
  120. Public Const MF_SYSMENU = &H2000&
  121. Public Const MF_HELP = &H4000&
  122. Public Const MF_RIGHTJUSTIFY = &H4000&
  123. Public Const MF_MOUSESELECT = &H8000&
  124. Public Const MF_END = &H80&
  125.  
  126. Public Const MFT_STRING = MF_STRING
  127. Public Const MFT_BITMAP = MF_BITMAP
  128. Public Const MFT_MENUBARBREAK = MF_MENUBARBREAK
  129. Public Const MFT_MENUBREAK = MF_MENUBREAK
  130. Public Const MFT_OWNERDRAW = MF_OWNERDRAW
  131. Public Const MFT_RADIOCHECK = &H200&
  132. Public Const MFT_SEPARATOR = MF_SEPARATOR
  133. Public Const MFT_RIGHTORDER = &H2000&
  134. Public Const MFT_RIGHTJUSTIFY = MF_RIGHTJUSTIFY
  135.  
  136. ' Menu flags for Add/Check/EnableMenuItem/etc
  137. Public Const MFS_GRAYED = &H3&
  138. Public Const MFS_DISABLED = MFS_GRAYED
  139. Public Const MFS_CHECKED = MF_CHECKED
  140. Public Const MFS_HILITE = MF_HILITE
  141. Public Const MFS_ENABLED = MF_ENABLED
  142. Public Const MFS_UNCHECKED = MF_UNCHECKED
  143. Public Const MFS_UNHILITE = MF_UNHILITE
  144. Public Const MFS_DEFAULT = MF_DEFAULT
  145.  
  146. ' QueryContextMenu uFlags
  147. Public Const CMF_NORMAL = &H0&
  148. Public Const CMF_DEFAULTONLY = &H1&
  149. Public Const CMF_VERBSONLY = &H2&
  150. Public Const CMF_EXPLORE = &H4&
  151. Public Const CMF_NOVERBS = &H8&
  152. Public Const CMF_CANRENAME = &H10&
  153. Public Const CMF_NODEFAULT = &H20&
  154. Public Const CMF_INCLUDESTATIC = &H40&
  155. Public Const CMF_RESERVED = &HFFFF0000
  156.  
  157. ' GetCommandString uFlags
  158. Public Const GCS_VERBA = &H0&                   ' canonical verb
  159. Public Const GCS_HELPTEXTA = &H1&               ' help text (for status bar)
  160. Public Const GCS_VALIDATEA = &H2&               ' validate command exists
  161. Public Const GCS_VERBW = &H4&                   ' canonical verb (Unicode)
  162. Public Const GCS_HELPTEXTW = &H5&               ' help text (Unicode version)
  163. Public Const GCS_VALIDATEW = &H6&               ' validate command exists (Unicode)
  164.  
  165. Public Const CMDSTR_NEWFOLDER = "NewFolder"
  166. Public Const CMDSTR_VIEWLIST = "ViewList"
  167. Public Const CMDSTR_VIEWDETAILS = "ViewDetails"
  168.  
  169.  
  170. '--------------------------------------------------------------------------
  171. '-      And now for the program's code, not the API calls and stuff       -
  172. '--------------------------------------------------------------------------
  173.  
  174. ' For storing the old address of a remapped procedure
  175. Public pOldFunction As Long
  176. ' Two constants that our DLL recognises the menu systems by.
  177. ' They have to be in here so that sc_QueryContextMenu can access them
  178. Public Const mPROGRAM_CLASS_NAME = "ContextMenu.cMenu"
  179. Public Const mMENU_ITEM_TEXT = "My NIFTY little &Extension"
  180. Public Const mSTATUS_TEXT = "Check out what my NIFTY little extension can do!"
  181. ' The Ampersand ("&") says to add the little line under the letter
  182. ' like in a normal VB menu.
  183.  
  184. Public Function ReplaceVtableEntry(pObj As Long, _
  185.     EntryNumber As Integer, _
  186.     ByVal lpfn As Long) As Long
  187. '
  188. ' Don't even ask about this procedure... I've basically ripped it out of the
  189. ' original source code; I certainly couldn't write it myself, but I do understand it.
  190. ' Basically, it rips out the reference to the class' function that needs to
  191. ' be replaced, and replaces it with the address of another function.
  192. ' It actually alters memory inside the VB workings of the VTable, and I really
  193. ' suggest that you stay away from it as far as possible...
  194. '
  195.  
  196. Dim lOldAddr        As Long
  197. Dim lpVtableHead    As Long
  198. Dim lpfnAddr        As Long
  199. Dim lOldProtect     As Long
  200.  
  201. CopyMemory lpVtableHead, ByVal pObj, 4
  202. lpfnAddr = lpVtableHead + (EntryNumber - 1) * 4
  203. CopyMemory lOldAddr, ByVal lpfnAddr, 4
  204.  
  205. Call VirtualProtect(lpfnAddr, 4, PAGE_EXECUTE_READWRITE, lOldProtect)
  206. CopyMemory ByVal lpfnAddr, lpfn, 4
  207. Call VirtualProtect(lpfnAddr, 4, lOldProtect, lOldProtect)
  208.  
  209. ReplaceVtableEntry = lOldAddr
  210. End Function
  211.  
  212. ' This is the function that modifies the menu to add our own items to it.
  213. ' Do anything that needs to be done to the menu here!
  214. Public Function sc_QueryContextMenu(ByVal This As IContextMenu, _
  215.     ByVal hMenu As Long, _
  216.     ByVal indexMenu As Long, _
  217.     ByVal idCmdFirst As Long, _
  218.     ByVal idCmdLast As Long, _
  219.     ByVal uFlags As Long) As Long
  220.  
  221. Dim rc              As Long
  222. Dim idCmd           As Long
  223. Dim szMenu          As String
  224. Dim szMenuText      As String
  225. Dim bAppendItems    As Boolean
  226. Dim szTemp          As String
  227.  
  228. idCmd = idCmdFirst
  229. bAppendItems =aString
  230. kpldAddr
  231. End Function FunA
  232. Ainu        .rbleHef-VtableHead, ByVal pObj, 4
  233. ByVayVal leHeobleHef-VtartIaaWTlidat     pdLong) As L pObYag) As As