I am trying to call a C function in my VB6 application. The problem is that that function takes simple char as a parameter. Not char array pointer (char *) but regular char.

This is how it is declared:

#define FM_API extern "C" int _stdcall
FM_API NVF_SetGroupFormat(char *aMarker, char aDecSeparator, int aDigits)

It returns -1 if execution was successful and 0 if not.

At the very beginning I did what I always do - just declared it and that is it.

This was my declaration:

Declare Function SetGroupFormat Lib "C:\Libs\FM_API.dll" Alias "NVF_SetGroupFormat" (ByVal lMarker As String, ByVal lDecSeparator As String, ByVal lDigits As Integer) As Integer

But it always returned 0 (fail/false).

I thought that is something wrong with parameters I passed. I called dll author for advice and told him how I pass my parameters. This is a call of that function:

Dim lSt As Integer
lSt = SetGroupFormat("=;\n", ".", 0)

Author told me that this call is correct but he told me that my declaration is "probably" wrong, because I declared lDecSeparator As String, but it should be char.

Main problem is that char does not exist in VB6.

How do I declare and pass char to a c based Dll from a VB6 code?

1 Answers

4
Konrad Rudolph On Best Solutions

In VB6 you can use fixed-length strings for this purpose:

Declare Function SetGroupFormat Lib "C:\Libs\FM_API.dll" Alias "NVF_SetGroupFormat" ( _
    ByVal lMarker As String, _
    ByVal lDecSeparator As String * 1, _
    ByVal lDigits As Integer _
) As Integer

Admittedly it’s been a while since I used VB6, and my knowledge is a bit rusty; if the above syntax isn’t supported, then the following will definitely work:

Type SingleChar
    Value As String * 1
End Type

Declare Function SetGroupFormat Lib "C:\Libs\FM_API.dll" Alias "NVF_SetGroupFormat" ( _
    ByVal lMarker As String, _
    ByVal lDecSeparator As SingleChar, _
    ByVal lDigits As Integer _
) As Integer

Alternatively you can also use the data type Byte to represent a single 8-bit value, equivalent to char in the external API. If the call then fails, this is due to something else, unrelated to the parameter type.