Link to home
Start Free TrialLog in
Avatar of imackey
imackey

asked on

Using Strings in Win32 DLLs

I am having difficulty in using a WIN32 DLL from VB.  I am looking for code surrounding the DECLARE statement as well as any WIN32 API calls to prepare variables in memory.

Here is the C header file
extern "C" __declspec (dllexport) int
GetGenerationLog(LPCSTR strGDFileName, LPCSTR strGenLogFileName);
Avatar of 3rsrichard
3rsrichard

Here are examples from a program of mine;

In the C++ program that is the dll;

EXPORT char* CALLBACK somestuff( char* string1, int length1 ,char* string2, int length2, long a number )

In the VB program;

Private Declare Function somestuff Lib "somestuff.DLL" (ByVal Str1 As Any, ByVal StrLen1 As Integer, ByVal Str2 As Any, ByVal StrLen2 As Integer, ByVal number As Long) As String

Hope that helps.
Also, in the VC program I convert the incoming strings to C format;
      sTemp = string1;
      for( i=0; i<length1; i++)
      {
            newstr[i] = *sTemp;
            sTemp++;
      }

Hope this helps you out.
Avatar of imackey

ASKER

Unfortunately I do not have the option to change the c++ code it is a dsitributed component under strict change control.  I am looking to work with the function as currently written.

If the dll isn't set up to interface with VB then you will have a hard time.

If the dll has something like

EXPORT int CALLBACK GetGenerationLog( char* string1, char* string2)

then you would need
Private Declare Function GetGenerationLog Lib "GetGenerationLog.DLL" (ByVal Str1 As Any, ByVal Str2 As Any ) As Integer
Avatar of imackey

ASKER

Here is the export

extern "C" __declspec (dllexport) int
GetGenerationLog(LPCSTR strGDFileName, LPCSTR strGenLogFileName

Are you saying this will not be usable under VB?  
I'm not sure, you will have to test it.
MS says in one place

The dllexport storage-class modifier exports functions, data, and objects to and from a DLL. These modifiers, or attributes, explicitly define the DLL’s interface to its client, which can be the executable file or another DLL. Declaring functions as dllexport eliminates the need for a module-definition (.DEF) file, at least with respect to the specification of exported functions.

However when you build a dll in VC MS inserts the CALLBACK and wants you to use a .def file.
But if it's going to work it will be like what I wrote;
Private Declare Function GetGenerationLog Lib "GetGenerationLog.DLL" (ByVal Str1 As Any, ByVal Str2 As Any ) As Integer

You have to get the names right, but VB should complain if you get something wrong.
Once you have it declared, something like;
retv1 = GetGenerationLog(string1, string2)
should work. Or not.
3rsrichard mostly has it, but you *must* make sure the two VB strings you're passing to the function are not empty, because your program will crash if they are. The best way to do this is to do something like:

Str1 = Space$(256)
Str2 = Space$(256)

before passing them to the DLL. Incidentally, whoever wrote your C DLL didn't do his job properly, because you should always specify the length of the buffer in C--it's too easy to overrun a buffer and cause a crash otherwise.
Avatar of imackey

ASKER

A coworker has pointed out to me a thread in Deja that refers to this problem as being oriented towards the use of declspec instead of stdcall.  The thread is concluding that the declaration will work for compiled exe's that use the declspec, but not for debug.  Your suggestions with any as parameters do not work either.  It appears to be compiler oriented and declspec is not "safe" for VB use.  I appreciate the response.  I will give you the points towards the end of the week.  (send me a quick note if I forget)
ASKER CERTIFIED SOLUTION
Avatar of 3rsrichard
3rsrichard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I looked some more at my code, in the source of the dll it reads;

#define EXPORT extern "C" __declspec( dllexport )

EXPORT char* CALLBACK somestuff( char* Str1, int Len1, char* Str2, int Len2, long anumber);

The difference between that and your code is the word CALLBACK, which specifies how parameters are passed between the programs.  So it's not the declspec that is causing your problem but the CALLBACK.