FreeRDP
client/Windows/cli/wfreerdp.c
1 
22 #include <freerdp/config.h>
23 
24 #include <winpr/windows.h>
25 
26 #include <winpr/crt.h>
27 
28 #include <freerdp/freerdp.h>
29 #include <freerdp/constants.h>
30 
31 #include <freerdp/client/file.h>
32 #include <freerdp/client/cmdline.h>
33 #include <freerdp/client/channels.h>
34 #include <freerdp/channels/channels.h>
35 
36 #include "../resource/resource.h"
37 
38 #include <wf_client.h>
39 #include <wf_defaults.h>
40 
41 #include <shellapi.h>
42 
43 INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
44 {
45  int status;
46  HANDLE thread;
47  wfContext* wfc;
48  DWORD dwExitCode;
49  rdpContext* context;
50  rdpSettings* settings;
51  LPWSTR cmd;
52  char** argv = NULL;
53  RDP_CLIENT_ENTRY_POINTS clientEntryPoints = { 0 };
54  int ret = 1;
55  int argc = 0;
56  LPWSTR* args = NULL;
57 
58  WINPR_UNUSED(hInstance);
59  WINPR_UNUSED(hPrevInstance);
60  WINPR_UNUSED(lpCmdLine);
61  WINPR_UNUSED(nCmdShow);
62 
63  RdpClientEntry(&clientEntryPoints);
64  context = freerdp_client_context_new(&clientEntryPoints);
65 
66  if (!context)
67  return -1;
68 
69  cmd = GetCommandLineW();
70 
71  if (!cmd)
72  goto out;
73 
74  args = CommandLineToArgvW(cmd, &argc);
75 
76  if (!args || (argc <= 0))
77  goto out;
78 
79  argv = calloc((size_t)argc, sizeof(char*));
80 
81  if (!argv)
82  goto out;
83 
84  for (int i = 0; i < argc; i++)
85  {
86  int size = WideCharToMultiByte(CP_UTF8, 0, args[i], -1, NULL, 0, NULL, NULL);
87  if (size <= 0)
88  goto out;
89  argv[i] = calloc((size_t)size, sizeof(char));
90 
91  if (!argv[i])
92  goto out;
93 
94  if (WideCharToMultiByte(CP_UTF8, 0, args[i], -1, argv[i], size, NULL, NULL) != size)
95  goto out;
96  }
97 
98  freerdp_client_warn_deprecated(argc, argv);
99 
100  settings = context->settings;
101  wfc = (wfContext*)context;
102 
103  if (!settings || !wfc)
104  goto out;
105 
106  status = freerdp_client_settings_parse_command_line(settings, argc, argv, FALSE);
107  if (status)
108  {
109  ret = freerdp_client_settings_command_line_status_print(settings, status, argc, argv);
110  goto out;
111  }
112 
113  AddDefaultSettings(settings);
114 
115  if (freerdp_client_start(context) != 0)
116  goto out;
117 
118  thread = freerdp_client_get_thread(context);
119 
120  if (thread)
121  {
122  if (WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0)
123  {
124  GetExitCodeThread(thread, &dwExitCode);
125  ret = (int)dwExitCode;
126  }
127  }
128 
129  if (freerdp_client_stop(context) != 0)
130  goto out;
131 
132 out:
133  freerdp_client_context_free(context);
134 
135  if (argv)
136  {
137  for (int i = 0; i < argc; i++)
138  free(argv[i]);
139 
140  free(argv);
141  }
142 
143  LocalFree(args);
144  return ret;
145 }
146 
147 #ifdef WITH_WIN_CONSOLE
148 int main()
149 {
150  return WinMain(NULL, NULL, NULL, 0);
151 }
152 #endif