Solved

Structure in VB

Posted on 1998-10-09
12
288 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link a textbox in excel to a cell with more than 255 characters. 5 85
How to debug this code 7 61
How to Add / Edit Windows Menu 4 60
RUNRMTCMD from AS/400 12 45
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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