// 获得计算机名
AnsiString GetComputerName()
{
char name[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
if(GetComputerName(name,&size))
return AnsiString(name);
return "";
}
// 获得当前用户名
AnsiString GetUserName()
{
char username[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
if(GetUserName(username,&size))
return AnsiString(username);
return "";
}
// Windows 文件夹
AnsiString GetWindowsDir()
{
String path;
path.SetLength(MAX_PATH);
path.SetLength(GetWindowsDirectory(path.c_str(),path.Length()));
return path;
}
// System 文件夹
AnsiString GetSystemDir()
{
String path;
path.SetLength(MAX_PATH);
path.SetLength(GetSystemDirectory(path.c_str(),path.Length()));
return path;
}
// Temp 文件夹
AnsiString GetTempDir()
{
String path;
path.SetLength(MAX_PATH);
path.SetLength(GetTempPath(MAX_PATH,path.c_str()));
return path;
}
// 当前文件夹
AnsiString GetCurrDir()
{
String path;
path.SetLength(MAX_PATH);
path.SetLength(GetCurrentDirectory(MAX_PATH,path.c_str()));
return path;
// return ExtractFilePath(Application->ExeName);
// return ExtractFilePath(ParamStr(0));
}
void Jpg2Bmp(String JpgFile, String BmpFile) //将Jpg文件转换为Bmp文件
{
TJPEGImage *MyJPEG = new TJPEGImage;
try
{
MyJPEG->LoadFromFile(JpgFile); //图片位置
Graphics::TBitmap *MyBMP = new Graphics::TBitmap;
MyBMP->Assign(MyJPEG);
MyBMP->SaveToFile(BmpFile); //保存路径
delete MyBMP;
}
__finally
{
delete MyJPEG;
}
}
void Bmp2Jpg(String BmpName, String JpgName) //将bmp文件转换为jpg文件
{
Graphics::TBitmap *MyBMP = new Graphics::TBitmap;
try
{
MyBMP->LoadFromFile(BmpName); //图片位置
TJPEGImage *MyJPEG = new TJPEGImage;
MyJPEG->Assign(MyBMP);
MyJPEG->CompressionQuality = 60; //压缩比例 1..100
MyJPEG->Compress();
MyJPEG->SaveToFile(JpgName); //保存路径
delete MyJPEG;
}
__finally
{
delete MyBMP;
}
}
// 开机后自动运行程序设置
#include <Registry.hpp>
void __fastcall AutoRunFromStart(bool Set,AnsiString Title, AnsiString ExeFile)
{
TRegistry *Reg;
Reg = new TRegistry();
Reg->RootKey = HKEY_LOCAL_MACHINE;
if(Set)
{
if(Reg->OpenKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run",false))
Reg->WriteString(Title,ExeFile);
delete Reg;
}
else
{
if(Reg->OpenKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run",false))
Reg->WriteString(Title,"; " + ExeFile);
}
delete Reg;
}
// 根据窗口句柄获得窗口的Caption
AnsiString GetWndCaption(HWND hWnd)
{
AnsiString strCaption;
if(hWnd)
{
int Length = (int)SendMessage(hWnd,WM_GETTEXTLENGTH,0,0);
if(Length)
{
char *buf = new char[Length+2];
buf[Length] = '/0';
buf[Length+1] = '/0';
SendMessage(hWnd,WM_GETTEXT,Length+1,(LPARAM)buf);
strCaption = AnsiString(buf);
delete buf;
}
}
return strCaption;
}
// 根据窗口句柄获得窗口的类名
AnsiString GetWndClassName(HWND hWnd)
{
char buffer[256];
GetClassName(hWnd,buffer,255);
return AnsiString(buffer);
}
// 窗口位于最上
void __fastcall SetStayOnTop(bool Set,void *Handle)
{
if(Set)
SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
else
SetWindowPos(Handle,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
}
// 是否显示在任务栏
void __fastcall ShowOnTaskbar(bool Set,void *Handle)
{
if(Set)
ShowWindow(Handle,SW_SHOW);
else
ShowWindow(Handle,SW_HIDE);
}
// 获得子网掩码
#include <Registry.hpp>
AnsiString GetSubnetMask()
{
AnsiString SubnetMask;
TRegistry *reg = new TRegistry;
DWORD Version = GetVersion();
if(Version < 0x80000000) //WindowsNT
{
reg->RootKey = HKEY_LOCAL_MACHINE;
if(reg->OpenKeyReadOnly("SYSTEM//CurrentControlSet//Services//Tcpip//Linkage"))
{
int BuffLength = reg->GetDataSize("Bind");
char *Buff = new char[BuffLength+1];
reg->ReadBinaryData("Bind",Buff,BuffLength);
AnsiString Interface = (AnsiString)Buff;
Interface = Interface.SubString(9,Interface.Length()-8);
delete [] Buff;
reg->CloseKey();
if(reg->OpenKeyReadOnly("SYSTEM//CurrentControlSet//Services//"+Interface+"//Parameters//Tcpip"))
{
BuffLength = reg->GetDataSize("SubnetMask");
Buff = new char[BuffLength+1];
reg->ReadBinaryData("SubnetMask",Buff,BuffLength);
SubnetMask = (AnsiString)Buff;
delete [] Buff;
}
}
}
else //Windows9X
{
int i;
reg->RootKey = HKEY_LOCAL_MACHINE;
TStringList *ent = new TStringList;
if(reg->OpenKeyReadOnly("System//CurrentControlSet//Services//Class//NetTrans"))
reg->GetKeyNames(ent);
reg->CloseKey();
for(i=0;i<ent->Count ;i++)
{
reg->CloseKey();
if(reg->OpenKeyReadOnly("System//CurrentControlSet//Services//Class//NetTrans//" +ent->Strings[i]))
{
AnsiString ip = reg->ReadString("IPAddress");
AnsiString node = reg->ReadString("NodeType");
if(ip != "0.0.0.0" && ip != "" && node == "1")
{
SubnetMask = reg->ReadString("IPMask");
if(SubnetMask != "" && SubnetMask != "0.0.0.0")
break;
}
}
}
delete ent;
}
delete reg;
return SubnetMask;
}
include<winsock.h>
AnsiString GetLocalIP()
{
//Start up WinSock
WORD wVersionRequested = MAKEWORD(1,1);;
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
hostent *p;
char s[128];
char *p2;
gethostname(s,128);//获取指定计算机的名字
p = gethostbyname(s);
p2 = inet_ntoa(*((in_addr *)p->h_addr)); //获取指定计算机的IP地址
WSACleanup();
return p2;
}
// 打开、关闭光驱
#include "mmsystem.h"
void CDRomOpen(BOOL bOpenDrive,TCHAR cDrive)
{
MCI_OPEN_PARMS open;
MCI_STATUS_PARMS status;
DWORD flags;
TCHAR szDriveName[4];
strcpy(szDriveName,"?:");
::ZeroMemory(&open,sizeof(MCI_OPEN_PARMS));
open.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
szDriveName[0] = cDrive;
open.lpstrElementName = szDriveName;
flags = MCI_OPEN_TYPE + MCI_OPEN_TYPE_ID + MCI_OPEN_ELEMENT + MCI_OPEN_SHAREABLE;
if(!mciSendCommand(0,MCI_OPEN,flags,(unsigned long)&open))
{
status.dwItem = MCI_STATUS_READY;
if(bOpenDrive)
mciSendCommand(open.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
else
mciSendCommand(open.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);
mciSendCommand(open.wDeviceID,MCI_CLOSE,MCI_WAIT,0);
}
}
// 获得端口
#include <Registry.hpp>
void __fastcall GetPort(TStrings *List)
{
TRegistry *reg = new TRegistry;
reg->RootKey = HKEY_LOCAL_MACHINE;
reg->OpenKey("HARDWARE//DEVICEMAP//SERIALCOMM",true);
reg->GetValueNames(List);
List->BeginUpdate();
for(int i=0;i<List->Count;i++)
List->Strings[i] = reg->ReadString(List->Strings[i]);
List->EndUpdate();
delete reg;
}
// 结束一个进程
void __fastcall ProcessKill(int pPid)
{
HANDLE ps = OpenProcess(1,false,pPid);
if(ps && TerminateProcess(ps,-9))
{
MessageBox(Handle,"中止成功!","信息",MB_OK|MB_ICONINFORMATION);
}
else
{
MessageBox(Handle,"中止失败!","信息",MB_OK|MB_ICONWARNING);
}
}
String __fastcall TfrmTest::GetVersionInfo(String FileName)
{
/
函数名:GetVersionInfo
用途:返回指定文件的版本信息
/
FileName=ExtractFilePath(Application->ExeName)+FileName;
if(!FileExists(FileName))
{
//将要更新的文件不存在
return 0;
}
//首先获得版本信息资源的长度
DWORD dwHandle,InfoSize;
String strVer;
InfoSize = GetFileVersionInfoSize(FileName.c_str(),&dwHandle);
//将版本信息资源读入缓冲区
char *InfoBuf = new char[InfoSize];
GetFileVersionInfo(FileName.c_str(),0,InfoSize,InfoBuf);
//获得生成文件使用的代码页及字符集信息
char *pInfoVal;
unsigned int dwInfoValSize;
try
{
VerQueryValue(InfoBuf,"//VarFileInfo//Translation",&((void *)pInfoVal), &dwInfoValSize);
AnsiString V = "//StringFileInfo//" +IntToHex(*((unsigned short int *) pInfoVal),4) +IntToHex(*((unsigned short int *) &pInfoVal[2]),4)+ "//FileVersion";
//获得具体的版本号
VerQueryValue(InfoBuf, V.c_str(),&((void *)pInfoVal),&dwInfoValSize);
strVer=AnsiString(pInfoVal).SetLength(dwInfoValSize-1);
delete InfoBuf;
}
catch(...)
{
return 0;
}
if(strVer.Length()>10)
{
return 0;
}
String TmpStr;
//截取.
for (int i=1;i<=strVer.Length();i++)
{
if(strVer.SubString(i,1)!=".")
{
TmpStr=TmpStr+strVer.SubString(i,1);
}
else if((strVer.SubString(i,1)==".")&&(i==2))
{
TmpStr=TmpStr+strVer.SubString(i,1);
}
}
//截取,
if(strVer.Pos(",")!=0)
{
TmpStr="";
for (int i=1;i<=strVer.Length();i++)
{
if((strVer.SubString(i,1)!=",")&&(strVer.SubString(i,1)!=" "))
{
TmpStr=TmpStr+strVer.SubString(i,1);
}
else if((strVer.SubString(i,1)==",")&&(i==2))
{
TmpStr=TmpStr+".";
}
}
}
strVer=TmpStr;
return strVer;
}
The socket components are not installed by default. To use the socket components, you must install the dclsockets<.bpl> package.
To install the socket components:
Select Component > Install Packages.
In the Install Packages dialog box, click Add.
In the Add Design Package dialog, browse to C:Program Files (x86)EmbarcaderoStudio22.0bin.
Select dclsockets.bpl, and click Open.
Click OK to dismiss Install Packages dialog.
The socket components (TClientSocket and TServerSocket) are listed in the Internet category of the Toot Palette.
Note: These steps are one-time installation instructions. The socket components should be available for all future projects.