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
Solved

VB + C = pain.dll

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

790 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