ATL COM: Pointer passing between components

1. Urgent!

2. This deserves more points, That's the most I can give today.

3. I have a struct.
I want 3 componets (ATL objects) using it. While 1 creates it.
OBJ1 - creates and uses struct
OBJ2, OBJ3 - use the struct.
OBJ1 - tells obj2 and 3 that struct not available, and deletes it.

They all work on same machine.
1. If possible at all, how do I pass a pointer between components?
2. Would it work only with INPROC, or a local / remote would work too?
3. If this works only with inproc, how do I tell the obj2 and 3 to use this only if inproc?
4. If the answer is marshaling, practically: how do I do this with ATL?
A pointer to the MSDN is sufficient.

Thank you,  Moshe
LVL 2
mflamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
That's quite easy with inproc servers, but can also be done with out-of-process servers (even remotely). What does the struct contain?
0
basantCommented:
If you want to do it for different
apartment. You should do it Like that :

say your struct is :
struct s
{
 int a;
int b;
};

atfter Defining it a struct define a interface like this :

interface sInt
{
    void SetValue([in] s sobj);
    s GetValue();
    BOOL bIsStructPresent();
};


Now You implement this interface in a separate class.

Now when you want to pass this interface between process/hosts, COM will take care of other things.


Note : You can implement these functions as methods of Obj1 also.
whatever suits you, you can use.
0
mflamAuthor Commented:
Basant thanks,
But I still need some further info.
1. How do I know if bIsStructPresent?
2. According to your code, I'm giving a copy of the struct.
How can I give back a POINTER to the struct. (Is this possible at all?)
3. If there is a more "standard" way to do this in COM I will be glad to know.
Again: I have an object that fills in user info, and other objects that use this info, and wish to get the info as a pointer to the info struct.
(I understand that this may be impossible with com over process boundaries, and should be done via a database, but I'm not sure if I'm correct).

JKR, my struct is a set of "setup info":
 BSTR theName
 long theSize
 long theType
 BSTR theFileName
 bool theTruth
or something like that...

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

chensuCommented:
For in-process servers, you don't have to do anything. For out-of-process servers, MIDL will generate the proxy/stub code from your IDL file to do the marshaling.
0
basantCommented:
>1. How do I know if bIsStructPresent?
You can write a member fn interface.
e.g.

interface B
{
....
void MakeStructPresent();
};

and Call this memeber from Obj1.

>2. According to your code, I'm giving a copy of the struct.
>How can I give back a POINTER to the struct. (Is this possible at all?)
I have serveral points to add here.

(1 )See when you work between apartments even though you receive a copy of the struct this is done by the COM because two processes shared different address spaces.

(2) If you want pointer then
define methods like
    void SetValue([in] s* psobj);
    s* GetValue();
    BOOL bIsStructPresent();

If you want to change the methods in interface define it as in,out parameter.
e.g.
    void SetValue([in,out] s* psobj);


>3. If there is a more "standard" way to do this in COM I will be glad to know.
See my answer 1 that the structure like method arguments are copied by COM and if there is misalignment/Byte Order difference between different hosts, COM takes care for that. You problem is some way similar to passing structures in Methods.


If you don't like this approach then I can suggest something else too.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mflamAuthor Commented:
Basant, please tell me the other approach.
Chensu, what do you mean "for inproc nothing".
If I have a simple ATL object Obj2, which is the client for my ATL simple COM dll OBJ1, I call Obj1 by importing and creating it with it's smartpointer.
Now, I can what:
1. ...just add a method: getStructPointer and give it the pointer to the struct?
2. or did you mean that in the Obj2 I just use obj1's struct pointer (how?)?
3. Will getStructPointer work?
4. Will it not cause an error when I use the object in MTS or with a (forgot the term for running a dll on a different machine)?
5. If it's an EXE, what do you mean using the IDL. What do I write in the IDL, that causes it not to fail.
Please a few more words.

Thanks, Moshe
0
mflamAuthor Commented:
Basant, please tell me the other approach.
Chensu, what do you mean "for inproc nothing".
If I have a simple ATL object Obj2, which is the client for my ATL simple COM dll OBJ1, I call Obj1 by importing and creating it with it's smartpointer.
Now, I can what:
1. ...just add a method: getStructPointer and give it the pointer to the struct?
2. or did you mean that in the Obj2 I just use obj1's struct pointer (how?)?
3. Will getStructPointer work?
4. Will it not cause an error when I use the object in MTS or with a (forgot the term for running a dll on a different machine)?
5. If it's an EXE, what do you mean using the IDL. What do I write in the IDL, that causes it not to fail.
Please a few more words.

Thanks, Moshe
0
mflamAuthor Commented:
I don't understand:

"You can write a member fn interface.
e.g.

interface B
{
.....
void MakeStructPresent();
};

and Call this memeber from Obj1. "

1. What does this mean?
2. What is the implementation? (Or is none needed?)
3. How do I use this with bIsStructPresent();

Also, I added points (with the few I have) and sorry for the iterations. It's not all clear to me yet.

Thanks, Moshe
0
MaydipalleCommented:
Hi,
 I implemented using single static instance class with shered memory file some what close to your requirements. The following description may explain the answer.

Create a single instance calss with static function which returns the instance of the class.Make the object pointer is the static member of the same class.Firat time(in your OBJ1) use CreateFileMapping() API to create File mapping Object next time onwards (in your OBJ2,OBJ3)use MapViewOfFile()API maps a view of a file into the address space of the calling process.By maintaining a flag we can come to know whether it is already initialized or not.Maintaining all rece conditions is user responsibility.Hope that this will help you

Madhav
0
mflamAuthor Commented:
1. increased points...
2. MadHav, your solution seems totally
unrelated to COM.
3. Basant or chensu please comment further.
I just want to be completely sure:
Are you saying that I just define the struct pointer as a param in the IDL, and the ATL COM and IDL compiler will take care of everything else?
Including pointer marshaling if needed (in exe server) and Including direct use of pointer if it's inproc?

Thanks,  Moshe
0
chensuCommented:
If it is in-proc, the client and server are in the same process address space and passing pointers is not a problem. IDL files are usually generated by ATL. You need to write something like what basant suggests. You need to use the attributes [in], [out], [size], etc. Check out the marshaling part of some COM programming books.
0
mflamAuthor Commented:
When I try building a method so I could change the struct
I cant add the Interface because the idl cant recognize the struct
say your struct is :
struct s
{
int a;
int b;
};
After Defining it a struct define a interface like this :
interface sInt
{
      void SetValue([in] s sobj);    // I cant write this two line because they are in the idl file
      s GetValue();
                    BOOL bIsStructPresent();
};


0
mflamAuthor Commented:
chensu? jkl? madhav?
How does the IDL know of my struct?!

0
basantCommented:
You can write struct in your
Idl file too.
0
mflamAuthor Commented:
Thank you for your time.

Moshe
0
basantCommented:
Which approach you used. Did u get
my suggestion. I am sorry I could not spent much time to explain it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.