• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

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

Thanks.
0
kwix
Asked:
kwix
  • 4
  • 4
1 Solution
 
AlexFMCommented:
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()
{
public:
    SampleClass();
    ~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.
0
 
kwixAuthor Commented:
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
#if defined(FILESYSTEM_EXPORTS) || defined(FILESYSTEMCOM_EXPORTS)
#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];
} M_FIND_DATAW, *LPM_FIND_DATAW;

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?

Thanks!

0
 
AlexFMCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 counterpart..in 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

0
 
kwixAuthor Commented:
Btw, what does the "IN" and "OUT" mean on the sample header code above?
0
 
AlexFMCommented:
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.
0
 
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.
0
 
AlexFMCommented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now