Solved

VB + C = pain.dll

Posted on 1997-06-15
4
214 Views
Last Modified: 2006-11-17
I have a problem: I need to write a Windows DLL, in Borland C/C++ (v. 5.0 Win95 32-bit), and tweak it so that it can interface with Visual Basic (4.0 Professional Edition 32-bit). How do I
1). Pass arrays of integers (int, long, etc.) from VB into a function in the DLL ? If possible, I would prefer to pass them "by reference" - that is, the alterations that the C function makes in the array are reflected in the "original" array.
2). Pass arrays of user-defined types (structs, that is) from VB into a function in the C DLL (also "by reference", if possible) ?
3). Return strings from the C DLL into VB ?
4). Alter strings passed to the C DLL functions as parameters inside the DLL (from VB point of view, this is "passing strings by reference"), so that the changes do not go away when the function returns ?
5). Do all of these with dynamic memory allocation ?

Any help will be greatly appreciated... Thanks for reading this :-)
0
Comment
Question by:Sinclair
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
byang earned 100 total points
ID: 1427281
1. Just declare the parameter as byref, then pass the first element of your array to the DLL.
2. Same as 1.
3. Allocate a string large enough to hold the maximum number of characters, then pass this string BYVAL to DLL (see below). C-strings are zero-terminated, so after the DLL functions returns, truncate the returned string at the first ascii 0, for example:

    Dim str as string
    str=string(256,0) 'a zero-filled 256-character buffer
    retval=DllFunction(....,str,....)
    str=left$(str, instr(str,chr$(0))-1 )

Make sure your DLL function zero-terminate the string.

4. If you want the DLL function to receive a pointer to the starting address of a VB string, you must pass the string BYVAL, not byref. The reason is that in VB, a string is actually a handle, the value of a string is its starting address, and if you pass byref, you're actually passing the address of the VB handle.

5. Use redim statement to do dynamic memory allocation. For strings, use String$() function.
0
 
LVL 2

Author Comment

by:Sinclair
ID: 1427282
Uh, thanks for the answer, but I need some clarification before I can give you the points:
1,2). Let's say that I have
    Type Moo
      i as integer
      s as string
    end type

    And also Declare Sub Lib "mydll.dll" Foo(byref arr() as Moo)
    How would I declare Foo in C ?

5). Is it possible to allocate a VB string inside my DLL ? (since I don't know how long it will be before the DLL reads it from file)

Thanks !

0
 
LVL 3

Expert Comment

by:byang
ID: 1427283
To make things easier, use  a fixed length string inside Moo, or the C function will get a string handle, which you need to dereference to get the pointer to the actual string bytes. See the documentation in CDK for details.

In VB:
  Type Moo
  i as integer
  s as string*30
  End Type
  declare sub lib "mydll.dll" foo(byref arr as Moo)

In C:
  #pragma pack (1) //see your compiler doc for details
  typedef struct {
    int i;
    char s[30];
  } Moo;  
  #pragma pack

  void WINAPI __export foo(Moo*pmoo)
  {
  }

Don't know a way to allocate a VB string inside a C DLL, just pass in a string large enough to hold the largest possible object.

0
 
LVL 2

Author Comment

by:Sinclair
ID: 1427284
Thanks ! I will go see if it works :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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