 |

07-30-2002, 05:33 AM
|
|
|
access C structures trurned By Dlls from VB
|
I have to modify an existing VB project to use a DLL written in C and wish to invoke its various methods from by VB code. These methods return various C structures.
Is it possible to access these in a simple fashion from within VB or do I have to create equivalent Class Modules or UDT's? SHould IO be considering writting wrapper class objects for both the methods and their parameters etc and if so have you any guidelines as to the best way to go about this ?
Some of these structures are failry complex as they are composite's compromising of other structures, arrays etc etc.
It may be quicker to rewrite the whole application in a real language like Visual C++ , with which I'm quite familiar, what do you think?
|
|

07-30-2002, 07:37 AM
|
|
|
Convert C dll to COM Objects ??
|
Thought I'd post a reply to myself and see what people think of this solution.
I suppose what I should be considering is converting the C dll to a suite of COM objects as these are supposed to be language independent ... Anyone out there agree with this proposal or am I going mad ??
|
|

07-30-2002, 07:41 AM
|
 |
Ultimate Contributor
* Guru *
|
|
Join Date: Sep 2001
Location: Dublin, Ireland
Posts: 1,828
|
|
|
Converting the C dll to an ActiveX dll (in C) would certainly make integration with VB a whole heap easier.
Otherwise it's a case of creating VB equivalent strutures to the C structs and using the "RtlMoveMemory" API call (suitable aliased) to copy from the address of the c struct...
HTH,
Duncan
|
|

07-30-2002, 07:50 AM
|
|
|
|
Thanks Duncan,
I've a feeling that time constraints/ access to the dll's source code are going to force me down the route of creating VB equivalent structures. Can you point me to some documentation to help me find out what equivalence there is and how to go about this.
Angie
|
|

07-30-2002, 07:56 AM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
It really depends on what is in the structs as to whether there is
an exact VB equivalent. Some areas that will cause problems are
char arrays and unsigned numbers.
|
|

07-30-2002, 08:00 AM
|
 |
Ultimate Contributor
* Guru *
|
|
Join Date: Sep 2001
Location: Dublin, Ireland
Posts: 1,828
|
|
The definitive "how to convert C to VB" is in the book [i]The Visual Basic programmer's guide to the Win32 API[i] by Daniel Appleman
Also from the VB help file:
Quote:
The following table shows the supported data types, including storage sizes and ranges.
Data type Storage size Range
Byte 1 byte 0 to 255
Boolean 2 bytes True or False
Integer 2 bytes -32,768 to 32,767
Long
(long integer) 4 bytes -2,147,483,648 to 2,147,483,647
Single
(single-precision floating-point) 4 bytes -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values
Double
(double-precision floating-point) 8 bytes -1.79769313486232E308 to
-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Currency
(scaled integer) 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
+/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is
+/-0.0000000000000000000000000001
Date 8 bytes January 1, 100 to December 31, 9999
|
HTH,
Duncan
|
|

07-30-2002, 08:03 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
|
You might want to consider passing the Structs ByRef from VB and filling them in the C dll. VB has a hard time with returned structs. VB *can* handle unsigned types, but if the high bit is set, the number will be negative. Here is some info on common VB Datatypes:
VB
Long = Int
|
|

07-30-2002, 08:06 AM
|
 |
Obsessive OPtimizer
Administrator * Guru *
|
|
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
|
|
|
(got cut off)
VB = C
Double = Double
Single = Float
Long = Int
Integer = Signed Short
Byte = Unsigned Byte
Array = SafeArray
String = BSTR <- if passed StrPtr(mystring) or in a tlb
String = LPSTR <- if passed ByVal in your declare
|
__________________
Quis custodiet ipsos custodues.
|

07-30-2002, 08:07 AM
|
|
|
|
Thanks folks,
will take a look at both approaches before taking the plunge.
TTFN,
Angie
|
|

07-30-2002, 08:41 AM
|
|
|
|
Ouch !
Just took a hard look at the parameters returned by this DLL. For the most part the structures are compromised mostly of pointers to other structures not just primitives.
Not having access to the original source it looks like I'll have to write a wrapper dll in C in addition to a bunch of VB structures and do a heap of translation from one to the other.
Loks like I'll have to find a good book on the subject after all ..... and hit myself on the head with it for being daft enough to volunteer for this one! .
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|