将指针传递给对象/方法并在另一个名称空间中使用它们? [英] Passing pointers to objects/methods and using them in another namespace?

查看:104
本文介绍了将指针传递给对象/方法并在另一个名称空间中使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用的术语不正确,请上帝帮助我.

我正在为用户在该应用程序中加载的应用程序编码COM服务器插件.
用户可以加载同一个com服务器插件的多个实例,并且它们是通过ClassFactory创建的.

我需要在同一APP的过程中从另一个监督者"插件中讨论这些插件,因此在该应用程序正在创建其对象实例的插件的Create实例中,我希望将结果发送给创建的对象指向监督者"插件的指针,以供以后调用此创建的对象的方法以及其他创建的实例方法.我可以将指针发送到监督者"插头并将其存储起来,所以这样做很有用,但是我有一些问题,因为并不是所有计划都按计划进行.首先他的代码:

在这里创建对象实例,并为其指定一个指针(pbcrshimcontrol).

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

God help me if I''m not using the right terms.

I am coding COM server plugins for an app that users load up in that app.
Users can load multiple instances of the same com server plug and they are created via ClassFactory.

I have a need to talk to these plugins from another "overseer" plugin in the same APP''s process, so in the Create instance of the plugin as the app is creating instances of it''s object I wish to send the resulting created object''s pointer to the "overseer" plugin for later calling of this created object''s methods, and it''s other created instances methods. This works in so much as I am able to send the pointers over to the "overseer" plug and store them away but I have some questions as it''s not all going as planned. First he code:

Here is where the object instance is created and a pointer to it (pbcrshimcontrol) assigned.

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

HRESULT CbcrshimcontrolFactory::CreateInstance( IUnknown* pUnkOuter, REFIID riid, void** ppv )
{
    AFX_MANAGE_STATE( AfxGetStaticModuleState() );
    // Cannot aggregate.
    if (pUnkOuter != NULL)
        return CLASS_E_NOAGGREGATION;
    // Create component. 
    Cbcrshimcontrol* const pbcrshimcontrol = new Cbcrshimcontrol;



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

在通过标准LoadLibrary()和GetProcAddress()函数获得监督者"插件的句柄之后(可以做些事情,因为监督者"插件只有一个实例),我将指针发送给新创建的对象到那个监督者"插件.

///////////////////////////////////////////////////////////////////////////////////
uReturnVal = lpfnDllFunc1((int const)pbcrshimcontrol);

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

指针到达并称为pObject
现在,我的问题是:

在overseer插件的上方,我根据需要为创建的插件的每个实例提供了这些指针.但是因为(我认为),在监督者"插件中,我处于不同的名称空间中,所以我无法使用:pObject-> method.所以我需要帮助-or-
也许我只发送了一个指向我要调用的方法的指针,但我似乎无法以任何形式满足VC 2008编译器:

uReturnVal = lpfnDllFunc1((int const)pbcrshimcontrol-> method);

哪个生产:

无法从``void(__thiscall Cbcrshimcontrol :: *)(DWORD)''转换为``int''




uReturnVal = lpfnDllFunc1((int const)pbcrshimcontrol->(DWORD)MidiOutFromMaster);

哪个生产:

''-> Cbcrshimcontrol :: Release'':左操作数具有''''类型,请使用''.''



我想要的是在不同名称空间中的监督者"插件上键入
pObject->并查看我在适当的命名空间中执行的所有方法,或者至少调用仅对一个方法的访问.

我已经把这棵树围成一圈盘旋了几天,所以我需要帮助

在此先感谢您,我希望我足够清楚.

:Ron



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

Here after getting a handle to the "overseer" plugin via standard LoadLibrary() and GetProcAddress() functions (Something I can do as there is only one instance of the "overseer" plugin), I send off the pointer to the newly created object to that "overseer" plugin.

//////////////////////////////////////////////////////////////////////////////////
uReturnVal=lpfnDllFunc1((int const)pbcrshimcontrol);

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

The pointers arrive and are called pObject
Now for my questions:

Over at the overseer plugin I have these pointers for each instance of the created plugin as desired. But because (I think), in the "overseer" plug I am in a different namespace, I cannot go: pObject->method. So I need help with that -or-
Maybe I send a pointer to just the method I wish to call but I cant seem to satisfy the VC 2008 compiler in any shape of:

uReturnVal=lpfnDllFunc1((int const)pbcrshimcontrol->method);

Which produces:

cannot convert from ''void (__thiscall Cbcrshimcontrol::* )(DWORD)'' to ''int''

Or


uReturnVal=lpfnDllFunc1((int const)pbcrshimcontrol->(DWORD)MidiOutFromMaster);

Which produces:

''->Cbcrshimcontrol::Release'' : left operand has '''' type, use ''.''



What I desire is, on the "overseer" plugin in the different namespace, to type
pObject-> and see all the methods I do over in the proper namespace or at least calling access to just one in particular.

I have been circling this tree barking up it for a couple days now so I need so help

Thanks in advance, I hope I was clear enough.

:Ron

推荐答案

不确定我完全遵循您的尝试,但是据我了解,在监督者"中您具有:
Not sure I totally follow what you are trying to do, but from what I understand, in the "overseer" you have:
void *pObject; // this actually points to a thingamabob


而您想做的是:


And what you''d like to do is:

((thingamabob *)pObject)->methodIWantToCall(some_parameter);


但是监督者"和"thingamabob"在不同的命名空间中吗?

最简单的答案是在overseer.h:


But "overseer" is in a different namespace than "thingamabob" right?

Simplest answer is in overseer.h:

#include "thingamabob.h"
((NameSpace::thingamabob *)pObject)->methodIWantToCall(some_parameter);



如果您不想让监督者完全了解thingamabob的任何知识,那么您就必须给监督者一个指向函数的指针和一个指向thingamabob的指针:



If you don''t want overseer to know anything at all about thingamabob''s then you''ll have to give overseer a pointer to a function and a pointer to a thingamabob:

void *pObject; // this is some object, don''t know what it is
void (*pFunc)(void *pObject, int param1, ...);



在thingamabob命名空间中,创建一个使用thingamabob和一些参数的C函数(不是成员函数,只是一个标准的静态C函数),然后在该函数中调用thingamabob成员函数:



In the thingamabob namespace you create a C function (not a member function -- just a standard static C function) that takes a thingamabob and some parameter(s) and in that function you call the thingamabob member function:

void myFunctionForOverseerToCall(void *pObj, int param1)
{
    ((thingamabob *)pObj)->myMemberFunction(param1);
}



Thre可能是使用指向成员函数的指针执行此操作的另一种方法,但是这种方式使事情很明显.



Thre is probably another way to do this with a pointer to a member function, but this way makes it pretty obvious what is happening.


使用运行对象可能更合适桌子;在ROT中注册每个实例,然后监督者可以查询每个实例,并通过管理界面与之交谈
It may be more appropriate to look at using the Running Object Table; register each of the instances in the ROT, then the overseer can query for each of the instances and talk to them thru an admin interface


这篇关于将指针传递给对象/方法并在另一个名称空间中使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆