请教高手——插入agent失败。(最好上过vckbase)
热门软件下载:
第十一期:vc中利用flash制作图声并茂的动画程序
1,我把
talk.exc ,share.dll,dot.acs 拷贝入工程目录。在资源理import过来
并在resource里面插入进来。
2,在主程序加入全局函数
int res2file(lpctstr lpname,lpctstr lptype,lpctstr )
//放在下面
3,在myagentapp::initinstance()中插入
res2file (makeintresource(idr_dot),"acs","dot.acs");
res2file (makeintresource(idr_share),"dll","share.dll");
res2file (makeintresource(idr_talk),"dll","talk.e");
handle heven =createevent(null,false,false,"lsbeven");
//用于和msagent通信。动画调用share.dll进行置信号.
process_information pi;
startupinfo si={0};
si.cb=sizeof(si);
//si.wshowwindow=sw_show;
// si.dwflags=startf_useshowwindow;
char *a =new char [255];
char *b=new char [255];
getcurrentdirectory (255,a);
cstring *str=new cstring(a);
cstring *sou =new cstring ;
////////////////////////////////////////
(*sou)=*str+cstring ("\\dot.acs");
getwindowsdirectory (b,255);
cstring *des =new cstring(b) ;
(*des)+=cstring ("\\msagent\\chars");
createdirectory (des->getbuffer (10),null);
*des+=cstring ("\\dot.acs");
//afxmessagebox (*des);
//afxmessagebox (*sou);
copyfile (sou->getbuffer (10),des->getbuffer (10),false);
*str+=cstring("\\talk.e\0");
//vb制作的开场动画。
bool fret=createprocess(null,
str->getbuffer (5),
null,
null,
false,
0,
null,
null,
&si,
&pi);
cstring mystr("对不起了!你的机器不支持这个动画!\n\n不过可以看下面的-:");
if (fret ==0)
afxmessagebox (mystr);
else{
waitforsingleobject (heven,40000);
//等待信号被置。40s超时。
closehandle(heven);
}
4,运行程序,老是说对不起了!你的机器不支持这个动画!\n\n不过可以看下。。。
到底那理错了?
///附res2file函数如下
int res2file(lpctstr lpname,lpctstr lptype,lpctstr filename)
{//将从资源写到文件;
hrsrc myres=findresource (null,lpname,lptype);
hglobal gl=loadresource (null,myres);
lpvoid lp=lockresource(gl);
handle fp= createfile(filename ,generic_write,0,null,create_always,
0,null);
if (!fp)return false;
dword a;
if (!writefile (fp,lp,sizeofresource (null,myres),&a,null))
return false;
closehandle (fp);
freeresource (gl);
return true;
}
推荐阅读
msdn上面已经讲的非常清楚了
programming the ms agent control
by santosh rao
programming an animated agent similar to the office assistant.
http://www.codeproject.com/tips/msagentctrl.asp
ms sdk
static const lpwstr kpwzcharacterold = l"\\program files\\microsoft agent\\characters\\genie.acs";
static const lptstr kpszapptitle = _t("microsoft agent samples");
extern "c" int pascal winmain(hinstance hinst,
hinstance hinstprev,
lpstr lpcmdline,
int ncmdshow) {
hresult hres;
_tchar szerror[256];
variant vpath;
bstr bszspeak;
long lcharid;
long lrequestid;
iagentex *pagentex;
iagentcharacterex *pcharacterex = null;
// initialize com
if (failed(coinitialize(null))) {
messagebox(null,
_t("there was an error initializing com."),
kpszapptitle,
mb_ok | mb_iconerror);
return -1;
}
// create an instance of the agent 2.0 server. note: by
// asking for an iagentex interface we know we will get
// at least agent 2.0. the clsid also changed between
// 1.5 and 2.0 so we know we wont get the 1.5 server.
hres = cocreateinstance(clsid_agentserver,
null,
clsctx_server,
iid_iagentex,
(lpvoid *)&pagentex);
if (failed(hres)) {
wsprintf(szerror, _t("there was an error initializing microsoft agent, code = 0x%x"), hres);
messagebox(null,
szerror,
kpszapptitle,
mb_ok | mb_iconerror | mb_topmost);
couninitialize();
return -1;
}
__try {
// first try to load the default character
variantinit(&vpath);
vpath.vt = vt_empty;
hres = pagentex->load(vpath, &lcharid, &lrequestid);
if (failed(hres)) {
// theres no default character. see if we can load the
// character from the directory used in most version 1.5
// applications.
vpath.vt = vt_bstr;
vpath.bstrval = sysallocstring(kpwzcharacterold);
if (vpath.bstrval == null) {
hres = e_outofmemory;
__leave;
}
hres = pagentex->load(vpath, &lcharid, &lrequestid);
// did we successfully load a character?
if (failed(hres))
__leave;
}
// get the iagentcharacterex interface
hres = pagentex->getcharacterex(lcharid, &pcharacterex);
if (failed(hres))
__leave;
// set the language of the character
hres = pcharacterex->setlanguageid(makelangid(lang_english, sublang_english_us));
if (failed(hres))
__leave;
// show the character. the first parameter tells microsoft
// agent to show the character by playing an animation.
hres = pcharacterex->show(false, &lrequestid);
if (failed(hres))
__leave;
// make the character speak
bszspeak = sysallocstring(l"hello world!");
hres = pcharacterex->speak(bszspeak, null, &lrequestid);
sysfreestring(bszspeak);
if (failed(hres))
__leave;
// this is a very simplistic sample. sleep for 10 seconds
// and then die.
sleep(10000);
}
__finally {
if (failed(hres)) {
wsprintf(szerror, _t("an error occurred in microsoft agent, code = 0x%x"), hres);
messagebox(null,
szerror,
kpszapptitle,
mb_ok | mb_iconerror | mb_topmost);
}
}
// clean up
if (pcharacterex) {
// release the character interface
pcharacterex->release();
// unload the character. note: releasing the character
// interface does not make the character go away. you must
// call unload.
pagentex->unload(lcharid);
}
// release the agent
pagentex->release();
variantclear(&vpath);
couninitialize();
return 0;
}
相关评论