这段代码错在哪?????

baozhi 2003-01-22 01:55:35
我做了一个函数用来得到系统路径
struct DEFINE_SYSTEM_PATH
{
AnsiString ProgramFilesPath;
AnsiString DeskTopPath;
AnsiString StartMenuPath;
AnsiString WindowsPath;
AnsiString RunPath;
};


bool __fastcall TDefine_Setup_Function::GetSystemPath(DEFINE_SYSTEM_PATH *SystemPath)
{
LPMALLOC ShellMalloc;
LPITEMIDLIST id;
char Dir[50];
//得到桌面路径
if(FAILED(SHGetMalloc(&ShellMalloc)))
return(false);
if(FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&id)))
return(false);
if(!SHGetPathFromIDList(id,Dir))
{
ShellMalloc->Free(id);
ShellMalloc->Release();
return(false);
}
SystemPath->DeskTopPath = Dir;
//得到程序组路径
if (FAILED(SHGetMalloc(&ShellMalloc)))
return(false);
if (FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_PROGRAMS,&id)))
return(false);
if (!SHGetPathFromIDList(id,Dir))
{
ShellMalloc->Free(id);
ShellMalloc->Release();
return(false);
}
SystemPath->StartMenuPath = Dir;
//得到开始菜单的路径
if (FAILED(SHGetMalloc(&ShellMalloc)))
return(false);
if (FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_STARTUP,&id)))
return(false);
if (!SHGetPathFromIDList(id,Dir))
{
ShellMalloc->Free(id);
ShellMalloc->Release();
return(false);
}
SystemPath->RunPath = Dir;
ShellMalloc->Free(id);
ShellMalloc->Release();
//得到WINDOWS安装路径
GetWindowsDirectory(Dir,50);
SystemPath->WindowsPath = Dir;
//得到程序组路径
TRegistry *Path = new TRegistry;
Path->RootKey = HKEY_LOCAL_MACHINE;
if (Path->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion",false))
{
SystemPath->ProgramFilesPath = Path->ReadString("ProgramFilesDir");
Path->CloseKey();
delete Path;
}
else
{
Path->CloseKey();
delete Path;
return(false);
}
ShowMessage(SystemPath->DeskTopPath);
ShowMessage(SystemPath->StartMenuPath);
ShowMessage(SystemPath->RunPath);
ShowMessage(SystemPath->ProgramFilesPath);
ShowMessage(SystemPath->WindowsPath);
return(true);

}

在WIN98上运行一点问题都没有,可是不能在WIN2000上运行,出现内存错误!!怎么改才可以在WIN98和WIN2000上同时用呀!
...全文
35 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
痞子酷 2003-01-27
  • 打赏
  • 举报
回复
我在win2000调试通过。没有发现内存问题。codeguade没有报警
rikky 2003-01-27
  • 打赏
  • 举报
回复
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
是释放内存空间啊
kingkee 2003-01-27
  • 打赏
  • 举报
回复
要获得以上路径,你可用BCB自带的一些函数就可以做到。
如:
GetCurrentDir()    //取当前程序所在路径
Application->ExeName //取当前应用程序的路径及程序名

等等。多查查帮助。
halibut 2003-01-23
  • 打赏
  • 举报
回复
UP!学习!
baozhi 2003-01-23
  • 打赏
  • 举报
回复
如果不用
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
灾如果不用这两句了用对程序有没有大的影响呀?为什么
影响在哪?
baozhi 2003-01-23
  • 打赏
  • 举报
回复
为什么我把
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
去掉后,程序就可以在WIN2000下正常运行了呢?
baozhi 2003-01-22
  • 打赏
  • 举报
回复
我根据ouygg(痞子酷)大哥的改了我的程序了,可是在WIN98正常在WIN2000还是不行呀,一样的错呀!!内存内存!!
我是这样用的,这个函数是在一个自己的类里面,我又在FORM类里面用这个类里面的这个函数,应当没问题吧!
痞子酷 2003-01-22
  • 打赏
  • 举报
回复
#include <winbase.h>
#include <windows.h>
#include <shlobj.h>
#include <registry.hpp>

void __fastcall TForm1::Button1Click(TObject *Sender)
{
DEFINE_SYSTEM_PATH SystemPath ;
ZeroMemory(&SystemPath,sizeof(SystemPath));
GetSystemPath(&SystemPath);
AnsiString str;
str.sprintf("SystemPath: \n\tProgramFilesPath:%s\n\tDeskTopPath:%s\n\tStartMenuPath:%s\n\tWindowsPath:%s\n\tRunPath:%s",
SystemPath.ProgramFilesPath.c_str(),SystemPath.DeskTopPath.c_str(),SystemPath.StartMenuPath.c_str(),SystemPath.WindowsPath.c_str(),SystemPath.RunPath.c_str());
ShowMessage(str);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::GetSystemPath(DEFINE_SYSTEM_PATH *SystemPath)
{
LPMALLOC ShellMalloc;
LPITEMIDLIST id;
char Dir[50];

ZeroMemory(&ShellMalloc,sizeof(LPMALLOC));
ZeroMemory(&id,sizeof(LPITEMIDLIST));
//得到桌面路径
if(FAILED(SHGetMalloc(&ShellMalloc)))
return(false);
if(FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOPDIRECTORY,&id)))
return(false);
if(!SHGetPathFromIDList(id,Dir))
{
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
return(false);
}
SystemPath->DeskTopPath = Dir;
//得到程序组路径
if (FAILED(SHGetMalloc(&ShellMalloc)))
return(false);
if (FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_PROGRAMS,&id)))
return(false);
if (!SHGetPathFromIDList(id,Dir))
{
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
return(false);
}
SystemPath->StartMenuPath = Dir;
//得到开始菜单的路径
if (FAILED(SHGetMalloc(&ShellMalloc)))
return(false);
if (FAILED(SHGetSpecialFolderLocation(NULL,CSIDL_STARTUP,&id)))
return(false);
if (!SHGetPathFromIDList(id,Dir))
{
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
return(false);
}
SystemPath->RunPath = Dir;
ShellMalloc->Free((void *)id);
ShellMalloc->Release();
//得到WINDOWS安装路径
GetWindowsDirectory(Dir,50);
SystemPath->WindowsPath = Dir;
//得到程序组路径
TRegistry *Path = new TRegistry;
Path->RootKey = HKEY_LOCAL_MACHINE;
if (Path->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion",false))
{
SystemPath->ProgramFilesPath = Path->ReadString("ProgramFilesDir");
Path->CloseKey();
delete Path;
}
else
{
Path->CloseKey();
delete Path;
return(false);
}
return(true);

}
baozhi 2003-01-22
  • 打赏
  • 举报
回复
不会吧!!!!我用BCB做的,前面我定义了一个结构呀,哪个结构是我自己定义的,加紧的不是BCB的就是C++要不就是API的,没问题呀
包函windows.h试试
l_clove 2003-01-22
  • 打赏
  • 举报
回复
你这是什么数据类型啊,根本调不过啊

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧