Link to home
Start Free TrialLog in
Avatar of hasmet
hasmet

asked on

Structure in VB

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.
Avatar of TheAnswerMan
TheAnswerMan

what version are you running?
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)
Avatar of hasmet

ASKER

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.

Have you downloaded the example I pointed you to?
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

Avatar of hasmet

ASKER

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.
Mail it to mirkwood23@hotmail.com
or ICQ 5982016
Avatar of hasmet

ASKER

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
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.
Any questions left???
Avatar of hasmet

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial