1 #include "stdafx.h"
2
3 using namespace std;
4
5 void AlertError(DWORD err)
6 {
7 LPVOID lpMsgBuf;
8 LPVOID lpDisplayBuf;
9
10 FormatMessage(
11 FORMAT_MESSAGE_ALLOCATE_BUFFER |
12 FORMAT_MESSAGE_FROM_SYSTEM |
13 FORMAT_MESSAGE_IGNORE_INSERTS,
14 NULL,
15 err,
16 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
17 (LPTSTR) &lpMsgBuf,
18 0, NULL );
19
20 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
21 strlen((char*)lpMsgBuf) + 40);
22 sprintf((char*)lpDisplayBuf,"错误码: %d\n信 息: %s", err,lpMsgBuf);
23 MessageBox(NULL, (LPCTSTR)lpDisplayBuf, "错误", MB_OK|MB_ICONERROR);
24
25 LocalFree(lpMsgBuf);
26 LocalFree(lpDisplayBuf);
27 }
28
29 void GetCommandOutput(char* CmdLine, string& strOutput, bool IncludeErr=false)
30 {
31 strOutput="";
32 SECURITY_ATTRIBUTES sa;
33 HANDLE hRead, hWrite;
34 memset(&sa,0,sizeof(sa));
35 sa.nLength=sizeof(sa);
36 sa.bInheritHandle=TRUE;
37 sa.lpSecurityDescriptor=NULL;
38 if(CreatePipe(&hRead,&hWrite,&sa,0))
39 {
40 STARTUPINFO si;
41 memset(&si,0,sizeof(si));
42 si.cb=sizeof(si);
43 si.hStdOutput=hWrite;
44 si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
45 si.hStdError=(IncludeErr?hWrite:GetStdHandle(STD_ERROR_HANDLE));
46 si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
47 si.wShowWindow=SW_HIDE;
48
49 PROCESS_INFORMATION pi;
50 if(CreateProcess(NULL,CmdLine,&sa,&sa,TRUE,CREATE_NO_WINDOW|NORMAL_PRIORITY_CLASS,
51 NULL,NULL,&si,&pi))
52 {
53 char szBuf[512];
54 DWORD dwReaded;
55 if(WaitForSingleObject(pi.hProcess,INFINITE)!=WAIT_TIMEOUT)
56 {
57 CloseHandle(hWrite);
58 while(ReadFile(hRead,szBuf,511,&dwReaded,NULL))
59 {
60 szBuf[dwReaded]='\0';
61 strOutput+=szBuf;
62 }
63 }
64 else
65 {
66 cout<<"WaitForSingleObject错误;"<<endl;
67 CloseHandle(hWrite);
68 AlertError(GetLastError());
69 }
70 CloseHandle(pi.hThread);
71 CloseHandle(pi.hProcess);
72 }
73 else
74 {
75 cout<<"建立进程错误;"<<endl;
76 AlertError(GetLastError());
77 }
78 CloseHandle(hRead);
79 }
80 else
81 {
82 cout<<"建立管道错误;"<<endl;
83 AlertError(GetLastError());
84 }
85 }
86
87 int APIENTRY WinMain(HINSTANCE hInstance,
88 HINSTANCE hPrevInstance,
89 LPSTR lpCmdLine,
90 int nCmdShow)
91 {
92 char szBuf[MAX_PATH+20];
93 wchar_t wcBuf[MAX_PATH];
94 int i, j;
95 string msg;
96
97 GetCommandOutput("svn info --xml",msg);
98 TiXmlDocument *doc = new TiXmlDocument();
99 doc->Parse(msg.c_str());
100 TiXmlNode *node;
101 node=doc->FirstChild("info");
102 if(node==NULL)
103 {
104 MessageBox(NULL,"当前目录没有被SVN","结果",MB_OK|MB_ICONINFORMATION);
105 return 0;
106 }
107 node=node->FirstChild("entry");
108 if(node==NULL)
109 {
110 MessageBox(NULL,"当前目录没有被SVN","结果",MB_OK|MB_ICONINFORMATION);
111 return 0;
112 }
113 node=node->FirstChild("wc-info");
114 if(node==NULL)
115 {
116 MessageBox(NULL,"当前目录没有被SVN","结果",MB_OK|MB_ICONINFORMATION);
117 return 0;
118 }
119 node=node->FirstChild("wcroot-abspath");
120 if(node==NULL)
121 {
122 MessageBox(NULL,"当前目录没有被SVN","结果",MB_OK|MB_ICONINFORMATION);
123 return 0;
124 }
125 strcpy(szBuf,node->ToElement()->GetText());
126 delete node;
127 delete doc;
128
129 j=strlen(szBuf);
130 i=MultiByteToWideChar(CP_UTF8,0,szBuf,j,wcBuf,MAX_PATH);
131 wcBuf[i]=static_cast<wchar_t>(0);
132 strcpy(szBuf,"svn relocate file:///");
133 j=strlen(szBuf);
134 i=WideCharToMultiByte(CP_ACP,0,wcBuf,i,&szBuf[j],MAX_PATH,NULL,NULL);
135 szBuf[i+j]=static_cast<char>(0);
136 SetCurrentDirectory(&szBuf[j]);
137
138 i+=j;
139 if((szBuf[i-1]=='\\')||(szBuf[i-1]=='/'))
140 strcat(szBuf,"svn/");
141 else
142 strcat(szBuf,"/svn/");
143 for(i=0,j=strlen(szBuf);i<j;i++)
144 {
145 if(szBuf[i]=='\\')
146 szBuf[i]='/';
147 }
148
149 GetCommandOutput(szBuf,msg,true);
150 MessageBox(NULL,msg.c_str(),"结果",MB_OK);
151
152 return 0;
153 }