Solved

VB + C = pain.dll

Posted on 1997-06-15
4
197 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
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…

773 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