Creating A Visual Basic 6 Module From A DLL Header File
Creating A Visual Basic 6 Module From A DLL Header File
Example:
C (.h) File:
__declspec(dllexport) int Add(int a, int b);
Example:
C(.h) File:
#define PI 3.14159
#define MAX_BYTE_VALUE 0xFF
Type C VB6
1 byte unsigned bool, unsigned char, BYTE Byte
1 byte signed char Byte
2 bytes unsigned unsigned short, WORD Integer
2 bytes signed short Integer
4 bytes unsigned unsigned int, unsigned long, UINT, DWORD Long
4 bytes signed int, long, BOOL Long
4 bytes floating point float Single
8 bytes floating point double Double
4/8 byte pointer void* Any (ByVal)
- Special cases:
1. Visual Basic 6 differentiates between functions that return a value and functions that
return void. A function that returns a value is called a function in VB, and a function that
returns void (no return value) is called a subroutine.
Example:
C (.h) File:
__declspec(dllexport) void SetLatch(int value);
3. Parameters passed as a C string (char*) should use the String data type (by value).
Example:
C (.h) File:
__declspec(dllexport) void GetName(char* name, int size);
Calling Example:
Dim name as String * 128
Call GetName(name, 128)
4. Parameters passed as an array should use a reference to the first element of the array.
Example:
C (.h) File:
__declspec(dllexport) void GetBuffer(BYTE* buffer, int size, int* bytesReturned);
Calling Example:
Dim buffer (0 to 9) as Byte
Dim size as Long
size = UBound(buffer) + 1
Call GetBuffer(buffer(0), size)
5. Parameters passed as a void pointer (void*) should use a value type of Any.
Example:
C (.h) File:
__declspec(dllexport) void SetObject(void* object);
__declspec(dllexport) void GetObject(void** object);
Note: Passing pointers can be problematic when dealing with 32-bit/64-bit systems.
IntPtr is platform dependent, meaning that it is a four byte pointer on 32-bit systems
and an eight byte pointer on a 64-bit system. A .NET application running in 64-bit mode
will not be able to load a 32-bit DLL. You must either build a separate 64-bit DLL or
modify your .NET project to only run in 32-bit mode.
} PERSON, *PPERSON;
Calling Example:
Dim person as PERSON
GetPerson(person)
person.id = 2
Call SetPerson(person)
7. Parameters passed as an array of structs should use VB6 references to User Defined
Types.
Examples:
C (.h) File:
typedef struct PERSON
{
BYTE id;
WORD month;
char name[10];
} PERSON, *PPERSON;
Public Declare Sub GetPeople Lib "StructTest.dll" (ByRef peole As PERSON, ByRef
numPeople As Long)
Public Declare Sub SetPeople Lib "StructTest.dll" (ByRef peole As PERSON, ByVal
numPeople As Long)
Calling Example:
Dim people(0 To 1) As PERSON
Dim numPeople As Long
numPeople = UBound(people) + 1
people(0).id = 1
people(0).month = 11
people(0).fname(0) = &H31
people(1).id = 2
people(1).month = 12
people(1).fname(1) = &H32
Call SetPeople(people(0), numPeople)
Call GetPeople(people(0), numPeople)