Create a C++ lib file dll wrapper

Posted on 2006-04-04
Last Modified: 2008-03-10

Hello, i'm a newbie in c++ and need urgent help. I just started using VC++ a few days ago just because of the current situation i'm forced to deal with. I primarily use VB6, but because i stumbled upon a c++ lib file and header file that i need to use on vb6, i need crash course on how to create a dll wrapper for the lib file so that it's routines are callable on vb6. I don't have the source for the lib file.

On all the forums that i've read, they always say that a "wrapper" is needed but they don't even give a basic sample, a syntax specifically, on how to do it. The c++ dll that the lib is referenced to is not _stdcall so it can't be called directly from vb6, so i have to use the lib file instead.

I just need a step by step example on how to do it. let's say the lib file is named "mylibrary.lib" and the header file is named "myheader.h". How do i start? I'm using Visual C++. Pls be gentle, i'm a newbie on C++.

Question by:kwix
    LVL 48

    Expert Comment

    If myheader.h contains list of exported C-style functions, they can be used directly in VB6 using API calls.
    If myheader.h contatins exported class, it cannot be used directly. You need to write C wrapper for this class.

    class SampleClass()
        int DoSomething(int n)
           return n+1;

    C wrapper for this class:

    void* Create()
        SampleClass* p = new SampleClass();
        return (void*)p;

    void Release(void* p)
        SampleClass* p1 = (SampleClass*)p;
        delete p1;

    int DoSomething(void*p, int n)
        SampleClass* p1 = (SampleClass*)p;
        return p1->DoSomething();

    VB6 client calls Create function and keeps returned value in some variable. This is class instance pointer, for VB6 vlient this is handle. It is used as parameter for any instance call, like DoSomething. In the end class instance is released using Release function.

    Author Comment

    Thanks AlexFM for the quick reply.

    Here is the sample source code for the portion of the header file:


    #ifndef M_INC_FILESYSTEM_H
    #define M_INC_FILESYSTEM_H

    #include "Mfsys.h"

    #ifndef FSAPI
    #define FSAPI __declspec(dllexport)
    #else // defined(FILESYSTEM_EXPORTS) || defined(FILESYSTEMCOM_EXPORTS)
    #define FSAPI __declspec(dllimport)
    #endif // defined(FILESYSTEM_EXPORTS) || defined(FILESYSTEMCOM_EXPORTS)
    #endif // FSAPI

    FSAPI DWORD WINAPI fsInitLibrary (void);
    FSAPI DWORD WINAPI fsFreeLibrary (void);

    FSAPI BOOL WINAPI fsIsSupported (
          IN      HSOURCE hSource);

    FSAPI DWORD WINAPI fsPrepareHandle (
          IN      HSOURCE hSource,      // handle to data source to be prepared for data source calls
          IN      DWORD dwFlags);            // prepare flags (none is currently defined)

    FSAPI DWORD WINAPI fsMapPath2ClassW (
          IN      HSOURCE hSource,
          IN      LPCWSTR pszPath,
          IN      size_t tLength,
          OUT      LPDWORD pdwClass);

        ..... etc....

    typedef struct _M_FIND_DATAW {
          DWORD dwFileAttributes;
          FILETIME ftCreationTime;
          FILETIME ftLastAccessTime;
          FILETIME ftLastWriteTime;
          DWORD nFileSizeHigh;
          DWORD nFileSizeLow;
          WCHAR cFileName[MAX_PATH];

    FSAPI HANDLE WINAPI fsFindFirstFileW (
          HSOURCE hSource,
          LPCWSTR pszFile,
          LPM_FIND_DATAW pData);

    FSAPI BOOL WINAPI fsFindNextFileA (
          HSOURCE hSource,
          HANDLE hFind,
          LPM_FIND_DATAA pData);

        ...... etc...

    #define fsFindFirstFile                                    fsFindFirstFileW
    #define fsFindNextFile                                    fsFindNextFileW
    #define fsMapPath2Class                                    fsMapPath2ClassW
    #define fsCreateFile                                    fsCreateFileW
    #define fsDeleteFile                                    fsDeleteFileW
    #define fsCreateDirectory                              fsCreateDirectoryW
    #define fsRemoveDirectory                              fsRemoveDirectoryW
    #define fsMoveFile                                          fsMoveFileW
    #define fsCopyFile                                          fsCopyFileW
    #define fsGetFileAttributes                              fsGetFileAttributesW
    #define fsDirectoryMemInfo                              fsDirectoryMemInfoW


    How will the wrapper wrap this up?


    LVL 48

    Accepted Solution

    You don't need wrapper for this. Library exports API interface, all these functions can be called from VB6 directly using API calls.
    If C++ project produces static library and not Dll, you need to convert it to Dll - create new Dll project and move all this stuff to it.
    About details of VB6 API calls you need to ask in VB forum.

    Author Comment

    You mean the dll is directly callable from vb6?

    The problem is the structures and other declarations, do i need to convert all of it to its vb6 a module? it's gonna take ages for me to convert it consodering other cryptic symbols i saw in the header file. not to mention the pointers


    Author Comment

    Btw, what does the "IN" and "OUT" mean on the sample header code above?
    LVL 48

    Expert Comment

    IN and OUT are used to make code readable, they don't produce any executable code. IN means that called fills parameter, OUT means that callee fills parameter.
    I don't think that this stuff is too complicated to call it from VB6. Ask in VB area, I am sure that there is solution.
    Notice that every function with string parameters has two versions - ASCII (A) and Unicode (W). I think that ASCII version should be used from VB.

    Author Comment

    i guess i have to rewrite the structures as enums in vb6. i was trying to avoid that by using the lib and the h file since all the varialbes and declarations are already there, i just want to call the routines.
    LVL 48

    Expert Comment

    Structures cannot be converted to enums. enum is just number. To use structures in VB6 it is necessary to declare the same structures in VB6 code an use them as parameters in API calls.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    728 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

    15 Experts available now in Live!

    Get 1:1 Help Now