Create a C++ lib file dll wrapper

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++.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
kwixAuthor Commented:
Thanks AlexFM for the quick reply.

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



#include "Mfsys.h"

#ifndef FSAPI
#define FSAPI __declspec(dllexport)
#define FSAPI __declspec(dllimport)
#endif // FSAPI

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

      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)

      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];

      HSOURCE hSource,
      LPCWSTR pszFile,
      LPM_FIND_DATAW pData);

      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?


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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

kwixAuthor Commented:
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

kwixAuthor Commented:
Btw, what does the "IN" and "OUT" mean on the sample header code above?
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.
kwixAuthor Commented:
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.
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.