10 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
11 #define AGENT_MAX_MSGLEN 8192
13 int agent_exists(void)
16 hwnd = FindWindow("Pageant", "Pageant");
24 * Unfortunately, this asynchronous agent request mechanism doesn't
25 * appear to work terribly well. I'm going to comment it out for
26 * the moment, and see if I can come up with a better one :-/
28 #ifdef WINDOWS_ASYNC_AGENT
30 struct agent_query_data {
32 unsigned char *mapping;
36 void (*callback)(void *, void *, int);
40 DWORD WINAPI agent_query_thread(LPVOID param)
42 struct agent_query_data *data = (struct agent_query_data *)param;
46 id = SendMessage(data->hwnd, WM_COPYDATA, (WPARAM) NULL,
50 retlen = 4 + GET_32BIT(data->mapping);
51 ret = snewn(retlen, unsigned char);
53 memcpy(ret, data->mapping, retlen);
58 UnmapViewOfFile(data->mapping);
59 CloseHandle(data->handle);
62 agent_schedule_callback(data->callback, data->callback_ctx, ret, retlen);
69 int agent_query(void *in, int inlen, void **out, int *outlen,
70 void (*callback)(void *, void *, int), void *callback_ctx)
75 unsigned char *p, *ret;
82 hwnd = FindWindow("Pageant", "Pageant");
84 return 1; /* *out == NULL, so failure */
85 mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
86 filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
87 0, AGENT_MAX_MSGLEN, mapname);
89 return 1; /* *out == NULL, so failure */
90 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
92 cds.dwData = AGENT_COPYDATA_ID;
93 cds.cbData = 1 + strlen(mapname);
95 #ifdef WINDOWS_ASYNC_AGENT
96 if (callback != NULL && !(flags & FLAG_SYNCAGENT)) {
98 * We need an asynchronous Pageant request. Since I know of
99 * no way to stop SendMessage from blocking the thread it's
100 * called in, I see no option but to start a fresh thread.
101 * When we're done we'll PostMessage the result back to our
102 * main window, so that the callback is done in the primary
103 * thread to avoid concurrency.
105 struct agent_query_data *data = snew(struct agent_query_data);
108 data->handle = filemap;
109 data->mapname = mapname;
110 data->callback = callback;
111 data->callback_ctx = callback_ctx;
112 data->cds = cds; /* structure copy */
114 if (CreateThread(NULL, 0, agent_query_thread, data, 0, &threadid))
121 * The user either passed a null callback (indicating that the
122 * query is required to be synchronous) or CreateThread failed.
123 * Either way, we need a synchronous request.
125 id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
127 retlen = 4 + GET_32BIT(p);
128 ret = snewn(retlen, unsigned char);
130 memcpy(ret, p, retlen);
136 CloseHandle(filemap);