我写了一个c程序,它只是创建一个SQL查询并将其复制到剪贴板。但是,程序似乎会产生内存泄漏并导致计算机崩溃。我不知道如何解决这个问题。
//copies the given string TO the clipboard
int copyStringToClipboard(char * source)
{
int ok = OpenClipboard(NULL);
if (!ok) return MY_ERROR;
/* else */
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(source)+1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, source);
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[MAX_PATH];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
using namespace std;
int main(int argc, char *argv[])
{
char tag[MAX_PATH] = "";
char startDate[MAX_PATH] = "";
char startDay[MAX_PATH] = "";
char startTime[MAX_PATH] = "";
char stopDate[MAX_PATH] = "";
char stopDay[MAX_PATH] = "";
char stopTime[MAX_PATH] = "";
char query[MAX_PATH] = "";
char *temp; //point at mins and secs
char temp2[MAX_PATH] = ""; //hold mins and secs
char addPMstart[3]="";
char addPMstop[3]="";
int morning=0;
char *slash="/";
char *hyph="-";
if(argv[1]) strcpy(tag,argv[1]);
if(argv[2])
{
strcpy(startDay,argv[2]);
strcpy(startDay,replace_str(startDay,slash,hyph)); //first hyphen
strcpy(startDay,replace_str(startDay,slash,hyph)); //second hyphen
}
if(argv[3]) strcpy(startTime,argv[3]);
if(strcmp(argv[4],"PM")==0)
{
addPMstart[0]=startTime[0];
if(startTime[1]!=':') addPMstart[1]=startTime[1];
morning=atoi(addPMstart);
morning+=12;
itoa(morning,addPMstart,10);
temp = strpbrk(startTime,":");
strcpy(temp2,temp);
strcpy(startTime,addPMstart);
strcat(startTime,temp2);
}
if(argv[5])
{
strcpy(stopDay,argv[5]);
strcpy(stopDay,replace_str(stopDay,slash,hyph)); //first hyphen
strcpy(stopDay,replace_str(stopDay,slash,hyph)); //second hyphen
}
if(argv[6]) strcpy(stopTime,argv[6]);
if(strcmp(argv[7],"PM")==0)
{
morning=0; //reinitialize
addPMstop[0]=stopTime[0];
if(stopTime[1]!=':') addPMstop[1]=stopTime[1];
morning=atoi(addPMstop);
morning+=12;
itoa(morning,addPMstop,10);
temp = strpbrk(stopTime,":");
strcpy(temp2,temp);
strcpy(stopTime,addPMstop);
strcat(stopTime,temp2);
}
strcpy(startDate,startDay);
strcat(startDate," ");
strcat(startDate,startTime);
strcpy(stopDate,stopDay);
strcat(stopDate," ");
strcat(stopDate,stopTime);
strcpy(query,"SELECT \n");
strcat(query,"convert(varchar,convert(bigint,StateTime/3600000))+':'+convert(varchar,convert(bigint,StateTime)%3600000/60000)+':'+convert(varchar,convert(bigint,StateTime)%3600000%60000/1000) \n");
strcat(query,"FROM History \n");
strcat(query,"WHERE DateTime > '");
strcat(query,startDate);
strcat(query,"' \n");
strcat(query,"AND DateTime <= '");
strcat(query,stopDate);
strcat(query,"' \n");
strcat(query,"AND TagName IN ('");
strcat(query,tag);
strcat(query,"') \n");
strcat(query,"AND wwRetrievalMode = 'ValueState' AND wwStateCalc = 'Total' AND wwCycleCount = 1 AND vValue = 1\n");
copyStringToClipboard(query);
return 0;
}