If only one path is specified like it would happen if someone used the "open
[TortoiseGit.git] / src / TortoiseMerge / TortoiseMerge.cpp
blob4c344181db5ff27ba3a7dddf17d8e633524afd95
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2013 - TortoiseGit
4 // Copyright (C) 2006-2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include <dlgs.h>
22 #include "TortoiseMerge.h"
23 #include "MainFrm.h"
24 #include "AboutDlg.h"
25 #include "CmdLineParser.h"
26 #include "version.h"
27 #include "AppUtils.h"
28 #include "PathUtils.h"
29 #include "BrowseFolder.h"
30 #include "DirFileEnum.h"
31 #include "SelectFileFilter.h"
32 #include "FileDlgEventHandler.h"
33 #include "TempFile.h"
34 #include "TaskbarUUID.h"
36 #ifdef _DEBUG
37 #define new DEBUG_NEW
38 #endif
40 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
42 BEGIN_MESSAGE_MAP(CTortoiseMergeApp, CWinAppEx)
43 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
44 END_MESSAGE_MAP()
46 class PatchOpenDlgEventHandler : public CFileDlgEventHandler
48 public:
49 PatchOpenDlgEventHandler() {}
50 ~PatchOpenDlgEventHandler() {}
52 virtual STDMETHODIMP OnButtonClicked(IFileDialogCustomize* pfdc, DWORD dwIDCtl)
54 if (dwIDCtl == 101)
56 CComQIPtr<IFileOpenDialog> pDlg = pfdc;
57 if (pDlg)
59 pDlg->Close(S_OK);
62 return S_OK;
67 CTortoiseMergeApp::CTortoiseMergeApp()
68 : m_nAppLook(0)
70 EnableHtmlHelp();
71 m_bHiColorIcons = TRUE;
74 // The one and only CTortoiseMergeApp object
75 CTortoiseMergeApp theApp;
76 CString sOrigCWD;
77 #if ENABLE_CRASHHANLDER
78 CCrashReportTGit g_crasher(L"TortoiseGitMerge " _T(APP_X64_STRING), TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD, TGIT_VERDATE);
79 #endif
81 CString g_sGroupingUUID;
82 CString g_sGroupingIcon;
83 bool g_bGroupingRemoveIcon = false;
85 // CTortoiseMergeApp initialization
86 BOOL CTortoiseMergeApp::InitInstance()
88 SetDllDirectory(L"");
89 SetTaskIDPerUUID();
90 CCrashReport::Instance().AddUserInfoToReport(L"CommandLine", GetCommandLine());
93 DWORD len = GetCurrentDirectory(0, NULL);
94 if (len)
96 std::unique_ptr<TCHAR[]> originalCurrentDirectory(new TCHAR[len]);
97 if (GetCurrentDirectory(len, originalCurrentDirectory.get()))
99 sOrigCWD = originalCurrentDirectory.get();
100 sOrigCWD = CPathUtils::GetLongPathname(sOrigCWD);
105 //set the resource dll for the required language
106 CRegDWORD loc = CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
107 long langId = loc;
108 CString langDll;
109 HINSTANCE hInst = NULL;
112 langDll.Format(_T("%sLanguages\\TortoiseMerge%ld.dll"), (LPCTSTR)CPathUtils::GetAppParentDirectory(), langId);
114 hInst = LoadLibrary(langDll);
115 CString sVer = _T(STRPRODUCTVER);
116 CString sFileVer = CPathUtils::GetVersionFromFile(langDll);
117 if (sFileVer.Compare(sVer)!=0)
119 FreeLibrary(hInst);
120 hInst = NULL;
122 if (hInst != NULL)
123 AfxSetResourceHandle(hInst);
124 else
126 DWORD lid = SUBLANGID(langId);
127 lid--;
128 if (lid > 0)
130 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
132 else
133 langId = 0;
135 } while ((hInst == NULL) && (langId != 0));
136 TCHAR buf[6] = { 0 };
137 _tcscpy_s(buf, _T("en"));
138 langId = loc;
139 CString sHelppath = CPathUtils::GetAppDirectory() + _T("TortoiseMerge_en.chm");
140 free((void*)m_pszHelpFilePath);
141 m_pszHelpFilePath=_tcsdup(sHelppath);
142 sHelppath = CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseMerge_en.chm");
145 GetLocaleInfo(MAKELCID(langId, SORT_DEFAULT), LOCALE_SISO639LANGNAME, buf, _countof(buf));
146 CString sLang = _T("_");
147 sLang += buf;
148 sHelppath.Replace(_T("_en"), sLang);
149 if (PathFileExists(sHelppath))
151 free((void*)m_pszHelpFilePath);
152 m_pszHelpFilePath=_tcsdup(sHelppath);
153 break;
155 sHelppath.Replace(sLang, _T("_en"));
156 GetLocaleInfo(MAKELCID(langId, SORT_DEFAULT), LOCALE_SISO3166CTRYNAME, buf, _countof(buf));
157 sLang += _T("_");
158 sLang += buf;
159 sHelppath.Replace(_T("_en"), sLang);
160 if (PathFileExists(sHelppath))
162 free((void*)m_pszHelpFilePath);
163 m_pszHelpFilePath=_tcsdup(sHelppath);
164 break;
166 sHelppath.Replace(sLang, _T("_en"));
168 DWORD lid = SUBLANGID(langId);
169 lid--;
170 if (lid > 0)
172 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
174 else
175 langId = 0;
176 } while (langId);
177 setlocale(LC_ALL, "");
178 // We need to explicitly set the thread locale to the system default one to avoid possible problems with saving files in its original codepage
179 // The problems occures when the language of OS differs from the regional settings
180 // See the details here: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100887
181 SetThreadLocale(LOCALE_SYSTEM_DEFAULT);
183 // InitCommonControls() is required on Windows XP if an application
184 // manifest specifies use of ComCtl32.dll version 6 or later to enable
185 // visual styles. Otherwise, any window creation will fail.
186 InitCommonControls();
188 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
189 CMFCButton::EnableWindowsTheming();
190 EnableTaskbarInteraction(FALSE);
192 // Initialize all Managers for usage. They are automatically constructed
193 // if not yet present
194 InitContextMenuManager();
195 InitKeyboardManager();
196 InitTooltipManager ();
197 CMFCToolTipInfo params;
198 params.m_bVislManagerTheme = TRUE;
200 GetTooltipManager ()->SetTooltipParams (
201 AFX_TOOLTIP_TYPE_ALL,
202 RUNTIME_CLASS (CMFCToolTipCtrl),
203 &params);
205 CCmdLineParser parser = CCmdLineParser(this->m_lpCmdLine);
207 g_sGroupingUUID = parser.GetVal(L"groupuuid");
209 if (parser.HasKey(_T("?")) || parser.HasKey(_T("help")))
211 CString sHelpText;
212 sHelpText.LoadString(IDS_COMMANDLINEHELP);
213 MessageBox(NULL, sHelpText, _T("TortoiseGitMerge"), MB_ICONINFORMATION);
214 return FALSE;
217 // Initialize OLE libraries
218 if (!AfxOleInit())
220 AfxMessageBox(IDP_OLE_INIT_FAILED);
221 return FALSE;
223 AfxEnableControlContainer();
224 // Standard initialization
225 // If you are not using these features and wish to reduce the size
226 // of your final executable, you should remove from the following
227 // the specific initialization routines you do not need
228 // Change the registry key under which our settings are stored
229 SetRegistryKey(_T("TortoiseGitMerge"));
231 if (CRegDWORD(_T("Software\\TortoiseGitMerge\\Debug"), FALSE)==TRUE)
232 AfxMessageBox(AfxGetApp()->m_lpCmdLine, MB_OK | MB_ICONINFORMATION);
234 // To create the main window, this code creates a new frame window
235 // object and then sets it as the application's main window object
236 CMainFrame* pFrame = new CMainFrame;
237 if (pFrame == NULL)
238 return FALSE;
239 m_pMainWnd = pFrame;
241 // create and load the frame with its resources
242 if (!pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL))
243 return FALSE;
245 // Fill in the command line options
246 pFrame->m_Data.m_baseFile.SetFileName(parser.GetVal(_T("base")));
247 pFrame->m_Data.m_baseFile.SetDescriptiveName(parser.GetVal(_T("basename")));
248 pFrame->m_Data.m_baseFile.SetReflectedName(parser.GetVal(_T("basereflectedname")));
249 pFrame->m_Data.m_theirFile.SetFileName(parser.GetVal(_T("theirs")));
250 pFrame->m_Data.m_theirFile.SetDescriptiveName(parser.GetVal(_T("theirsname")));
251 pFrame->m_Data.m_theirFile.SetReflectedName(parser.GetVal(_T("theirsreflectedname")));
252 pFrame->m_Data.m_yourFile.SetFileName(parser.GetVal(_T("mine")));
253 pFrame->m_Data.m_yourFile.SetDescriptiveName(parser.GetVal(_T("minename")));
254 pFrame->m_Data.m_yourFile.SetReflectedName(parser.GetVal(_T("minereflectedname")));
255 pFrame->m_Data.m_mergedFile.SetFileName(parser.GetVal(_T("merged")));
256 pFrame->m_Data.m_mergedFile.SetDescriptiveName(parser.GetVal(_T("mergedname")));
257 pFrame->m_Data.m_mergedFile.SetReflectedName(parser.GetVal(_T("mergedreflectedname")));
258 pFrame->m_Data.m_sPatchPath = parser.HasVal(_T("patchpath")) ? parser.GetVal(_T("patchpath")) : _T("");
259 pFrame->m_Data.m_sPatchPath.Replace('/', '\\');
260 if (parser.HasKey(_T("patchoriginal")))
261 pFrame->m_Data.m_sPatchOriginal = parser.GetVal(_T("patchoriginal"));
262 if (parser.HasKey(_T("patchpatched")))
263 pFrame->m_Data.m_sPatchPatched = parser.GetVal(_T("patchpatched"));
264 pFrame->m_Data.m_sDiffFile = parser.GetVal(_T("diff"));
265 pFrame->m_Data.m_sDiffFile.Replace('/', '\\');
266 if (parser.HasKey(_T("oneway")))
267 pFrame->m_bOneWay = TRUE;
268 if (parser.HasKey(_T("diff")))
269 pFrame->m_bOneWay = FALSE;
270 if (parser.HasKey(_T("reversedpatch")))
271 pFrame->m_bReversedPatch = TRUE;
272 if (parser.HasKey(_T("saverequired")))
273 pFrame->m_bSaveRequired = true;
274 if (parser.HasKey(_T("saverequiredonconflicts")))
275 pFrame->m_bSaveRequiredOnConflicts = true;
276 if (pFrame->m_Data.IsBaseFileInUse() && !pFrame->m_Data.IsYourFileInUse() && pFrame->m_Data.IsTheirFileInUse())
278 pFrame->m_Data.m_yourFile.TransferDetailsFrom(pFrame->m_Data.m_theirFile);
281 if ((!parser.HasKey(_T("patchpath")))&&(parser.HasVal(_T("diff"))))
283 // a patchfile was given, but not folder path to apply the patch to
284 // If the patchfile is located inside a working copy, then use the parent directory
285 // of the patchfile as the target directory, otherwise ask the user for a path.
286 if (parser.HasKey(_T("wc")))
287 pFrame->m_Data.m_sPatchPath = pFrame->m_Data.m_sDiffFile.Left(pFrame->m_Data.m_sDiffFile.ReverseFind('\\'));
288 else
290 CBrowseFolder fbrowser;
291 fbrowser.m_style = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
292 if (fbrowser.Show(NULL, pFrame->m_Data.m_sPatchPath)==CBrowseFolder::CANCEL)
293 return FALSE;
297 if ((parser.HasKey(_T("patchpath")))&&(!parser.HasVal(_T("diff"))))
299 // A path was given for applying a patchfile, but
300 // the patchfile itself was not.
301 // So ask the user for that patchfile
303 HRESULT hr;
304 // Create a new common save file dialog
305 CComPtr<IFileOpenDialog> pfd = NULL;
306 hr = pfd.CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER);
307 if (SUCCEEDED(hr))
309 // Set the dialog options
310 DWORD dwOptions;
311 if (SUCCEEDED(hr = pfd->GetOptions(&dwOptions)))
313 hr = pfd->SetOptions(dwOptions | FOS_FILEMUSTEXIST | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST);
316 // Set a title
317 if (SUCCEEDED(hr))
319 CString temp;
320 temp.LoadString(IDS_OPENDIFFFILETITLE);
321 pfd->SetTitle(temp);
323 CSelectFileFilter fileFilter(IDS_PATCHFILEFILTER);
324 hr = pfd->SetFileTypes(fileFilter.GetCount(), fileFilter);
325 bool bAdvised = false;
326 DWORD dwCookie = 0;
327 CComObjectStackEx<PatchOpenDlgEventHandler> cbk;
328 CComQIPtr<IFileDialogEvents> pEvents = cbk.GetUnknown();
331 CComPtr<IFileDialogCustomize> pfdCustomize;
332 hr = pfd->QueryInterface(IID_PPV_ARGS(&pfdCustomize));
333 if (SUCCEEDED(hr))
335 // check if there's a unified diff on the clipboard and
336 // add a button to the fileopen dialog if there is.
337 UINT cFormat = RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
338 if ((cFormat)&&(OpenClipboard(NULL)))
340 HGLOBAL hglb = GetClipboardData(cFormat);
341 if (hglb)
343 pfdCustomize->AddPushButton(101, CString(MAKEINTRESOURCE(IDS_PATCH_COPYFROMCLIPBOARD)));
344 hr = pfd->Advise(pEvents, &dwCookie);
345 bAdvised = SUCCEEDED(hr);
347 CloseClipboard();
352 // Show the save file dialog
353 if (SUCCEEDED(hr) && SUCCEEDED(hr = pfd->Show(pFrame->m_hWnd)))
355 // Get the selection from the user
356 CComPtr<IShellItem> psiResult = NULL;
357 hr = pfd->GetResult(&psiResult);
358 if (bAdvised)
359 pfd->Unadvise(dwCookie);
360 if (SUCCEEDED(hr))
362 PWSTR pszPath = NULL;
363 hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath);
364 if (SUCCEEDED(hr))
366 pFrame->m_Data.m_sDiffFile = pszPath;
367 CoTaskMemFree(pszPath);
370 else
372 // no result, which means we closed the dialog in our button handler
373 std::wstring sTempFile;
374 if (TrySavePatchFromClipboard(sTempFile))
375 pFrame->m_Data.m_sDiffFile = sTempFile.c_str();
378 else
380 if (bAdvised)
381 pfd->Unadvise(dwCookie);
382 return FALSE;
385 else
387 OPENFILENAME ofn = {0}; // common dialog box structure
388 TCHAR szFile[MAX_PATH] = {0}; // buffer for file name
389 // Initialize OPENFILENAME
390 ofn.lStructSize = sizeof(OPENFILENAME);
391 ofn.hwndOwner = pFrame->m_hWnd;
392 ofn.lpstrFile = szFile;
393 ofn.nMaxFile = _countof(szFile);
394 CString temp;
395 temp.LoadString(IDS_OPENDIFFFILETITLE);
396 if (temp.IsEmpty())
397 ofn.lpstrTitle = NULL;
398 else
399 ofn.lpstrTitle = temp;
401 ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER;
402 if( HasClipboardPatch() ) {
403 ofn.Flags |= ( OFN_ENABLETEMPLATE | OFN_ENABLEHOOK );
404 ofn.hInstance = AfxGetResourceHandle();
405 ofn.lpTemplateName = MAKEINTRESOURCE(IDD_PATCH_FILE_OPEN_CUSTOM);
406 ofn.lpfnHook = CreatePatchFileOpenHook;
409 CSelectFileFilter fileFilter(IDS_PATCHFILEFILTER);
410 ofn.lpstrFilter = fileFilter;
411 ofn.nFilterIndex = 1;
413 // Display the Open dialog box.
414 if (GetOpenFileName(&ofn)==FALSE)
416 return FALSE;
418 pFrame->m_Data.m_sDiffFile = ofn.lpstrFile;
422 if ( pFrame->m_Data.m_baseFile.GetFilename().IsEmpty() && pFrame->m_Data.m_yourFile.GetFilename().IsEmpty() )
424 int nArgs;
425 LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
426 if( NULL == szArglist )
428 TRACE("CommandLineToArgvW failed\n");
430 else
432 if ( nArgs==3 || nArgs==4 )
434 // Four parameters:
435 // [0]: Program name
436 // [1]: BASE file
437 // [2]: my file
438 // [3]: THEIR file (optional)
439 // This is the same format CAppUtils::StartExtDiff
440 // uses if %base and %mine are not set and most
441 // other diff tools use it too.
442 if ( PathFileExists(szArglist[1]) && PathFileExists(szArglist[2]) )
444 pFrame->m_Data.m_baseFile.SetFileName(szArglist[1]);
445 pFrame->m_Data.m_yourFile.SetFileName(szArglist[2]);
446 if ( nArgs == 4 && PathFileExists(szArglist[3]) )
448 pFrame->m_Data.m_theirFile.SetFileName(szArglist[3]);
452 else if (nArgs == 2)
454 // only one path specified: use it to fill the "open" dialog
455 if (PathFileExists(szArglist[1]))
457 pFrame->m_Data.m_yourFile.SetFileName(szArglist[1]);
458 pFrame->m_Data.m_yourFile.StoreFileAttributes();
463 // Free memory allocated for CommandLineToArgvW arguments.
464 LocalFree(szArglist);
467 pFrame->m_bReadOnly = !!parser.HasKey(_T("readonly"));
468 if (GetFileAttributes(pFrame->m_Data.m_yourFile.GetFilename()) & FILE_ATTRIBUTE_READONLY)
469 pFrame->m_bReadOnly = true;
470 pFrame->m_bBlame = !!parser.HasKey(_T("blame"));
471 // diffing a blame means no editing!
472 if (pFrame->m_bBlame)
473 pFrame->m_bReadOnly = true;
475 pFrame->SetWindowTitle();
477 if (parser.HasKey(_T("createunifieddiff")))
479 // user requested to create a unified diff file
480 CString origFile = parser.GetVal(_T("origfile"));
481 CString modifiedFile = parser.GetVal(_T("modifiedfile"));
482 if (!origFile.IsEmpty() && !modifiedFile.IsEmpty())
484 CString outfile = parser.GetVal(_T("outfile"));
485 if (outfile.IsEmpty())
487 CCommonAppUtils::FileOpenSave(outfile, NULL, IDS_SAVEASTITLE, IDS_COMMONFILEFILTER, false, NULL);
489 if (!outfile.IsEmpty())
491 CAppUtils::CreateUnifiedDiff(origFile, modifiedFile, outfile, false);
492 return FALSE;
497 pFrame->resolveMsgWnd = parser.HasVal(L"resolvemsghwnd") ? (HWND)parser.GetLongLongVal(L"resolvemsghwnd") : 0;
498 pFrame->resolveMsgWParam = parser.HasVal(L"resolvemsgwparam") ? (WPARAM)parser.GetLongLongVal(L"resolvemsgwparam") : 0;
499 pFrame->resolveMsgLParam = parser.HasVal(L"resolvemsglparam") ? (LPARAM)parser.GetLongLongVal(L"resolvemsglparam") : 0;
501 // The one and only window has been initialized, so show and update it
502 pFrame->ActivateFrame();
503 pFrame->ShowWindow(SW_SHOW);
504 pFrame->UpdateWindow();
505 pFrame->ShowDiffBar(!pFrame->m_bOneWay);
506 if (!pFrame->m_Data.IsBaseFileInUse() && pFrame->m_Data.m_sPatchPath.IsEmpty() && pFrame->m_Data.m_sDiffFile.IsEmpty())
508 pFrame->OnFileOpen(pFrame->m_Data.m_yourFile.InUse());
509 return TRUE;
512 int line = -2;
513 if (parser.HasVal(_T("line")))
515 line = parser.GetLongVal(_T("line"));
516 line--; // we need the index
519 return pFrame->LoadViews(line);
522 // CTortoiseMergeApp message handlers
524 void CTortoiseMergeApp::OnAppAbout()
526 CAboutDlg aboutDlg;
527 aboutDlg.DoModal();
530 UINT_PTR CALLBACK
531 CTortoiseMergeApp::CreatePatchFileOpenHook(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM /*lParam*/)
533 if(uiMsg == WM_COMMAND && LOWORD(wParam) == IDC_PATCH_TO_CLIPBOARD)
535 HWND hFileDialog = GetParent(hDlg);
537 // if there's a patchfile in the clipboard, we save it
538 // to a temporary file and tell TortoiseMerge to use that one
539 std::wstring sTempFile;
540 if (TrySavePatchFromClipboard(sTempFile))
542 CommDlg_OpenSave_SetControlText(hFileDialog, edt1, sTempFile.c_str());
543 PostMessage(hFileDialog, WM_COMMAND, MAKEWPARAM(IDOK, BM_CLICK), (LPARAM)(GetDlgItem(hDlg, IDOK)));
546 return 0;
549 int CTortoiseMergeApp::ExitInstance()
551 // Look for temporary files left around by TortoiseMerge and
552 // remove them. But only delete 'old' files
553 CTempFiles::DeleteOldTempFiles(_T("*tsm*.*"));
555 return CWinAppEx::ExitInstance();
558 bool CTortoiseMergeApp::HasClipboardPatch()
560 // check if there's a patchfile in the clipboard
561 const UINT cFormat = RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
562 if (cFormat == 0)
563 return false;
565 if (OpenClipboard(NULL) == 0)
566 return false;
568 bool containsPatch = false;
569 UINT enumFormat = 0;
572 if (enumFormat == cFormat)
574 containsPatch = true; // yes, there's a patchfile in the clipboard
576 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
577 CloseClipboard();
579 return containsPatch;
582 bool CTortoiseMergeApp::TrySavePatchFromClipboard(std::wstring& resultFile)
584 resultFile.clear();
586 UINT cFormat = RegisterClipboardFormat(_T("TSVN_UNIFIEDDIFF"));
587 if (cFormat == 0)
588 return false;
589 if (OpenClipboard(NULL) == 0)
590 return false;
592 HGLOBAL hglb = GetClipboardData(cFormat);
593 LPCSTR lpstr = (LPCSTR)GlobalLock(hglb);
595 DWORD len = GetTempPath(0, NULL);
596 std::unique_ptr<TCHAR[]> path(new TCHAR[len+1]);
597 std::unique_ptr<TCHAR[]> tempF(new TCHAR[len+100]);
598 GetTempPath (len+1, path.get());
599 GetTempFileName (path.get(), _T("tsm"), 0, tempF.get());
600 std::wstring sTempFile = std::wstring(tempF.get());
602 FILE* outFile = 0;
603 _tfopen_s(&outFile, sTempFile.c_str(), _T("wb"));
604 if (outFile != 0)
606 size_t patchlen = strlen(lpstr);
607 size_t size = fwrite(lpstr, sizeof(char), patchlen, outFile);
608 if (size == patchlen)
609 resultFile = sTempFile;
611 fclose(outFile);
613 GlobalUnlock(hglb);
614 CloseClipboard();
616 return !resultFile.empty();