msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Implement C Interface in VB

 
Post new topic   Reply to topic    msvisual.com Forum Index -> OLE
Author Message
George



Joined: 04 Oct 2007
Posts: 12

PostPosted: Wed May 11, 2005 1:28 pm    Post subject: Implement C Interface in VB Reply with quote

I am writing a Plugin Architecture where the application sends messages to
the Plugins (Com Server DLLs) and the Plugin does some work. The problem is
there are different kinds of messages so in C - I went by this definition:

HRESULT PluginMessage(UINT Message, void* Param);

this method is a part of the interface that Plugin implements. NOTE:
Parameter 2 "Param" is of type void because it can point to any type of data
struct or it could simply be a variable.

The problem is VB6, VB.Net don't support "void" data type, I know they
support the "Any" type but this type isn't available in MIDL, so I won't be
able to generate a type library.

What should I do If I want to make it implementable in VB? Would this work:

HRESULT PluginMessage(LONG Message, LONG Param).

Also note, That the Param like I said earlier could be a IN, IN/OUT or OUT
type of variable. Do you think that using above definition I would be able to
write COM Server DLLs in C#, VB.NET and VB6? If not, How can I do it?

Thanks!

Archived from group: microsoft>public>vb>com
Back to top
View user's profile Send private message
David Avsajanishvili



Joined: 04 Oct 2007
Posts: 1

PostPosted: Fri May 13, 2005 5:06 pm    Post subject: RE: Implement C Interface in VB Reply with quote

Hello, George!

The best way is to use VARIANT-type variables to pass data of different
types. And to make IN, OUT and both IN/OUT parameter, you may declare it as
"out", by reference. There is an example of declaratioin in MIDL:

[
id(MY_ID),
helpstring('My help string")
] HRESULT PluginMessage([in] UINT Message, [out] VARIANT* Param);

.... and corresponding header in C++ :

HRESULT PluginMessage(UINT Message, VARIANT* Param)

------------------------

Then you must write a switch-block to each type of Param variable in body of
the function:

switch(Param.vt)
{
case VT_I2:
...
case VT_I4:
...
...
}

To return variable of type you want - the simplest way is to use CComVariant
object, that automatically converts any type to VARIANT-type variable:

CComVariant retParam;
retParam = "sdfsdfsw"; //or retParam = 1, or retParam = (ULONG)0;
retParam.Copy(Param);

In VB you must simply call this function with only condition, that Param is
a VARIABLE (not constant) with VARIANT type:

''''''''''''''''''''''''''
' Errors:
Dim i as Integer
...
call MyPlugin.PluginMessage(Msg,i)
...
call MyPlugin.PluginMessage(Msg,"sfsdfsd")

''''''''''''''''''''''''''
'Must be:
Dim i as Integer
Dim v as Variant
...
v = i
call MyPlugin.PluginMessage(Msg,v)
...
v = "sfsdfsd"
call MyPlugin.PluginMessage(Msg,v)

It is a standard use of VARIANT-type variable and must work in any
implementation of COM-technology.

Good luck!
Back to top
View user's profile Send private message
George



Joined: 04 Oct 2007
Posts: 12

PostPosted: Mon May 16, 2005 10:52 pm    Post subject: RE: Implement C Interface in VB Reply with quote

Hi David,
Sorry for the late reply, I checked for two days and there was no
response. I thought nobody knew the solution. so I didn't check it anymore.
Today, while browsing, I just saw your reply. Thanks a lot!

Actually, its the other way around. The Plugin doesn't call
PluginMessage(...), It Implements the Interface that defines this method. And
my C++ client (which loads this Server DLL) calls this message to every once
in a while to get some work done.

You're right, I think using Variants is the only solution. But would I be
able to pass around Handles, Interface Pointers and Struct pointers to the VB
COM Dll Server this way?

Thanks



"David Avsajanishvili" wrote:

> Hello, George!
>
> The best way is to use VARIANT-type variables to pass data of different
> types. And to make IN, OUT and both IN/OUT parameter, you may declare it as
> "out", by reference. There is an example of declaratioin in MIDL:
>
> [
> id(MY_ID),
> helpstring('My help string")
> ] HRESULT PluginMessage([in] UINT Message, [out] VARIANT* Param);
>
> ... and corresponding header in C++ :
>
> HRESULT PluginMessage(UINT Message, VARIANT* Param)
>
> ------------------------
>
> Then you must write a switch-block to each type of Param variable in body of
> the function:
>
> switch(Param.vt)
> {
> case VT_I2:
> ...
> case VT_I4:
> ...
> ...
> }
>
> To return variable of type you want - the simplest way is to use CComVariant
> object, that automatically converts any type to VARIANT-type variable:
>
> CComVariant retParam;
> retParam = "sdfsdfsw"; //or retParam = 1, or retParam = (ULONG)0;
> retParam.Copy(Param);
>
> In VB you must simply call this function with only condition, that Param is
> a VARIABLE (not constant) with VARIANT type:
>
> ''''''''''''''''''''''''''
> ' Errors:
> Dim i as Integer
> ...
> call MyPlugin.PluginMessage(Msg,i)
> ...
> call MyPlugin.PluginMessage(Msg,"sfsdfsd")
>
> ''''''''''''''''''''''''''
> 'Must be:
> Dim i as Integer
> Dim v as Variant
> ...
> v = i
> call MyPlugin.PluginMessage(Msg,v)
> ...
> v = "sfsdfsd"
> call MyPlugin.PluginMessage(Msg,v)
>
> It is a standard use of VARIANT-type variable and must work in any
> implementation of COM-technology.
>
> Good luck!
Back to top
View user's profile Send private message
George



Joined: 04 Oct 2007
Posts: 12

PostPosted: Mon May 16, 2005 10:53 pm    Post subject: RE: Implement C Interface in VB Reply with quote

Hi David,
Sorry for the late reply, I checked for two days and there was no
response. I thought nobody knew the solution. so I didn't check it anymore.
Today, while browsing, I just saw your reply. Thanks a lot!

Actually, its the other way around. The Plugin doesn't call
PluginMessage(...), It Implements the Interface that defines this method. And
my C++ client (which loads this Server DLL) calls this message to every once
in a while to get some work done.

You're right, I think using Variants is the only solution. But would I be
able to pass around Handles, Interface Pointers and Struct pointers to the VB
COM Dll Server this way?

Thanks



"David Avsajanishvili" wrote:

> Hello, George!
>
> The best way is to use VARIANT-type variables to pass data of different
> types. And to make IN, OUT and both IN/OUT parameter, you may declare it as
> "out", by reference. There is an example of declaratioin in MIDL:
>
> [
> id(MY_ID),
> helpstring('My help string")
> ] HRESULT PluginMessage([in] UINT Message, [out] VARIANT* Param);
>
> ... and corresponding header in C++ :
>
> HRESULT PluginMessage(UINT Message, VARIANT* Param)
>
> ------------------------
>
> Then you must write a switch-block to each type of Param variable in body of
> the function:
>
> switch(Param.vt)
> {
> case VT_I2:
> ...
> case VT_I4:
> ...
> ...
> }
>
> To return variable of type you want - the simplest way is to use CComVariant
> object, that automatically converts any type to VARIANT-type variable:
>
> CComVariant retParam;
> retParam = "sdfsdfsw"; //or retParam = 1, or retParam = (ULONG)0;
> retParam.Copy(Param);
>
> In VB you must simply call this function with only condition, that Param is
> a VARIABLE (not constant) with VARIANT type:
>
> ''''''''''''''''''''''''''
> ' Errors:
> Dim i as Integer
> ...
> call MyPlugin.PluginMessage(Msg,i)
> ...
> call MyPlugin.PluginMessage(Msg,"sfsdfsd")
>
> ''''''''''''''''''''''''''
> 'Must be:
> Dim i as Integer
> Dim v as Variant
> ...
> v = i
> call MyPlugin.PluginMessage(Msg,v)
> ...
> v = "sfsdfsd"
> call MyPlugin.PluginMessage(Msg,v)
>
> It is a standard use of VARIANT-type variable and must work in any
> implementation of COM-technology.
>
> Good luck!
Back to top
View user's profile Send private message
Vishal More



Joined: 04 Oct 2007
Posts: 1

PostPosted: Tue May 17, 2005 10:22 am    Post subject: Re: Implement C Interface in VB Reply with quote

Hi George ,

Yes offcourse you can pass Handles, Interface Pointers and Struct
pointers to the VB
COM Dll Server.

Just make sure how are you passing the params to this Dll Server. VB by
default uses ByRef mechanism to pass params.

You can pass handle, but I dont think handles are valid across process.
It would be better if you use "DuplicateHandle" API. Interface
Pointers and Struct pointers goes AS IS.

All the best !

Regards,
VIshal

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> OLE All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group