一个弱智问题:如何将%ProgramFiles%转化为对应目录,如C:\Program Files
我在程序中要用WinExec来调用outlook,现得到命令行如下:
%ProgramFiles%\Outlook Express\msimn.exe
可是WinExec不能识别%ProgramFiles%,不知是否有简单办法能转化?(不要从注册表找%ProgramFiles%)
问题点数:10、回复次数:15Top
1 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-05 15:31:00 得分 0
关注!这个目录"\\Program Files"是可以求出的,
String GetProgDir()
{
char buf[256];
GetWindowsDirectory(buf,sizeof(buf));
return(ExtractFileDrive(buf)+"\\Program Files");
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage(GetProgDir());
}
这样,行不行!
Top
2 楼SuperQM(乖乖龙)回复于 2001-02-05 15:51:00 得分 0
首先,很感谢您的回复,可惜您可能没有看清我的问题,举例说吧:
sCommand = "%ProgramFiles%\\Outlook Express\\msimn.exe";
WinExec(sCommand.c_str(),SW_RESTORE);
是无法正确执行的,必须:
sCommand = "C:\\Program Files\\Outlook Express\\msimn.exe";
WinExec(sCommand.c_str(),SW_RESTORE);
方可;
Top
3 楼SuperQM(乖乖龙)回复于 2001-02-05 15:51:00 得分 0
首先,很感谢您的回复,可惜您可能没有看清我的问题,举例说吧:
sCommand = "%ProgramFiles%\\Outlook Express\\msimn.exe";
WinExec(sCommand.c_str(),SW_RESTORE);
是无法正确执行的,必须:
sCommand = "C:\\Program Files\\Outlook Express\\msimn.exe";
WinExec(sCommand.c_str(),SW_RESTORE);
方可;
Top
4 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-05 16:33:00 得分 0
String GetProgDir()
{
char buf[256];
GetWindowsDirectory(buf,sizeof(buf));
return(ExtractFileDrive(buf)+"\\Program Files");
}
String sCommand=GetProgDir()+"\\Outlook Express\\msimn.exe";
WinExec(sCommand.c_str(),SW_RESTORE);
Top
5 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-05 16:34:00 得分 0
这样行不行?Top
6 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-05 16:51:00 得分 0
用串函数删除掉 %Program Files%
Top
7 楼radish()回复于 2001-02-05 17:40:00 得分 0
获取桌面上对应的各个目录及WINDOWS目录
//获取控制面板等目录
void __fastcall TForm1::Button1Click(TObject *Sender)
{
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
char szDir[MAX_PATH];
// SHGetSpecialFolderLocation generates a PIDL. The memory for the PIDL
// is allocated by the shell, and should be freed using the shell
// mallocator COM object. Use SHGetMalloc to retrieve the malloc object
if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
{
// if we were able to get the shell malloc object, then
// proceed by fetching the pidl for the windows desktop
if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
CSIDL_DESKTOPDIRECTORY,
&pidl)))
{
/* CSIDL_BITBUCKET Recycle bin
CSIDL_CONTROLS Control Panel
CSIDL_DESKTOP Windows desktop
CSIDL_DESKTOPDIRECTORY Directory for the desktop
CSIDL_DRIVES My Computer
CSIDL_FONTS Fonts directory
CSIDL_NETHOOD Network Neighborhood
CSIDL_NETWORK Network Neighborhood virtual folder
CSIDL_PERSONAL My Documents
CSIDL_PRINTERS Printers
CSIDL_PROGRAMS Program groups
CSIDL_RECENT Most recent documents list
CSIDL_SENDTO Send To menu items
CSIDL_STARTMENU Taskbar Start menu items
CSIDL_STARTUP Startup directory
CSIDL_TEMPLATES Document templates
*/
// return is true if success
if(SHGetPathFromIDList(pidl, szDir))
{
// set one caption to the directory path
Label1->Caption = szDir;
}
pShellMalloc->Free(pidl);
}
pShellMalloc->Release();
}
}
//---------------------------------------------------------------------------
//获取WINDOWS及SYSTEM目录
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString WinDir;
UINT BufferSize = GetWindowsDirectory(NULL,0);
WinDir.SetLength(BufferSize+1);
GetWindowsDirectory(WinDir.c_str(),BufferSize+1);
Label2->Caption = WinDir;
AnsiString SysDir;
BufferSize = GetSystemDirectory(NULL,0);
SysDir.SetLength(BufferSize+1);
GetSystemDirectory(SysDir.c_str(),BufferSize+1);
Label3->Caption = SysDir;
AnsiString TempDir;
BufferSize = GetTempPath(0,NULL);
TempDir.SetLength(BufferSize+1);
GetTempPath(BufferSize+1,TempDir.c_str());
Label4->Caption = TempDir;
}
Top
8 楼SuperQM(乖乖龙)回复于 2001-02-05 17:53:00 得分 0
%System%又如何?
我是说有没有一个通用的函数将这 %xxxx% 变为绝对的字串!
在windows的“运行...”中键入:“%ProgramFiles%\Outlook Express\msimn.exe”是可以的,而在WinExec()中却不能识别,windows是否提供相应的命令行识别函数?Top
9 楼VirusHuo()回复于 2001-02-05 22:42:00 得分 0
换个办法,试试shell行不行。
好像shell和在run...里面直接打命令是一样的。Top
10 楼Darkblack(bloodmud)回复于 2001-02-05 23:40:00 得分 5
有API专门展开环境变量的,好像叫:ExpandEnvironmentStringsTop
11 楼actinia(海葵)回复于 2001-02-06 00:53:00 得分 0
关注!Top
12 楼actinia(海葵)回复于 2001-02-06 01:13:00 得分 0
这个问题应该不算是“弱智”的问题吧!呵呵Top
13 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-06 08:48:00 得分 0
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *var="%temp%\\%Program Files%\\adsdasd";
char buf[1024];
ExpandEnvironmentStrings(var,buf,sizeof(buf));
ShowMessage(buf);
}
ExpandEnvironmentStrings有局限性,只能根据系统已有的环境变量
进行串替换:
如系统中有 temp=c:\win98\temp
buf的结果: c:\win98\temp\%ProgramFiles%\adsdasd
Top
14 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-06 09:13:00 得分 5
嘿嘿,应该算解决了,可以设定自已进程的局部环境变量
putenv("Program Files=c:\\Program Files"); // 这是stdlib.h中的函数
例如:
#include <stdlib.h>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
putenv("Program Files=c:\\Program Files");
putenv("MyPath=Outlook Express");
char *var="%Program Files%\\%MyPath%\\msimn.exe";
char buf[1024];
ExpandEnvironmentStrings(var,buf,sizeof(buf));
ShowMessage(buf);
}
结果buf显示: c:\Program Files\outlook express\msimn.exe
自已去发挥一下吧
Top
15 楼SuperQM(乖乖龙)回复于 2001-02-06 09:30:00 得分 0
我终于找到了该API函数:ExpandEnvironmentStringsForUser,BCB中对它定义的头文件是:userenv.h,多谢各位大侠帮助,我看“Darkblack()”和 “BCB(yhec@china.com)”二位仁兄的回答是对的,所以分数由你们平分吧。Top
16 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2001-02-06 09:53:00 得分 0
userenv.h中,OK,收益非浅。
分是小事,掌握技术是大事;
Top





