明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5087|回复: 4

求问高手,关于executeInApplicationContext函数

[复制链接]
发表于 2005-8-21 23:42:00 | 显示全部楼层 |阅读模式

void LoadTemplate(void * pData)
{
  AcApDocument* pDoc = acDocManager->curDocument();
  if (acDocManager->isApplicationContext())
  {
   acDocManager->appContextNewDocument((const char *)pData);
  }
  else
   acutPrintf("Err To Create Doc!\\n");
}

void xxxx(){

CString templPath = "";
 templPath = m_arxPath + "模板\\模板.dwt";

char * pData = templPath.GetBuffer(templPath.GetLength());
 acDocManager->executeInApplicationContext(LoadTemplate,(void *)pData);
}

这样从模板打开一个了一个新文档,但是程序环境从此好像跳出了文档环境。获取的活动文档还是前一个文档,而且还有很多问题,连arx本身都无法卸载了。我想问问大家都是怎么处理打开和关闭文档的。怎么把上下文环境还给文档。难道只能用非模态对话框来处理。docman里的东西我也看了,根据arx的帮助,appContextNewDocument只能给com或非模态对话框使用??帮忙给推荐一个方法能打开和保存文件。

 楼主| 发表于 2005-8-24 02:47:00 | 显示全部楼层

弄懂了一点。。我的整个程序工作在一个文档范围的命令里,一个命令只能操作一个文档。汗`~~~,无论怎么执行,该命令停留在前一个文档里,除非命令结束。新打开的文档只能用数据库方式处理。(能不能用windows消息再打开一个命令处理新打开的文档??)

我后来又使用了程序范围的命令。更古怪的事情发生了,只要执行程序范围的命令,当前文档的resouce就不见了,意味着点击新建菜单工具条就出现致命错误。后来我又写了反应器把文档资源压栈出栈才勉强保证不出错了。为什么arx支持程序范围命令,却不把这种命令做的好用点。

各位ARX高手,能不能指点一下我理解中的错误。新学CAD开发,问题很多。

 楼主| 发表于 2005-8-24 09:11:00 | 显示全部楼层

老外就是凶哦,原来是2004arx的bug,程序范围的命令会导致资源失效,我的解决办法比较牵强。以下是从autodesk找来的。

I am having a problem with the code created by the ObjectARX wizard.
I register a command with ACRX_CMD_SESSION. My test command does nothing, but after it runs I can do everything except open another drawing. When I close all drawings the Open Toolbar is not there. It comes up with a dialog box that says
FATAL ERROR: Unhandled Access Violation Reading 0x0002 Exception at 7c18aacah

Thanks,

Justin

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Hi Justin,
I had the same problem, here is what i get.
Regards, Petra

-----------------------------------
The problem is similar to one that was reported before for similar
behavior with appContextOpenDocument and a Change Request was logged.
However it turns out that the problem has more to do with the code
generated by the ObjectARX wizard than AutoCAD itself.

The work around is to remove the following line in the
acrxEntrypPint.cpp
ACED_ARXCOMMAND_ENTRY_AUTO(CtestnewsyncdocApp, GIPS, testnewsyncdoc,
test, ACRX_CMD_MODAL, NULL)

and then add the following in the On_kInitAppMsg():

virtual AcRx::AppRetCode On_kInitAppMsg (void *pkt) {
AcRx::AppRetCode retCode =AcRxArxApp::On_kInitAppMsg (pkt) ;
acedRegCmds->addCommand( "GIPS",
"testopensyncdoc",
"test",
ACRX_CMD_MODAL ,
GIPStestnewsyncdoc);

return (retCode) ;
}
-----------------------------------
This is a follow up to my last reply. Here is a better work around that
does not break the wizards. (thanks Cyrille)

The bug in AutoCAD is that AutoCAD assumes its own resource handle is
always set when an API is called. So AutoCAD tries to load one of its
strings or dialog templates from the ARX application resource rather than
its own resource file. This indeed fails or succeeds but with the wrong
stuff. In any case, that will result in a crash sooner or later.

In your application which does not include MFC support do the following:
**** Use ACED_ARXCOMMAND_ENTRY_AUTO(CtestnewsyncdocApp, GIPS,
testnewsyncdoc, test, ACRX_CMD_MODAL, NULL)

declare this in StdAfx.h
HINSTANCE acedGetAcadResourceInstance () ;
(it is declared in rxmfcapi.h but you cannot include that file in non MFC
application, so you need to do it by hand)

Then use the acDocManager API
static void AsdkArxProject4_MyCommand1 (void) {
acDocManager->pushResourceHandle (acedGetAcadResourceInstance ()) ;
... acDocManager->popResourceHandle () ;
}
****

I changed your project to the following and the crash is resolved:
****
//wb added this to StdAfx.h
HINSTANCE acedGetAcadResourceInstance () ;


// ----- GIPStestnewsyncdoc.testnewsyncdoc command (do not rename)
static void GIPStestnewsyncdoc(void)
{
//wb added this
acDocManager->pushResourceHandle (acedGetAcadResourceInstance ()) ;

static char pData[] = "acad.dwt";
AcApDocument* pDoc = acDocManager->curDocument();
if (pDoc) {
acutPrintf("\nCurrently in Document context : %s, Switching to
App.\n",pDoc->fileName());
acDocManager->executeInApplicationContext(newSyncDocHelper, (void
*)pData);
}

//wb added this
acDocManager->popResourceHandle () ;

}
****

If you have a project that includes MFC support then you can do the
following:
****
Restore the AutoCAD resource handle in the command implementation by
adding
AfxSetResourceHandle (acedGetAcadResourceInstance ()) to the beginning of
the command:

static void GIPS_testnewsyncdoc () {
AfxSetResourceHandle (acedGetAcadResourceInstance ()) ;
...
}
This does not break the wizard and it is compatible with the AutoCAD
2005 code fix. It is also more simple to implement.
****

Cheers,
autodesk Wayne Brill
Developer Technical Services

发表于 2009-4-28 23:07:00 | 显示全部楼层

FATAL ERROR: Unhandled Access Violation Reading 0x0002 Exception at 7c18aacah

遇到同样的问题

感谢

1、修改命令方式为ACRX_CMD_SESSION

2、换06的sdk

发表于 2009-11-21 20:37:00 | 显示全部楼层

遇到同样问题,新手没明太白您是怎么解决的?

还是这方法行不通!

acDocManager->pushResourceHandle (acedGetAcadResourceInstance ()) ;
 /*acDocManager->pushResourceHandle(_hdllInstance); */
 acDocManager->executeInApplicationContext(CreateDoc,(void *)pData);
 acDocManager->popResourceHandle () ;
 acedCommand(RTSTR,_T("ZOOM"),RTSTR,_T("E"),RTNONE);还是不显示啊!

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 12:48 , Processed in 0.154689 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表