WabApi.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. {******************************************************************}
  2. { }
  3. { Borland Delphi Runtime Library }
  4. { Windows Address Book (WAB) functions interface unit }
  5. { }
  6. { Portions created by Microsoft are }
  7. { Copyright (C) 1995-2000 Microsoft Corporation. }
  8. { All Rights Reserved. }
  9. { }
  10. { The original file is: wabapi.h, released 31 Jan 2000. }
  11. { The original Pascal code is: WabApi.pas, released 15 Mar 2000. }
  12. { The initial developer of the Pascal code is Petr Vones }
  13. { (petr.v@mujmail.cz). }
  14. { }
  15. { Portions created by Petr Vones are }
  16. { Copyright (C) 2000 Petr Vones }
  17. { }
  18. { Obtained through: }
  19. { }
  20. { Joint Endeavour of Delphi Innovators (Project JEDI) }
  21. { }
  22. { You may retrieve the latest version of this file at the Project }
  23. { JEDI home page, located at http://delphi-jedi.org }
  24. { }
  25. { The contents of this file are used with permission, subject to }
  26. { the Mozilla Public License Version 1.1 (the "License"); you may }
  27. { not use this file except in compliance with the License. You may }
  28. { obtain a copy of the License at }
  29. { http://www.mozilla.org/MPL/MPL-1.1.html }
  30. { }
  31. { Software distributed under the License is distributed on an }
  32. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  33. { implied. See the License for the specific language governing }
  34. { rights and limitations under the License. }
  35. { }
  36. {******************************************************************}
  37. unit WabApi;
  38. interface
  39. uses
  40. Windows, ActiveX, WabDefs, WabIab;
  41. {$I WAB.INC}
  42. {$ALIGN ON}
  43. {$MINENUMSIZE 4}
  44. (*$HPPEMIT '#include <wabapi.h>'*)
  45. type
  46. IWabObject = interface(IUnknown)
  47. function GetLastError(hResult: HResult; ulFlags: ULONG;
  48. var lppMAPIError: TMapiError): HResult; stdcall;
  49. function AllocateBuffer(cbSize: ULONG; out lppBuffer: Pointer): HResult;
  50. stdcall;
  51. function AllocateMore(cbSize: ULONG; lpObject: Pointer;
  52. out lppBuffer: Pointer): HResult; stdcall;
  53. function FreeBuffer(lpBuffer: Pointer): HResult; stdcall;
  54. function Backup(lpFileName: LPSTR): HResult; stdcall;
  55. function Import(lpImportParam: LPSTR): HResult; stdcall;
  56. function Find(lpIAB: IAddrBook; hWnd: HWND): HResult; stdcall;
  57. function VCardDisplay(lpIAB: IAddrBook; hWnd: HWND;
  58. lpszFileName: LPSTR): HResult; stdcall;
  59. function LDAPUrl(lpIAB: IAddrBook; hWnd: HWND; Flags: ULONG; lpszURL: LPSTR;
  60. lppMailUser: IMailUser): HResult; stdcall;
  61. function VCardCreate(lpIAB: IAddrBook; ulFlags: ULONG; lpszVCard: LPSTR;
  62. lppMailUser: IMailUser): HResult; stdcall;
  63. function VCardRetrieve(lpIAB: IAddrBook; ulFlags: ULONG; lpszVCard: LPSTR;
  64. out lppMailUser: IMailUser): HResult; stdcall;
  65. function GetMe(lpIAB: IAddrBook; ulFlags: ULONG; lpdwAction: PDWORD;
  66. lpsbEID: PSBinary; ulParam: ULONG): HResult; stdcall;
  67. function SetMe(lpIAB: IAddrBook; ulFlags: ULONG; sbEID: PSBinary;
  68. ulParam: ULONG): HResult; stdcall;
  69. end;
  70. {$EXTERNALSYM IWabObject}
  71. const
  72. { WABObject_LDAPUrl flags }
  73. // If this flag is specified and the LDAPUrl returns a single
  74. // query result, instructs the WAB to return the result in the
  75. // form of a MailUser object instead of displaying Details on it
  76. // If there are multiple results to the query, fail ..
  77. WABOBJECT_LDAPURL_RETURN_MAILUSER = $00000001;
  78. {$EXTERNALSYM WABOBJECT_LDAPURL_RETURN_MAILUSER}
  79. // WAB 5.0x and higher:
  80. // If your application supports Unicode and wants to pass ina Unicode
  81. // URL to the WAB, you can cast the Unicode URL to an LPSTR and pass it
  82. // to the LDAPUrl API, *also* setting ulFlags to MAPI_UNICODE to mark the URL
  83. // as such. Casting is prefered to converting the string because
  84. // (a) Converting the string may result in loss of data (b) since this is an
  85. // already published interface we can't modify the interface.
  86. MAPI_UNICODE = $80000000;
  87. {$EXTERNALSYM MAPI_UNICODE}
  88. { WABObject_GetMe returned parameter }
  89. // If the GetMe call results in the creation of a new 'Me' contact,
  90. // the lpdwAction returned parameter will contain this value
  91. // indicating to the caller that the object is freshly created and
  92. // does not have any properties in it
  93. WABOBJECT_ME_NEW = $00000001;
  94. {$EXTERNALSYM WABOBJECT_ME_NEW}
  95. { WABObject_GetMe flags }
  96. // WABObject_GetMe will create a new ME object by default, if
  97. // none already exists. To force the call to not-create an object, if
  98. // one doesn't already exist, specify the WABOBJECT_ME_NOCREATE flag.
  99. // If no me is found, the call fails with MAPI_E_NOT_FOUND.
  100. // Other flag for WABObject_GetMe is AB_NO_DIALOG defined in wabdefs.h
  101. WABOBJECT_ME_NOCREATE = $00000002;
  102. {$EXTERNALSYM WABOBJECT_ME_NOCREATE}
  103. { IWABObject_VCard Create/Retrieve }
  104. // Flags the WAB whether the lpszVCard parameter is a filename or if
  105. // it is a NULL terminated string containing the compelte VCard contents
  106. WAB_VCARD_FILE = $00000000;
  107. {$EXTERNALSYM WAB_VCARD_FILE}
  108. WAB_VCARD_STREAM = $00000001;
  109. {$EXTERNALSYM WAB_VCARD_STREAM}
  110. type
  111. PWabParam = ^TWabParam;
  112. _tagWAB_PARAM = record
  113. cbSize: ULONG; // sizeof(WAB_PARAM).
  114. hwnd: HWND; // hWnd of calling client Application. Can be NULL
  115. szFileName: LPSTR; // WAB File name to open. if NULL, opens default.
  116. ulFlags: ULONG; // See below
  117. guidPSExt: TGUID; // A GUID that identifies the calling application's Property Sheet extensions
  118. // The GUID can be used to determine whether the extension prop sheets are displayed or not.
  119. end;
  120. {$EXTERNALSYM _tagWAB_PARAM}
  121. TWabParam = _tagWAB_PARAM;
  122. WAB_PARAM = _tagWAB_PARAM;
  123. {$EXTERNALSYM WAB_PARAM}
  124. const
  125. { flags for WAB_PARAM }
  126. WAB_USE_OE_SENDMAIL = $00000001; // Tells WAB to use Outlook Express for e-mail before checking for a
  127. // default Simple MAPI client. Default behaviour is to check for the
  128. // Simple MAPI client first
  129. {$EXTERNALSYM WAB_USE_OE_SENDMAIL}
  130. WAB_ENABLE_PROFILES = $00400000; // Invokes WAB in a Identity-aware session using Identity-Manager
  131. // based profiles
  132. {$EXTERNALSYM WAB_ENABLE_PROFILES}
  133. type
  134. TWABOpen = function (out lppAdrBook: IAddrBook; out lppWABObject: IWabObject;
  135. lpWP: PWabParam; Reserved2: DWORD): HResult; stdcall;
  136. TWABOpenEx = function (out lppAdrBook: IAddrBook; out lppWABObject: IWabObject;
  137. lpWP: PWabParam; Reserved: DWORD; fnAllocateBuffer: TAllocateBuffer;
  138. fnAllocateMore: TAllocateMore; fnFreeBuffer: TFreeBuffer): HResult; stdcall;
  139. PWabImportParam = ^TWabImportParam;
  140. _WABIMPORTPARAM = record
  141. cbSize: ULONG; // sizeof(WABIMPORTPARAM)
  142. lpAdrBook: IAddrBook; // ptr to the IAdrBook object (required)
  143. hWnd: HWND; // Parent HWND for any dialogs
  144. ulFlags: ULONG; // 0 or MAPI_DIALOG to show progress dialog and messages
  145. lpszFileName: LPSTR; // FileName to import or NULL .. if NULL will show FileOpen dialog
  146. end;
  147. {$EXTERNALSYM _WABIMPORTPARAM}
  148. TWabImportParam = _WABIMPORTPARAM;
  149. WABIMPORTPARAM = _WABIMPORTPARAM;
  150. {$EXTERNALSYM WABIMPORTPARAM}
  151. const
  152. // ---- WABEXTDISPLAY -----------------
  153. // WABEXTDISPLAY Structure used in extending the WAB Details Property Dialogs
  154. // and for doing WAB Context Menu verb extensions.
  155. // The structure is passed into the IWABExtInit::Initialize method
  156. // of the implementor
  157. WAB_DISPLAY_LDAPURL = $00000001; // The object being displayed is an LDAP URL
  158. // The URL can be found in the lpsz struct member
  159. {$EXTERNALSYM WAB_DISPLAY_LDAPURL}
  160. WAB_CONTEXT_ADRLIST = $00000002; // THe lpv parameter contains a pointer to an
  161. // AdrList structure corresponding to selected items
  162. // on which to display a context menu
  163. {$EXTERNALSYM WAB_CONTEXT_ADRLIST}
  164. WAB_DISPLAY_ISNTDS = $00000004; // Identifies that the entry being displayed originated
  165. // on the NT Directory Service, for clients that use ADSI and
  166. // retrieve additional information from the service.
  167. {$EXTERNALSYM WAB_DISPLAY_ISNTDS}
  168. // MAPI_UNICODE 0x80000000 // Indicates that the WED.lpsz string is actually a UNICODE
  169. // string and should be cast to a (LPWSTR) before using it
  170. // If this flag is not present then the WED.lpsz is a DBCS string
  171. // and should be cast to an LPSTR before using.
  172. type
  173. PWabExtDisplay = ^TWabExtDisplay;
  174. _WABEXTDISPLAY = record
  175. cbSize: ULONG;
  176. lpWABObject: IWabObject; // pointer to IWABObject
  177. lpAdrBook: IAddrBook; // pointer to IAdrBook object
  178. lpPropObj: IMapiProp; // Object being displayed
  179. fReadOnly: BOOL; // Indicates if this is a ReadOnly mode
  180. fDataChanged: BOOL; // Set by extension sheet to signal data change
  181. ulFlags: ULONG; // See above
  182. lpv: Pointer; // Used for passing in specific data
  183. lpsz: LPTSTR; // Used for passing in specific data
  184. end;
  185. {$EXTERNALSYM _WABEXTDISPLAY}
  186. TWabExtDisplay = _WABEXTDISPLAY;
  187. WABEXTDISPLAY = _WABEXTDISPLAY;
  188. {$EXTERNALSYM WABEXTDISPLAY}
  189. type
  190. IWABExtInit = interface(IUnknown)
  191. ['{EA22EBF0-87A4-11D1-9ACF-00A0C91F9C8B}']
  192. function Initialize(lpWABExtDisplay: PWabExtDisplay): HResult; stdcall;
  193. end;
  194. {$EXTERNALSYM IWABExtInit}
  195. const
  196. IID_IWABExtInit = IWABExtInit;
  197. {$EXTERNALSYM IID_IWABExtInit}
  198. WAB_DLL_NAME = 'WAB32.DLL';
  199. {$EXTERNALSYM WAB_DLL_NAME}
  200. WAB_DLL_PATH_KEY = 'Software\Microsoft\WAB\DLLPath';
  201. {$EXTERNALSYM WAB_DLL_PATH_KEY}
  202. function GetWabDllPath(var Path: string): Boolean;
  203. function WabApiLoaded: Boolean;
  204. {$IFDEF WAB_DYNAMIC_LINK_EXPLICIT}
  205. function LoadWabApi: Boolean;
  206. function UnloadWabApi: Boolean;
  207. {$ENDIF}
  208. var
  209. WABOpen: TWABOpen = nil;
  210. {$EXTERNALSYM WABOpen}
  211. WABOpenEx: TWABOpenEx = nil;
  212. {$EXTERNALSYM WABOpenEx}
  213. implementation
  214. function GetWabDllPath(var Path: string): Boolean;
  215. var
  216. Key: HKEY;
  217. BufSize: DWORD;
  218. Buffer: array[0..MAX_PATH] of Char;
  219. begin
  220. Path := '';
  221. Result := False;
  222. if RegOpenKeyEx(HKEY_LOCAL_MACHINE, WAB_DLL_PATH_KEY, 0, KEY_READ, Key) = ERROR_SUCCESS then
  223. begin
  224. BufSize := Sizeof(Buffer);
  225. RegQueryValueEx(Key, '', nil, nil, @Buffer, @BufSize);
  226. RegCloseKey(Key);
  227. SetString(Path, Buffer, BufSize);
  228. Result := True;
  229. end;
  230. end;
  231. var
  232. LibHandle: THandle = 0;
  233. function WabApiLoaded: Boolean;
  234. begin
  235. Result := LibHandle <> 0;
  236. end;
  237. function UnloadWabApi: Boolean;
  238. begin
  239. if WabApiLoaded then
  240. begin
  241. Result := FreeLibrary(LibHandle);
  242. LibHandle := 0;
  243. @WabOpen := nil;
  244. @WabOpenEx := nil;
  245. end else Result := True;
  246. end;
  247. function LoadWabApi: Boolean;
  248. var
  249. WabDllPath: string;
  250. begin
  251. Result := WabApiLoaded;
  252. if (not Result) and GetWabDllPath(WabDllPath) then
  253. begin
  254. LibHandle := LoadLibrary(PChar(WabDllPath));
  255. if WabApiLoaded then
  256. begin
  257. @WABOpen := GetProcAddress(LibHandle, 'WABOpen');
  258. @WABOpenEx := GetProcAddress(LibHandle, 'WABOpenEx');
  259. Result := Assigned(WABOpen) and Assigned(WABOpenEx);
  260. if not Result then UnloadWabApi;
  261. end;
  262. end;
  263. end;
  264. initialization
  265. {$IFNDEF WAB_DYNAMIC_LINK_EXPLICIT}
  266. LoadWabApi;
  267. {$ENDIF}
  268. finalization
  269. UnloadWabApi;
  270. end.