Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.4 KiB

  1. ////////////////////////////////////
  2. // (C) COPYRIGHT MICROSOFT CORP., 1998-1999
  3. //
  4. // FILE: WIASCNEX.CPP
  5. //
  6. // DESCRIPTION: Implements core DLL routines.
  7. //
  8. #include "precomp.h"
  9. #pragma hdrstop
  10. #include <string.h>
  11. #include <tchar.h>
  12. #include "resource.h"
  13. #include "wiascidl_i.c"
  14. CComModule _Module;
  15. BEGIN_OBJECT_MAP(ObjectMap)
  16. OBJECT_ENTRY(CLSID_ScannerShellExt, CShellExt)
  17. END_OBJECT_MAP()
  18. static CComBSTR g_strCategory;
  19. STDAPI DllRegisterServer(void)
  20. {
  21. // registers object, typelib and all interfaces in typelib
  22. return _Module.RegisterServer(TRUE);
  23. }
  24. STDAPI DllUnregisterServer(void)
  25. {
  26. return _Module.UnregisterServer();
  27. }
  28. EXTERN_C
  29. BOOL
  30. DllMain(
  31. HINSTANCE hinst,
  32. DWORD dwReason,
  33. LPVOID lpReserved)
  34. {
  35. switch (dwReason) {
  36. case DLL_PROCESS_ATTACH:
  37. _Module.Init (ObjectMap, hinst);
  38. DisableThreadLibraryCalls(hinst);
  39. break;
  40. case DLL_PROCESS_DETACH:
  41. _Module.Term();
  42. break;
  43. }
  44. return TRUE;
  45. }
  46. extern "C" STDMETHODIMP DllCanUnloadNow(void)
  47. {
  48. return _Module.GetLockCount()==0 ? S_OK : S_FALSE;
  49. }
  50. extern "C" STDAPI DllGetClassObject(
  51. REFCLSID rclsid,
  52. REFIID riid,
  53. LPVOID *ppv)
  54. {
  55. return _Module.GetClassObject(rclsid, riid, ppv);
  56. }
  57. /*****************************************************************************
  58. ShowMessage
  59. Utility function for displaying messageboxes
  60. ******************************************************************************/
  61. BOOL ShowMessage (HWND hParent, INT idCaption, INT idMessage)
  62. {
  63. MSGBOXPARAMS mbp;
  64. BOOL bRet;
  65. INT i;
  66. ZeroMemory (&mbp, sizeof(mbp));
  67. mbp.cbSize = sizeof(mbp);
  68. mbp.hwndOwner = hParent;
  69. mbp.hInstance = g_hInst;
  70. mbp.lpszText = MAKEINTRESOURCE(idMessage);
  71. mbp.lpszCaption = MAKEINTRESOURCE(idCaption);
  72. mbp.dwStyle = MB_OK | MB_APPLMODAL;
  73. i = MessageBoxIndirect (&mbp);
  74. bRet = (IDOK==i);
  75. return bRet;
  76. }
  77. /*****************************************************************************
  78. FindLastID
  79. Utility for getting the last relative pidl from a full pidl
  80. ******************************************************************************/
  81. // unsafe macros
  82. #define _ILSkip(pidl, cb) ((LPITEMIDLIST)(((BYTE*)(pidl))+cb))
  83. #define ILNext(pidl) _ILSkip(pidl, (pidl)->mkid.cb)
  84. LPITEMIDLIST
  85. FindLastID(LPCITEMIDLIST pidl)
  86. {
  87. LPCITEMIDLIST pidlLast = pidl;
  88. LPCITEMIDLIST pidlNext = pidl;
  89. if (pidl == NULL)
  90. return NULL;
  91. // Find the last one
  92. while (pidlNext->mkid.cb)
  93. {
  94. pidlLast = pidlNext;
  95. pidlNext = ILNext(pidlLast);
  96. }
  97. return (LPITEMIDLIST)pidlLast;
  98. }
  99. /*****************************************************************************
  100. CreateDeviceFromID
  101. Utility for attaching to WIA and getting a root IWiaItem interface
  102. *****************************************************************************/
  103. HRESULT
  104. CreateDeviceFromId (LPWSTR szDeviceId, IWiaItem **ppItem)
  105. {
  106. IWiaDevMgr *pDevMgr;
  107. HRESULT hr = CoCreateInstance (CLSID_WiaDevMgr,
  108. NULL,
  109. CLSCTX_LOCAL_SERVER,
  110. IID_IWiaDevMgr,
  111. reinterpret_cast<LPVOID*>(&pDevMgr));
  112. if (SUCCEEDED(hr))
  113. {
  114. BSTR strId = SysAllocString (szDeviceId);
  115. hr = pDevMgr->CreateDevice (strId, ppItem);
  116. SysFreeString (strId);
  117. pDevMgr->Release ();
  118. }
  119. return hr;
  120. }
  121. /*****************************************************************************\
  122. GetNamesFromDataObject
  123. Return the list of selected item identifiers. Each identifier is of the form
  124. "<DEVICEID>::<FULL PATH NAME>". the list is double-null terminated
  125. *****************************************************************************/
  126. LPWSTR
  127. GetNamesFromDataObject (IDataObject *lpdobj, UINT *puItems)
  128. {
  129. FORMATETC fmt;
  130. STGMEDIUM stg;
  131. LPWSTR szRet = NULL;
  132. LPWSTR szCurrent;
  133. UINT nItems;
  134. size_t size;
  135. if (puItems)
  136. {
  137. *puItems = 0;
  138. }
  139. fmt.cfFormat = (CLIPFORMAT) RegisterClipboardFormat (TEXT("WIAItemNames"));
  140. fmt.dwAspect = DVASPECT_CONTENT;
  141. fmt.lindex = -1;
  142. fmt.ptd = NULL;
  143. fmt.tymed = TYMED_HGLOBAL;
  144. if (lpdobj && puItems && SUCCEEDED(lpdobj->GetData (&fmt, &stg)))
  145. {
  146. szCurrent = reinterpret_cast<LPWSTR>(GlobalLock (stg.hGlobal));
  147. // count the number of items in the double-null terminated string
  148. szRet = szCurrent;
  149. nItems = 0;
  150. while (*szRet)
  151. {
  152. nItems++;
  153. while (*szRet)
  154. {
  155. szRet++;
  156. }
  157. szRet++;
  158. }
  159. *puItems = nItems;
  160. size = (szRet-szCurrent+1)*sizeof(WCHAR);
  161. szRet = new WCHAR[size];
  162. CopyMemory (szRet, szCurrent, size);
  163. GlobalUnlock (stg.hGlobal);
  164. GlobalFree (stg.hGlobal);
  165. }
  166. return szRet;
  167. }
  168. VOID Trace(LPCTSTR format,...)
  169. {
  170. //#ifdef DEBUG
  171. TCHAR Buffer[1024];
  172. va_list arglist;
  173. va_start(arglist, format);
  174. wvsprintf(Buffer, format, arglist);
  175. va_end(arglist);
  176. OutputDebugString(Buffer);
  177. OutputDebugString(TEXT("\n"));
  178. //#endif
  179. }