温习DELPHI,封装26个函数
Sunday, April 23, 2006 9:24:51 AM
LeoBase.dll:此番整理、简化了一些操作字符串、文件等的函数,可能会对某些朋友有handy的方便作用吧,分享出来。
2LeoBase.rar
[函数说明]
function LAuthor: pchar; stdcall;
//返回作者姓名和联系邮箱
function LMD5(const s: pChar): pChar; stdcall;
//返回字符串的MD5值
function LFileMD5(const s: pChar): pChar; stdcall;
//返回文件的MD5值
function LEncrypt(const s, keyStr: pChar): pChar; stdcall;
//对字符串s作可逆加密,keyStr为密钥,调用第二次为解密,使用不同的密钥连续调用可多重加/解密。
function LStrIsMulti(const s: PChar): bool; stdcall;
//判断字符串是否包含多字节字符
function LStrIsIp(const S: pchar): boolean; stdcall;
//判断字符串是否有效的IP地址(格式)
function LDelPort(const S: pchar): pchar; stdcall;
//如果字符串是“domain:port”的形式且port在[0,65536)之间,则返回“domain”,否则原样返回。
function LStrIsEm(const S: pchar): boolean; stdcall;
//判断字符串是否有效的EMAIL地址(格式,宽松匹配)
function LStrIsUrl(const S: pchar): boolean; stdcall;
//判断字符串是否有效的网址(格式,宽松匹配),限定正确格式是以“http://”或“ftp://”开头。
function LAddLink(const s: pChar): pChar; stdcall;
//为字符串中的网址和EMAIL地址加上链接
function LBlockIn(const ps1, ps2: pChar): pchar; stdcall;
//返回ps2中有多少个字符包含于ps1中,并被分为多少个连续的块(字符数+','+块数),块之间不考虑顺序
function LTxtCom(const s1, s2: pChar): real; stdcall;
//返回文本相似度
function LSplit(const sub, s: pChar): tstrings; stdcall;
//返回用sub作为分隔符,对s分割的tstrings(数组)
function LReSplit(const sReSep, s: pChar): tstrings; stdcall;
//LSplit的正则表达式版
function LOccurs(const sub, s: pChar): integer; stdcall;
//返回sub在s中出现的次数
function LReOccurs(const sub, s: PChar): integer; stdcall;
//LOccurs的正则表达式版
function LSeekFile(const sPath, sFile: pChar; const seekSubDir: boolean): tstrings; stdcall;
//返回在目录sPath查找文件名特征为sFile的结果集tstrings,由seekSubDir决定是否深入子目录。
function LReReplace(const FindFor, FindIn, ReplaceWith: pChar): pChar; stdcall;
//正则表达式替换
function LReTest(const re, Str: PChar): bool; stdcall;
//用正则表达式re测试字符串Str,匹配时返回真值
function LEnAlpha(const s: pChar): boolean; stdcall;
//测试字符串是否全为英语字母
function LVersion(const exeFile: PChar): PChar; stdcall;
//返回文件版本号,不能获取版本号时返回“0.0.0.0”
function LExecute(const Target: PChar; const iMod: integer): bool; stdcall;
//执行、打开一个文件(夹),iMod可选1~4:1-正常打开,2-隐藏,3-最大化,4-最小化
function LFileToStr(const F: PChar): PChar; stdcall;
//把文件读入一个串
function LSetReg(const ext, exeFile: PChar): bool; stdcall;
//设置文件关联
function LRanName(const sPath, sPrefix, sExt: PChar; const iLen: integer): PChar; stdcall;
//返回随机且唯一的目录名或文件名,sPath-传入基准目录,sPrefix-前缀,sExt-扩展名,iLen-随机产生部分的长度,不超过32
[示例代码]
1、用C#(.net 2.0)取指定文件版本号->调用LVersion
//代码开始-->
public class LeoBase
{
[DllImport("LeoBase.dll",
EntryPoint = "LVersion",
CallingConvention = CallingConvention.StdCall)]
public static extern String LVersion(String exeFile);
}
private void button1_Click(object sender, EventArgs e)
{
openf.ShowDialog();//选择文件
button1.Text = LeoBase.LVersion(openf.FileName);
}
//<--代码结束
2、用VC++6.0 计算指定文件的MD5值->调用LFileMD5
//代码开始-->
HINSTANCE hDll;
hDll = LoadLibrary("LeoBase.dll");
if (hDll != NULL)
{
CString str;
typedef char *(_stdcall * fromLeoBase)(char *);
fromLeoBase leoFun;
leoFun=(fromLeoBase)GetProcAddress(hDll, "LFileMD5");
GetDlgItem(IDC_EDIT1)->GetWindowText(str);
GetDlgItem(IDC_EDIT2)->SetWindowText(leoFun((LPSTR)(LPCTSTR)str));
FreeLibrary(hDll);
}
//<--代码结束
3、用Delphi搜索目录->调用LSeekFile
//代码开始-->
implementation
{$R *.dfm}
function LSeekFile(const sPath, sFile: pchar; const seekSubDir: Boolean): Tstrings; stdcall;
external 'LeoBase.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
Memo1.Lines.AddStrings(LSeekFile(pchar(Edit1.Text), pchar(Edit2.Text), true));
end;
//<--代码结束
2LeoBase.rar
[函数说明]
function LAuthor: pchar; stdcall;
//返回作者姓名和联系邮箱

function LMD5(const s: pChar): pChar; stdcall;
//返回字符串的MD5值
function LFileMD5(const s: pChar): pChar; stdcall;
//返回文件的MD5值
function LEncrypt(const s, keyStr: pChar): pChar; stdcall;
//对字符串s作可逆加密,keyStr为密钥,调用第二次为解密,使用不同的密钥连续调用可多重加/解密。
function LStrIsMulti(const s: PChar): bool; stdcall;
//判断字符串是否包含多字节字符
function LStrIsIp(const S: pchar): boolean; stdcall;
//判断字符串是否有效的IP地址(格式)
function LDelPort(const S: pchar): pchar; stdcall;
//如果字符串是“domain:port”的形式且port在[0,65536)之间,则返回“domain”,否则原样返回。
function LStrIsEm(const S: pchar): boolean; stdcall;
//判断字符串是否有效的EMAIL地址(格式,宽松匹配)
function LStrIsUrl(const S: pchar): boolean; stdcall;
//判断字符串是否有效的网址(格式,宽松匹配),限定正确格式是以“http://”或“ftp://”开头。
function LAddLink(const s: pChar): pChar; stdcall;
//为字符串中的网址和EMAIL地址加上链接
function LBlockIn(const ps1, ps2: pChar): pchar; stdcall;
//返回ps2中有多少个字符包含于ps1中,并被分为多少个连续的块(字符数+','+块数),块之间不考虑顺序
function LTxtCom(const s1, s2: pChar): real; stdcall;
//返回文本相似度
function LSplit(const sub, s: pChar): tstrings; stdcall;
//返回用sub作为分隔符,对s分割的tstrings(数组)
function LReSplit(const sReSep, s: pChar): tstrings; stdcall;
//LSplit的正则表达式版
function LOccurs(const sub, s: pChar): integer; stdcall;
//返回sub在s中出现的次数
function LReOccurs(const sub, s: PChar): integer; stdcall;
//LOccurs的正则表达式版
function LSeekFile(const sPath, sFile: pChar; const seekSubDir: boolean): tstrings; stdcall;
//返回在目录sPath查找文件名特征为sFile的结果集tstrings,由seekSubDir决定是否深入子目录。
function LReReplace(const FindFor, FindIn, ReplaceWith: pChar): pChar; stdcall;
//正则表达式替换
function LReTest(const re, Str: PChar): bool; stdcall;
//用正则表达式re测试字符串Str,匹配时返回真值
function LEnAlpha(const s: pChar): boolean; stdcall;
//测试字符串是否全为英语字母
function LVersion(const exeFile: PChar): PChar; stdcall;
//返回文件版本号,不能获取版本号时返回“0.0.0.0”
function LExecute(const Target: PChar; const iMod: integer): bool; stdcall;
//执行、打开一个文件(夹),iMod可选1~4:1-正常打开,2-隐藏,3-最大化,4-最小化
function LFileToStr(const F: PChar): PChar; stdcall;
//把文件读入一个串
function LSetReg(const ext, exeFile: PChar): bool; stdcall;
//设置文件关联
function LRanName(const sPath, sPrefix, sExt: PChar; const iLen: integer): PChar; stdcall;
//返回随机且唯一的目录名或文件名,sPath-传入基准目录,sPrefix-前缀,sExt-扩展名,iLen-随机产生部分的长度,不超过32
[示例代码]
1、用C#(.net 2.0)取指定文件版本号->调用LVersion
//代码开始-->
public class LeoBase
{
[DllImport("LeoBase.dll",
EntryPoint = "LVersion",
CallingConvention = CallingConvention.StdCall)]
public static extern String LVersion(String exeFile);
}
private void button1_Click(object sender, EventArgs e)
{
openf.ShowDialog();//选择文件
button1.Text = LeoBase.LVersion(openf.FileName);
}
//<--代码结束
2、用VC++6.0 计算指定文件的MD5值->调用LFileMD5
//代码开始-->
HINSTANCE hDll;
hDll = LoadLibrary("LeoBase.dll");
if (hDll != NULL)
{
CString str;
typedef char *(_stdcall * fromLeoBase)(char *);
fromLeoBase leoFun;
leoFun=(fromLeoBase)GetProcAddress(hDll, "LFileMD5");
GetDlgItem(IDC_EDIT1)->GetWindowText(str);
GetDlgItem(IDC_EDIT2)->SetWindowText(leoFun((LPSTR)(LPCTSTR)str));
FreeLibrary(hDll);
}
//<--代码结束
3、用Delphi搜索目录->调用LSeekFile
//代码开始-->
implementation
{$R *.dfm}
function LSeekFile(const sPath, sFile: pchar; const seekSubDir: Boolean): Tstrings; stdcall;
external 'LeoBase.dll';
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
Memo1.Lines.AddStrings(LSeekFile(pchar(Edit1.Text), pchar(Edit2.Text), true));
end;
//<--代码结束
