11 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
12 #define AGENT_MAX_MSGLEN 8192
14 #define GET_32BIT(cp) \
15 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
16 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
17 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
18 ((unsigned long)(unsigned char)(cp)[3]))
20 int agent_exists(void)
23 hwnd
= FindWindow("Pageant", "Pageant");
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
);
67 int agent_query(void *in
, int inlen
, void **out
, int *outlen
,
68 void (*callback
)(void *, void *, int), void *callback_ctx
)
73 unsigned char *p
, *ret
;
80 hwnd
= FindWindow("Pageant", "Pageant");
82 return 1; /* *out == NULL, so failure */
83 mapname
= dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
84 filemap
= CreateFileMapping(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
,
85 0, AGENT_MAX_MSGLEN
, mapname
);
87 return 1; /* *out == NULL, so failure */
88 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
90 cds
.dwData
= AGENT_COPYDATA_ID
;
91 cds
.cbData
= 1 + strlen(mapname
);
93 if (callback
!= NULL
&& !(flags
& FLAG_SYNCAGENT
)) {
95 * We need an asynchronous Pageant request. Since I know of
96 * no way to stop SendMessage from blocking the thread it's
97 * called in, I see no option but to start a fresh thread.
98 * When we're done we'll PostMessage the result back to our
99 * main window, so that the callback is done in the primary
100 * thread to avoid concurrency.
102 struct agent_query_data
*data
= snew(struct agent_query_data
);
105 data
->handle
= filemap
;
106 data
->mapname
= mapname
;
107 data
->callback
= callback
;
108 data
->callback_ctx
= callback_ctx
;
109 data
->cds
= cds
; /* structure copy */
111 if (CreateThread(NULL
, 0, agent_query_thread
, data
, 0, &threadid
))
117 * The user either passed a null callback (indicating that the
118 * query is required to be synchronous) or CreateThread failed.
119 * Either way, we need a synchronous request.
121 id
= SendMessage(hwnd
, WM_COPYDATA
, (WPARAM
) NULL
, (LPARAM
) & cds
);
123 retlen
= 4 + GET_32BIT(p
);
124 ret
= snewn(retlen
, unsigned char);
126 memcpy(ret
, p
, retlen
);
132 CloseHandle(filemap
);