Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Structure in VB

Posted on 1998-10-09
12
291 Views
Last Modified: 2013-12-26
I have a component (written in VC++6.0 ATL 3.0)
that has a dual interface, and a custom interface.
custom interface has a setparcel method, that takes
a structure (two methods one with value other with reference)
here is a part from idl file

typedef struct tagPocket  
{
      long ID; // here I removed all members but one for simplicity//
} Pocket;

interface IParcelCustom : IUnknown
{
            [helpstring("method SetParcel")] HRESULT SetParcel([in] Pocket parcel);
            [helpstring("method GetParcel")] HRESULT GetParcel([out, retval] Pocket *parcel);
            [helpstring("method SetParcelByRef")] HRESULT SetParcelByRef([in] Pocket *pparcel);
      };

and dual interface has

interface IParcel : IDispatch
{

[id(1), helpstring("method GetID")] HRESULT GetID([out, retval] long* id);
[id(2), helpstring("method GetCustomInterface")] HRESULT GetCustomInterface([out, retval] IParcelCustom** ppObj);
      };
so it can return a custom interface

this works fine from a C++ client.

but from excel VBA I try to convince it that I have the same structure, my vb code is
Dim a As New Parcel
Dim b As IParcelCustom
Private Type tagPocket
    samp_t As Long
End Type

Sub test()
Set b = a.GetCustomInterface
Dim pocket As tagPocket
pocket.samp_t = 30
b.SetParcelByRef (pocket) // here is the compile error
End Sub
it says Type mismatch (Error 13)
so he does not like my structure
although he expects pparcel As tagPocket on the tooltip
as below
b.SetParcelByRef (pparcel As tagPocket)
so how can I convince the compiler that it is what it should be. ??? If I can work this I will
increase the points, nd please do not lock the question
by guessing. just put a command. thanks.
0
Comment
Question by:hasmet
  • 7
  • 4
12 Comments
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1487561
what version are you running?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1487562
Change the following:

typedef struct Pocket  
{
long ID; // here I removed all members but one for simplicity//
} Pocket;

Dim pocket As Pocket
pocket.samp_t = 30
b.SetParcelByRef (pocket) // here is the compile error

I can mail you a project that shows you how to do structs in VB.
You can also download it at http://www.worldofatl.com/LegalDownload.asp?URL=Downloads/WorksWithVB.zip
(or choose Downloads and topic structs&vb at their home page)
0
 
LVL 1

Author Comment

by:hasmet
ID: 1487563
if you are saying that tagPocket change to Pocket, that did
change anything.

I have VB6.0 AS
Private Type Pocket
    samp_t As Long
End Type
Private Function doit(p As Pocket)
MsgBox p.samp_t
End Function
Private Sub Form_Load()
Dim a As New Parcel
Dim b As IParcelCustom
Set b = a.GetCustomInterface
Dim pparcel As Pocket
pparcel.samp_t = 30
MsgBox pparcel.samp_t
Call doit(pparcel) // WORKS
Call b.SetParcelByRef(pparcel) // DOESNOT WORK
End Sub

and ATL 3.0 (VC++6.0)

IDL has
typedef struct Pocket  
{
      long samp_t;
} Pocket;

if I missed your suggestion, please let me know. I just
wanted to keep question open until It is solved for sure.

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 13

Expert Comment

by:Mirkwood
ID: 1487564
Have you downloaded the example I pointed you to?
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1487565
You can do a QueryInterface in VB by just assigning.
So IParcelCustom and IParcel are both interfaces of Parcel then the following would work.

Dim parc as new Parcel
dim parcCustom as IParcelCustom
set parcCustom = parc  ' this does a queryinterface

BTW: Is b valid? debug.assert not b is nothing

0
 
LVL 1

Author Comment

by:hasmet
ID: 1487566
the problem is with the argument, errir is Type mismatch
b is valid, and as I pointed out before,  b.SetParcelByRef(pparcel) he even shows that he expects pparcel As Pocket
exactly same as doit(pparcel). but doit works fine( since
internal) and b.SetParcelByRef(pparcel) fails to compile
gives Type mismatch error, that is odd. If you like
I can mail you a very simple ATL component, VC++ client,
and VB client. so problem is with argument as structure,
if it is just a long type, it works fine, some how structure
fails in type checking. b is valid.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1487567
Mail it to mirkwood23@hotmail.com
or ICQ 5982016
0
 
LVL 1

Author Comment

by:hasmet
ID: 1487568
this question is reserved for mirkwood to get his points.
if you have a good way to somehow load the struct info
to Excel VBA, I can open a new question for a new answer.
thanks
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1487569
Here is and example on how to create a module using IDL
dllname should point to your dll.

      [
            dllname("advapi32.dll"),
            /*uuid(3601a086-06bb-11d2-9f31-008048ec4ce4),*/
            helpstring("ModWin32Reg module")
      ]
      module ModWin32Reg {
            //
            // Predefined Value Types.
            //
            const LONG REG_SZ                    = ( 1 ); // Unicode nul terminated string
            const LONG REG_EXPAND_SZ             = ( 2 ); // Unicode nul terminated string
                                                // (with environment variable references)
            const LONG REG_DWORD                 = ( 4 ); // 32-bit number
            const LONG REG_MULTI_SZ              = ( 7 ); // Multiple Unicode strings

            //
            // Reserved Key Handles.
            //
            const LONG HKEY_CLASSES_ROOT         = (( HKEY ) 0x80000000 );
            const LONG HKEY_CURRENT_USER         = (( HKEY ) 0x80000001 );
            const LONG HKEY_LOCAL_MACHINE        = (( HKEY ) 0x80000002 );
            const LONG HKEY_USERS                = (( HKEY ) 0x80000003 );

            //
            // Error code definitions
            //
            const LONG ERROR_NONE = 0;
            const LONG ERROR_SUCCESS = 0;
            const LONG ERROR_BADDB = 1;
            const LONG ERROR_BADKEY = 2;
            const LONG ERROR_CANTOPEN = 3;
            const LONG ERROR_CANTREAD = 4;
            const LONG ERROR_CANTWRITE = 5;
            const LONG ERROR_OUTOFMEMORY = 6;
            const LONG ERROR_INVALID_PARAMETER = 7;
            const LONG ERROR_ACCESS_DENIED = 8;
            const LONG ERROR_INVALID_PARAMETERS = 87;
            const LONG ERROR_NO_MORE_ITEMS = 259;


            const LONG KEY_ALL_ACCESS = 0x3f;

            const LONG REG_OPTION_NON_VOLATILE = 0;
}

Note that the dll should now be added to the references of your project and that the dll should be available in the seachpath of your application.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1487570
Any questions left???
0
 
LVL 1

Author Comment

by:hasmet
ID: 1487571
dllname is for using exported dll functions, not ?
how am I going to use it for VBA to understand structures
or enums in my IDL ?? few more lines of explanation please,
and send it as an answer not a comment and let us conclude
this part, for now so you get your points. thanks
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 total points
ID: 1487572
Just put the types inside the module part. The module part was basically the answer to your question on how to create a module.

VBA does not now user types.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question