两段几乎相同的代码,pascal代码运行后成功更改了资源句柄, c++代码却不行,不得其解?

kmfangxun 2006-09-20 06:11:12
代码来自cb6的Examples\apps\RichEdit,我把例子中Reinit.pas改成了C++,其它地方都没有问题,只有下面列出的代码部分运行后效果不一致。

//pascal代码,运行后成功更改了资源句柄
function SetResourceHInstance(NewInstance: Longint): LongInt;
var
CurModule: PLibModule;
begin
CurModule := LibModuleList;
Result := 0;
while CurModule <> nil do
begin
if CurModule.Instance = HInstance then
begin
if CurModule.ResInstance <> CurModule.Instance then
FreeLibrary(CurModule.ResInstance);
CurModule.ResInstance := NewInstance;
Result := NewInstance;
Exit;
end;
CurModule := CurModule.Next;
end;
end;


//c++代码,运行后资源句柄没有变化
long int __fastcall SetResourceHInstance(long int NewInstance)
{

long int Result;
TLibModule *CurModule;
CurModule = LibModuleList; //

Result = 0;
while( CurModule != NULL )
{
if (CurModule->instance ==(long) HInstance )
{
if(CurModule->resinstance != CurModule->instance)
FreeLibrary((HINSTANCE)CurModule->resinstance);

CurModule->resinstance = NewInstance;
Result = NewInstance;
break;
}
CurModule = CurModule->next;
}

return Result;

}
...全文
192 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kmfangxun 2006-09-21
  • 打赏
  • 举报
回复
搞定,呵呵,俺delphi菜鸟,请别见笑。


请 zswang 到下面也接点分,以示感谢!

http://community.csdn.net/Expert/topic/5029/5029039.xml?temp=.9412195
kmfangxun 2006-09-21
  • 打赏
  • 举报
回复
谢谢!我试试。
王集鹄 2006-09-21
  • 打赏
  • 举报
回复
struct PACKAGE TLibModuleEx
{
TLibModuleEx *next;
long Instance;
long CodeInstance;
long DataInstance;
long ResInstance;
int Reserved;
};

long int __fastcall SetResourceHInstance(long int NewInstance)
{

long int Result;
TLibModuleEx *CurModule;
(TLibModule*)CurModule = LibModuleList; //

Result = 0;
while( CurModule != NULL )
{
if (CurModule->Instance ==(long)HInstance)
{
if(CurModule->ResInstance != CurModule->Instance)
FreeLibrary((HINSTANCE)CurModule->ResInstance);
((TLibModuleEx*)CurModule)->ResInstance = NewInstance;
CurModule->ResInstance = NewInstance;
Result = NewInstance;
break;
}
CurModule = CurModule->next;
}
}


将Delphi和BCB声明比较就知道问题了
type
PLibModule = ^TLibModule;
TLibModule = record
Next: PLibModule;
Instance: LongWord;
CodeInstance: LongWord;
DataInstance: LongWord;
ResInstance: LongWord;
Reserved: Integer;
end;


struct PACKAGE TLibModule
{
TLibModule *next;
long instance;
long resinstance;
long reserved;
};

Delphi转BCB的时候遗漏TLibModule类型,汗
kmfangxun 2006-09-20
  • 打赏
  • 举报
回复
检查运行后资源句柄是否更改:


//放在c++ return Result之前
HINSTANCE Hres=(HINSTANCE)FindResourceHInstance((long)HInstance);

if(Hres==(HINSTANCE)Result)
ShowMessage("Changed");
else
ShowMessage("UnChanged");

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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