• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

How do I pass structures (UDT's) over a com interface

How can I pass structures over a com interface... I am looking for a good example....
0
Dak_Programmer
Asked:
Dak_Programmer
  • 11
  • 6
  • 3
1 Solution
 
jkrCommented:
0
 
Dak_ProgrammerAuthor Commented:
Yeah that works fine for vb -- but for Visual C++ it seems that it takes more work than that...

I would really appreciate a working example that works for both VB and VC++.
0
 
jkrCommented:
>>Yeah that works fine for vb

Err, scroll down - the article uses VC++...
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Dak_ProgrammerAuthor Commented:
what is pInterface in their code?
0
 
jkrCommented:
>>what is pInterface in their code?

The pointer to the COM interface.
0
 
Dak_ProgrammerAuthor Commented:
I could really use a real compilable example -- rather than just some code that is scratched in various portions in an article... hence the reason for the high point total
0
 
Dak_ProgrammerAuthor Commented:
I could really use a real compilable example -- rather than just some code that is scratched in various portions in an article... hence the reason for the high point total
0
 
BeyondWuCommented:
Please give your interface definition and structure which you want to pass.And tell me the direction of the passing, pass into the interface or return from the interface or both.
0
 
Dak_ProgrammerAuthor Commented:
ok well the project I am working on is a COM dll that handles communication over a serial port using a proprietary protocol... this protocol returns response structures..


these structures are usually of the form:

struct resp_struct
{
   byte byCommand;
   byte byError;
   byte abyReserved[6];
};

the interface functions are something of the form


HRESULT Connect(byte byAddress, byte byOption, short nRet, resp_struct * pResp);
0
 
BeyondWuCommented:
Try:
typedef     struct resp_struct
     {
       byte byCommand;
       byte byError;
       byte abyReserved[6];
     }RESP_STRUCT, * LPRESP_STRUCT;

HRESULT Connect(byte byAddress, byte byOption, short nRet, [in, out, size_is(sizeof(RESP_STRUCT))]RESP_STRUCT * pResp);
0
 
BeyondWuCommented:
You should declare your interface as a raw interface derived from IUnknown, and also create and register the proxy/stub dll.
0
 
BeyondWuCommented:
Sorry, your component is a dll, so you needn't proxy/stub.
You can also use raw interface or dual interface, Actually, you can directly wite HRESULT Connect(byte byAddress, byte byOption, short nRet, RESP_STRUCT
* pResp); because all the variables in the same process context.
0
 
Dak_ProgrammerAuthor Commented:
// method 'Connect' not emitted because of invalid return type or parameter type


I get this when I try to import my TLB file using class wizard....
0
 
Dak_ProgrammerAuthor Commented:
I also get this when I try to compile my DLL


warning MIDL2039 : interface does not conform to [oleautomation] attribute : [ Parameter 'pResp' of Procedure 'Connect' ( Interface 'Iwish' ) ]
0
 
Dak_ProgrammerAuthor Commented:
Ok well I got it work using the #import in VC++

However now I cannot pass my pResp in vb --

Can someone give me some sample code in vb on how to pass the struct into the com interface.
0
 
BeyondWuCommented:
I not very familiar with VB, you can try this:

Dim test As resp_struct
Dim testobj As IYourConnect

Set testobj = New IYourConnect

'the following Initialize the test struct
test.abyReserved(0) = 30
test.abyReserved(1) = 31
test.abyReserved(2) = 32
test.abyReserved(3) = 33
test.abyReserved(4) = 34
test.abyReserved(5) = 35
test.byCommand = 36
test.byError = 37

'Invoke the method
testobj.Connect 30, 31, 32, test

Remark:
You should first select the "Project|Reference" menu in VB,
Then in the "Available References" list select your component, then click "OK" button.

Good Luck
0
 
Dak_ProgrammerAuthor Commented:
why doesn't the following work then??

testobj.connect (30, 31, 32, test)


I get the following error

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Expected: =
---------------------------
OK   Help  
---------------------------
0
 
Dak_ProgrammerAuthor Commented:
do I need to use out, retval for one of my parameters in my idl file???


0
 
BeyondWuCommented:
1. you can't use this
testobj.connect (30, 31, 32, test)
because it is not a function, has no return value.


2. Which parameter you wanted to return?

For example:
If your function is:
HRESULT getvalue([in]int input, [out, retval]int* retval);
then in VB you can use this:
x = testobj.getvalue(30)
0
 
Dak_ProgrammerAuthor Commented:
Thanks so much for your help -- It is greatly appreciated
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now