Solved

Structure in VB

Posted on 1998-10-09
12
282 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
Comment Utility
what version are you running?
0
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
Have you downloaded the example I pointed you to?
0
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

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

Author Comment

by:hasmet
Comment Utility
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
Comment Utility
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
Comment Utility
Any questions left???
0
 
LVL 1

Author Comment

by:hasmet
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now