10 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
11 #define AGENT_MAX_MSGLEN 8192
13 #define GET_32BIT(cp) \
14 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
15 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
16 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
17 ((unsigned long)(unsigned char)(cp)[3]))
19 int agent_exists(void)
22 hwnd
= FindWindow("Pageant", "Pageant");
30 * Unfortunately, this asynchronous agent request mechanism doesn't
31 * appear to work terribly well. I'm going to comment it out for
32 * the moment, and see if I can come up with a better one :-/
34 #ifdef WINDOWS_ASYNC_AGENT
36 struct agent_query_data
{
38 unsigned char *mapping
;
42 void (*callback
)(void *, void *, int);
46 DWORD WINAPI
agent_query_thread(LPVOID param
)
48 struct agent_query_data
*data
= (struct agent_query_data
*)param
;
52 id
= SendMessage(data
->hwnd
, WM_COPYDATA
, (WPARAM
) NULL
,
56 retlen
= 4 + GET_32BIT(data
->mapping
);
57 ret
= snewn(retlen
, unsigned char);
59 memcpy(ret
, data
->mapping
, retlen
);
64 UnmapViewOfFile(data
->mapping
);
65 CloseHandle(data
->handle
);
68 agent_schedule_callback(data
->callback
, data
->callback_ctx
, ret
, retlen
);
75 int agent_query(void *in
, int inlen
, void **out
, int *outlen
,
76 void (*callback
)(void *, void *, int), void *callback_ctx
)
81 unsigned char *p
, *ret
;
88 hwnd
= FindWindow("Pageant", "Pageant");
90 return 1; /* *out == NULL, so failure */
91 mapname
= dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
92 filemap
= CreateFileMapping(INVALID_HANDLE_VALUE
, NULL
, PAGE_READWRITE
,
93 0, AGENT_MAX_MSGLEN
, mapname
);
95 return 1; /* *out == NULL, so failure */
96 p
= MapViewOfFile(filemap
, FILE_MAP_WRITE
, 0, 0, 0);
98 cds
.dwData
= AGENT_COPYDATA_ID
;
99 cds
.cbData
= 1 + strlen(mapname
);
100 cds
.lpData
= mapname
;
101 #ifdef WINDOWS_ASYNC_AGENT
102 if (callback
!= NULL
&& !(flags
& FLAG_SYNCAGENT
)) {
104 * We need an asynchronous Pageant request. Since I know of
105 * no way to stop SendMessage from blocking the thread it's
106 * called in, I see no option but to start a fresh thread.
107 * When we're done we'll PostMessage the result back to our
108 * main window, so that the callback is done in the primary
109 * thread to avoid concurrency.
111 struct agent_query_data
*data
= snew(struct agent_query_data
);
114 data
->handle
= filemap
;
115 data
->mapname
= mapname
;
116 data
->callback
= callback
;
117 data
->callback_ctx
= callback_ctx
;
118 data
->cds
= cds
; /* structure copy */
120 if (CreateThread(NULL
, 0, agent_query_thread
, data
, 0, &threadid
))
127 * The user either passed a null callback (indicating that the
128 * query is required to be synchronous) or CreateThread failed.
129 * Either way, we need a synchronous request.
131 id
= SendMessage(hwnd
, WM_COPYDATA
, (WPARAM
) NULL
, (LPARAM
) &cds
);
133 retlen
= 4 + GET_32BIT(p
);
134 ret
= snewn(retlen
, unsigned char);
136 memcpy(ret
, p
, retlen
);
142 CloseHandle(filemap
);