Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB + C = pain.dll

Posted on 1997-06-15
4
Medium Priority
?
217 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

580 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