Solved

VB + C = pain.dll

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now