1. 程式人生 > >C語言函式:找到字串中指定字串並替換,輸出為DLL

C語言函式:找到字串中指定字串並替換,輸出為DLL

// convert9.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "convert9.h"
#include "windows.h"
#include "stdio.h"
#include "string.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" _declspec(dllexport) int convert(char *convert, char *cont)
{
char StartFlag[5]="<";//左尖括號
char EndFlag[5]=">";//右尖括號
char flag_st[1000]="";//搜尋存放左尖括號位置
char flag_ed[1000]="";//搜尋存放右尖括號位置
char sharp_left[2]="<";
char sharp_right[2]=">";
char tmp[1000];//備份
char tmp1[1000];
char tmp2[1000];
long unsigned num_pos=0;

//備份cont
strcpy(tmp, cont);
//左尖括號替換
for (num_pos=0;num_pos < strlen(cont);)
{
strcpy(flag_st,"");
strncat(flag_st, cont+num_pos, 4);
if (strcmp(flag_st, StartFlag) != 0)
{
num_pos++;
if (num_pos > strlen(cont))
{
printf("------------------------");
printf("Start flag not found !");
printf("------------------------");
break;
}
}
else
{
strcpy(tmp1, "");//tmp1變數初始化
strncat(tmp1, cont, num_pos);//截止到"<"前的內容存入變數tmp1
strcat(tmp1, sharp_left);//tmp1連線左尖括號存入tmp1
strcpy(tmp2, cont+num_pos+4);//將"<"後的內容存入變數tmp2
strcpy(cont, tmp1);
strcat(cont, tmp2);//組合變數tmp1和tmp2內容,存入變數cont
}
}

//右邊尖括號替換
for (num_pos=0; num_pos < strlen(cont);)
{
strcpy(flag_ed, "");
strncat(flag_ed, cont+num_pos, 4);
if (strcmp(flag_ed, EndFlag) != 0)
{
num_pos++;
if (num_pos > strlen(cont))
{
printf("------------------------");
printf("End flag not found !");
printf("------------------------");
break;
}
}
else
{
strcpy(tmp1, "");//tmp1變數初始化
strncat(tmp1, cont, num_pos);//截止到"<"前的內容存入變數tmp1
strcat(tmp1, sharp_right);//tmp1連線右尖括號存入tmp1
strcpy(tmp2, cont+num_pos+4);//將"<"後的內容存入變數tmp2
strcpy(cont, tmp1);
strcat(cont, tmp2);//組合變數tmp1和tmp2內容,存入變數cont
}
}
//結果存入conv
strcpy(convert, cont);
strcpy(cont, tmp);
return 0;
}

// This is an example of an exported variable
CONVERT9_API int nConvert9=0;

// This is an example of an exported function.
CONVERT9_API int fnConvert9(void)
{
return 42;
}

// This is the constructor of a class that has been exported.
// see convert9.h for the class definition
CConvert9::CConvert9()
{
return;
}