pagent.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #define AGENT_COPYDATA_ID 0x804e50ba
  5. #define AGENT_MAX_MSGLEN 8192
  6. #define GET_32BIT_MSB_FIRST(cp) \
  7. (((unsigned long)(unsigned char)(cp)[0] << 24) | \
  8. ((unsigned long)(unsigned char)(cp)[1] << 16) | \
  9. ((unsigned long)(unsigned char)(cp)[2] << 8) | \
  10. ((unsigned long)(unsigned char)(cp)[3]))
  11. #define GET_32BIT(cp) GET_32BIT_MSB_FIRST(cp)
  12. #define RET_ERR_BADARGS 10
  13. #define RET_ERR_UNAVAILABLE 11
  14. #define RET_ERR_NOMAP 12
  15. #define RET_ERR_BINSTDIN 13
  16. #define RET_ERR_BINSTDOUT 14
  17. #define RET_ERR_BADLEN 15
  18. #define RET_NORESPONSE 1
  19. #define RET_RESPONSE 0
  20. int main (int argc, const char* argv[]) {
  21. HWND hwnd;
  22. char *mapname;
  23. HANDLE filemap;
  24. unsigned char *p, *ret;
  25. int id, retlen, inlen, n, rmode, r = RET_NORESPONSE;
  26. COPYDATASTRUCT cds;
  27. void *in;
  28. if (argc < 2)
  29. return RET_ERR_BADARGS;
  30. hwnd = FindWindow("Pageant", "Pageant");
  31. if (!hwnd)
  32. return RET_ERR_UNAVAILABLE;
  33. rmode = _setmode(_fileno(stdin), _O_BINARY);
  34. if (rmode == -1)
  35. return RET_ERR_BINSTDIN;
  36. rmode = _setmode(_fileno(stdout), _O_BINARY);
  37. if (rmode == -1)
  38. return RET_ERR_BINSTDOUT;
  39. inlen = atoi(argv[1]);
  40. in = malloc(inlen);
  41. n = fread(in, 1, inlen, stdin);
  42. if (n != inlen) {
  43. free(in);
  44. return RET_ERR_BADLEN;
  45. }
  46. mapname = malloc(32);
  47. n = sprintf(mapname, "PageantRequest%08x", (unsigned)GetCurrentThreadId());
  48. filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
  49. 0, AGENT_MAX_MSGLEN, mapname);
  50. if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
  51. free(in);
  52. free(mapname);
  53. return RET_ERR_NOMAP;
  54. }
  55. p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
  56. memcpy(p, in, inlen);
  57. cds.dwData = AGENT_COPYDATA_ID;
  58. cds.cbData = 1 + n;
  59. cds.lpData = mapname;
  60. id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
  61. if (id > 0) {
  62. r = RET_RESPONSE;
  63. retlen = 4 + GET_32BIT(p);
  64. fwrite(p, 1, retlen, stdout);
  65. }
  66. free(in);
  67. free(mapname);
  68. UnmapViewOfFile(p);
  69. CloseHandle(filemap);
  70. return r;
  71. }