Repository navigation
Expand file tree
/
Copy pathddeexecute.c
More file actions
116 lines (85 loc) · 2.47 KB
/
Copy pathddeexecute.c
File metadata and controls
116 lines (85 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <shellapi.h>
#include <ddeml.h>
// just hoping this is big enough
#define STRSAFE_MAX_CCH 1000
HDDEDATA CALLBACK dummy_callback(
UINT uType,
UINT uFmt,
HCONV hconv,
HSZ hsz1,
HSZ hsz2,
HDDEDATA hdata,
ULONG_PTR dwData1,
ULONG_PTR dwData2
) { return 0; }
int dde_execute(LPWSTR service, LPWSTR topic, LPWSTR command, UINT *lasterr)
{
DWORD idInst = 0;
DWORD cbCmdLength;
int retcode = 0;
UINT ret = DdeInitializeW(&idInst, &dummy_callback, APPCLASS_STANDARD, 0);
if (ret)
{
return 1;
}
HSZ hszService = DdeCreateStringHandleW(idInst, service, CP_WINUNICODE);
HSZ hszTopic = DdeCreateStringHandleW(idInst, topic, CP_WINUNICODE);
printf("Created string handles\n");
HCONV hConv = DdeConnect(idInst, hszService, hszTopic, 0);
if (hConv)
{
printf("Connected!\n");
cbCmdLength = (wcslen(command)+1) * sizeof(wchar_t);
HDDEDATA hDdeData = DdeClientTransaction((LPBYTE)command, cbCmdLength, hConv, 0,0, XTYP_EXECUTE, 1000, 0);
*lasterr = DdeGetLastError(idInst);
DdeFreeDataHandle(hDdeData);
printf("Completed transaction\n");
}
else {
printf("Could not connect!\n");
retcode = 3;
}
DdeFreeStringHandle(idInst, hszTopic);
DdeFreeStringHandle(idInst, hszService);
DdeDisconnect(hConv);
DdeUninitialize(idInst);
printf("Cleaned up\n");
return retcode;
}
int main()
{
LPWSTR *szArglist;
int nArgs;
UINT lasterr;
int dderes;
// Read Unicode command-line arguments
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if( NULL == szArglist )
{
wprintf(L"CommandLineToArgvW failed\n");
return 1;
}
// // Diagnostics:
// int i;
// for( i=0; i<nArgs; i++)
// wprintf(L"%d: %ws\n", i, szArglist[i]);
if( nArgs != 4 ) {
printf("usage: ddeexecute <service> <topic> <command>");
return 1;
}
// We're good to go
dderes = dde_execute(szArglist[1], szArglist[2], szArglist[3], &lasterr);
// Diagnostics:
if(dderes == 0)
printf("DdeClientTransaction returned %d\n", lasterr);
else
printf("dderes = %d\n", dderes);
// Free memory allocated for CommandLineToArgvW arguments.
LocalFree(szArglist);
// printf("Hit any key to exit...");
// char c; c = getchar();
return dderes;
}