We help IT Professionals succeed at work.

How can I pass a component pointer to anothor component?

MohdAsalah
MohdAsalah asked
on
Medium Priority
236 Views
Last Modified: 2008-02-01
I am developing a c++ client component witch use VB server component ,I want to build a method in c++ client component to pass VB component's pointer to use it in C++ component ,How can I do that?

Regards.
Comment
Watch Question

As long as you're in the same thread you can pass around pointers to COM object (written in VC, or VB) freely.
If you intend to pass those pointers between apartments (threads, processes) you'll need to marshal them across using CoMarshalInterThreafInterfaceInStream or the GIT.

Author

Commented:
I mean something like this:
--------------------------------------------
VB side:
myC++Component->passPointerMethod(VBcomponentInstance)

------------------------------------------
at VC side:
HRESULT passPointerMethod(VOID* p)
{
   p->VBComponentMethod();
}
------------------------------------------
Is it possible??
Well, you first said that the VB side is the server side...
The server side in COM is whatever side that implements a COM object. In this case I assume that both sides are COM object and you wish to pass interface pointers between them. If this is indeed the case then there's no problem:  pass the objects as pointers to IUnknown and they will be marshalled correctly between apartments.
But none of this void* stuff please. This is C++, not C. ;)

Author

Commented:
Can u please support a chunk of code?
The same code you're using except use a IUnknwon* instead of void*.

Again, you need both sides to be COM servers (objects).
The VB side invokes a function of the C++ side and passes a pointer to itself. The prototype of the C++ object side should be something like this:

HRESULT GetVBPointer(_VBObject *pVBObj)
{
    pVBOvj->InvokeVBMethod();
    ...
};

Author

Commented:
This will let me import the VB component in my  C++,actulay all the reason for ask this question to run away from importing a component(use #import statement)in my aplication coz I have got a compiling errors, All what I want that to use a VB component in my C++ component without have to use an #import statement, Is that possible ?
Well no. not if you want to use early binding.
You can pass an IDispatch pointer and invoke the method using Invoke - but that's late binding. It's harder to code and it runs like 100 times slower.
Why wouldn't you want to import the VB tlb into the VC project?

Author

Commented:
Actually there is many VB components that I have to import, some of these components depend on  each other when I importing it like that arrangement:
#import "VB1.dll"
#import "VB2.dll"
it gives me errors tell me that there is a pointers in the VB1.dll points to some instance in VB2.dll.
when I re-arrange the import statement like this:

#import "VB2.dll"
#import "VB1.dll"
it gives me the same result but vise versa.
So you're having importing problems - that's another issue.
The #import should work evenr for multiple VB modules. You'll probably have to tinker with it for a while until you can get it to work though. See the switches for #import (like raw_interfaces_only, rename, etc.) and try using them til you get it right. I know that I had some similar problems in the past trying to import VB modules that used other VB modules and office components.. it was a real headache.
But again, this is a seperate issue. You need to use early binding and pass a pointer between the modules using a function call.

Author

Commented:
Actually I submit this question(importing VB component) but there is no good answer so i began to find another way like this question in this page.
any way I search on using #import attribute like no_namespace but that does not work.
So can u explain to me how can I use 'raw_interfaces_only' attribute.
Regards.
I only mentioned that particular attribute as an example, I'm not saying that IT is that one you need to use.
It depends on your particular error messages, and sometimes it does take a lot of fiddling around with many attrobutes till you get it right. I don't think I can help you with that anyway. It usually takes a lot of trial and error and it's something you'll probably have to deal with yourself.

And again, it's a different question.


Explore More ContentExplore courses, solutions, and other research materials related to this topic.