Search code examples
cwindowsdllvb6idl

VB6 - How to pass Strings to a DLL written in C through a .tlb File


I'm working on a graphical interface written in VB6, where I have to call function contained in a certain DLL written in C. Because of a known limitation I had to implement a trick that allows me to load this DLL in a implicit way.

This is possible creating an IDL file, compile it with MIDL and reference the resulting .tlb file in the VB6 project.

The problem is that VB6 strings and C arrays of char do not match, so I can't pass (and get back) them to the DLL.

The prototype of the C function is:

int __stdcall myFunc(char filename_in[], char filename_out[], char ErrMsg[]);

What should I write in the IDL file and how should I call it from VB6?

Thanks.


Solution

  • Thanks to GSerg and wqw I found the solution to this problem:

    In the IDL file the char arrays should be declared as LPSTR, so the prototype of the function looks like:

    int _stdcall myFunc(LPSTR file_name_in, LPSTR file_name_out, LPSTR ErrMsg)

    note that ErrMsg is declared exactly as the other arrays, even if it will contains an output message (readable on the VB6 side).

    On the VB6 side the strings should be allocated as:

    Dim file_name_in As String * 256
    Dim file_name_out As String * 256
    Dim ErrMsg As String * 256
    

    Doing so these strings are allocated with a limited size of 256, thus being compatible with the char arrays in the C DLL.

    Hope this will help someone else.

    Regards,

    G.B.