Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > access C structures trurned By Dlls from VB


Reply
 
Thread Tools Display Modes
  #1  
Old 07-30-2002, 05:33 AM
gormless
Guest
 
Posts: n/a
Question 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?
Reply With Quote
  #2  
Old 07-30-2002, 07:37 AM
gormless
Guest
 
Posts: n/a
Default 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 ??
Reply With Quote
  #3  
Old 07-30-2002, 07:41 AM
Merrion's Avatar
Merrion Merrion is offline
Ultimate Contributor

* Guru *
 
Join Date: Sep 2001
Location: Dublin, Ireland
Posts: 1,828
Default

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
Reply With Quote
  #4  
Old 07-30-2002, 07:50 AM
gormless
Guest
 
Posts: n/a
Default

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
Reply With Quote
  #5  
Old 07-30-2002, 07:56 AM
Thinker Thinker is offline
Iron-Fisted Programmer

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
Default

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.
__________________
Posting Guidelines
Reply With Quote
  #6  
Old 07-30-2002, 08:00 AM
Merrion's Avatar
Merrion Merrion is offline
Ultimate Contributor

* Guru *
 
Join Date: Sep 2001
Location: Dublin, Ireland
Posts: 1,828
Default

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
Reply With Quote
  #7  
Old 07-30-2002, 08:03 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

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
Reply With Quote
  #8  
Old 07-30-2002, 08:06 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

(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.
Reply With Quote
  #9  
Old 07-30-2002, 08:07 AM
gormless
Guest
 
Posts: n/a
Default

Thanks folks,
will take a look at both approaches before taking the plunge.

TTFN,
Angie
Reply With Quote
  #10  
Old 07-30-2002, 08:41 AM
gormless
Guest
 
Posts: n/a
Default

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! .
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->