Pointers to Structures.. 
Author Message
 Pointers to Structures..

Hi All,

I am trying to call external functions present in a dll that is  written
using 'C' from my Visual Basic program. The problem comes when a function
returns a pointer to a structure. At runtime I get and error message saying
'Bad dll calling convention'. The parameters are proper, the parameter
datatypes are proper. The onlt thing that I am not able to define is the
return type. Since the function is returning a pointer to a structure.

The following code has been written

Public Type App_Msg
    Name(10) as String
    Value (10) as String
End Type

Public Declare Function Msg_Init Lib "Msg.dll" (ByRef intDebug as Integer)
as App_Msg

Function Test()
    Dim rApp_Msg as App_Msg
    Dim intDebug as Integer
    intDebug = 0
    rApp_Msg = Msg_Init(intDebug)

End Function

This doesn't seem to be working.
Please could someone help.

Thanks
Eric



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Do you have access to the C function?  If so, can you modify it to return an
error code, i.e. Long value, and have the structure passed through as a
parameter, similar to most API functions?  No idea if it will help, but it
seems to do well in APIs.

--
Alex
Software Developer

"My mind not only wanders,
sometimes it leaves completely."



Quote:
> Hi All,

> I am trying to call external functions present in a dll that is  written
> using 'C' from my visual basic program. The problem comes when a function
> returns a pointer to a structure. At runtime I get and error message
saying
> 'Bad dll calling convention'. The parameters are proper, the parameter
> datatypes are proper. The onlt thing that I am not able to define is the
> return type. Since the function is returning a pointer to a structure.

> The following code has been written

> Public Type App_Msg
>     Name(10) as String
>     Value (10) as String
> End Type

> Public Declare Function Msg_Init Lib "Msg.dll" (ByRef intDebug as Integer)
> as App_Msg

> Function Test()
>     Dim rApp_Msg as App_Msg
>     Dim intDebug as Integer
>     intDebug = 0
>     rApp_Msg = Msg_Init(intDebug)

> End Function

> This doesn't seem to be working.
> Please could someone help.

> Thanks
> Eric



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Without seeing the C code it's hard to tell what might work, but it's a good
bet that what you show won't.  First, be sure that the C function uses the
_stdcall convention... VB requires it.

You'll probably have to Declare the C function as returning a Long (the
'integer' parameter is probably a Long as well).  Use CopyMem to copy from
the returned pointer to an array of longs, then copymem from those into byte
arrays (because they're probably ANSI), then StrConv those to Unicode VB
strings.  Also, unlike C, "Name(10) As String" in VB actually yields an
array of 11 strings, numbered 0 to 10.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com





Quote:
> Hi All,

> I am trying to call external functions present in a dll that is  written
> using 'C' from my visual basic program. The problem comes when a function
> returns a pointer to a structure. At runtime I get and error message
saying
> 'Bad dll calling convention'. The parameters are proper, the parameter
> datatypes are proper. The onlt thing that I am not able to define is the
> return type. Since the function is returning a pointer to a structure.

> The following code has been written

> Public Type App_Msg
>     Name(10) as String
>     Value (10) as String
> End Type

> Public Declare Function Msg_Init Lib "Msg.dll" (ByRef intDebug as Integer)
> as App_Msg

> Function Test()
>     Dim rApp_Msg as App_Msg
>     Dim intDebug as Integer
>     intDebug = 0
>     rApp_Msg = Msg_Init(intDebug)

> End Function

> This doesn't seem to be working.
> Please could someone help.



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..

Quote:
> Jim Mack Wrote:
>Also, unlike C, "Name(10) As String" in VB actually yields an
>array of 11 strings, numbered 0 to 10

this statement is only true if you use the statement
Option Base 0.

you could declare Option Base 1, and you will wind up with
10 entries in the array



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Dan,

Quote:
> this statement is only true if you use the statement
> Option Base 0.

> you could declare Option Base 1, and you will wind up with
> 10 entries in the array

Not to be a pain, but if you're using Option Base in your VB code, than you
need to rethink your programming standards.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please post/reply to the
newsgroup(s)

Klaus H. Probst
http://members.xoom.com/kprobst/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Dan -
   Unless something has changed since VB5 when I last looked at this, Option
Base has no effect on arrays declared within structures, as this one was.
In such cases an undeclared lower bound is always 0.

   Personally, I never use Option Base and I never rely on implicit lower
bounds.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com




Quote:
> > Jim Mack Wrote:
> >Also, unlike C, "Name(10) As String" in VB actually yields an
> >array of 11 strings, numbered 0 to 10

> this statement is only true if you use the statement
> Option Base 0.

> you could declare Option Base 1, and you will wind up with
> 10 entries in the array



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Why do you not Declare the string array like:-

      Dim myStr(1 to 10) as string

Or even better, so that C likes it, make it a byte array!
    Dim myByteArray(1 to 10) as byte

As for the 'Bad Dll Calling Convention' error, this is either because you
are passing the wrong values in or out of the function, or the C function is
not declared as _stdcall. If the latter, you can create a wrapper in C.

Quote:

>Dan -
>   Unless something has changed since VB5 when I last looked at this,
Option
>Base has no effect on arrays declared within structures, as this one was.
>In such cases an undeclared lower bound is always 0.

>   Personally, I never use Option Base and I never rely on implicit lower
>bounds.

>--
>     Jim Mack
>     MicroDexterity, Inc

>     http://www.microdexterity.com





>> > Jim Mack Wrote:
>> >Also, unlike C, "Name(10) As String" in VB actually yields an
>> >array of 11 strings, numbered 0 to 10

>> this statement is only true if you use the statement
>> Option Base 0.

>> you could declare Option Base 1, and you will wind up with
>> 10 entries in the array



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Steve -

   I think you may be replying to someone else.  In any case, without more
information all anyone can do is guess.

--
     Jim Mack
     MicroDexterity, Inc

     http://www.microdexterity.com




Quote:
> Why do you not Declare the string array like:-

>       Dim myStr(1 to 10) as string

> Or even better, so that C likes it, make it a byte array!
>     Dim myByteArray(1 to 10) as byte

> As for the 'Bad Dll Calling Convention' error, this is either because you
> are passing the wrong values in or out of the function, or the C function
is
> not declared as _stdcall. If the latter, you can create a wrapper in C.


> >Dan -
> >   Unless something has changed since VB5 when I last looked at this,
> Option
> >Base has no effect on arrays declared within structures, as this one was.
> >In such cases an undeclared lower bound is always 0.

> >   Personally, I never use Option Base and I never rely on implicit lower
> >bounds.

> >--
> >     Jim Mack
> >     MicroDexterity, Inc

> >     http://www.microdexterity.com





> >> > Jim Mack Wrote:
> >> >Also, unlike C, "Name(10) As String" in VB actually yields an
> >> >array of 11 strings, numbered 0 to 10

> >> this statement is only true if you use the statement
> >> Option Base 0.

> >> you could declare Option Base 1, and you will wind up with
> >> 10 entries in the array



Mon, 18 Mar 2002 03:00:00 GMT  
 Pointers to Structures..
Jim,

Quote:
>    I think you may be replying to someone else.  In any case, without more
> information all anyone can do is guess.

He *must* be! ;-)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Klaus H. Probst
http://members.xoom.com/kprobst/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mon, 18 Mar 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Pointer to pointer to structure / Returnvalue from API

2. DX8.DrawPrimitiveUP / Pointers to Structures ???

3. string pointer in structure for API calls

4. VB.net - Pointers of structures

5. API Returning Pointer to Structure

6. Pointer within structure?

7. sending/receiving (pointers on) structures ...

8. API: Problems with pointers and structures (maybe arrays too)

9. API Returning Pointer to Structure

10. call VC DLL with structure contains pointer of the structure to next

11. call VC DLL with structure contains pointer of the structure to next

12. Passing pointer to pointer to a structure to a WINAPI function

 

 
Powered by phpBB® Forum Software