Solved

Using Strings in Win32 DLLs

Posted on 2000-03-03
9
300 Views
Last Modified: 2010-05-02
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);
0
Comment
Question by:imackey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2580703
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.
0
 

Author Comment

by:imackey
ID: 2580789
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.

0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2580878
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
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!

 

Author Comment

by:imackey
ID: 2580924
Here is the export

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

Are you saying this will not be usable under VB?  
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2581012
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.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2583099
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.
0
 

Author Comment

by:imackey
ID: 2588485
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)
0
 
LVL 3

Accepted Solution

by:
3rsrichard earned 200 total points
ID: 2588590
I'm reposting this as an answer so it doesn't get lost.
According to your procedure spec
EXPORT int CALLBACK GetGenerationLog( char* string1, char* string2)

the passed parameters need to be strings that point to files, like;
"c:\mydirectory\myfile.txt"


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.
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2592468
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.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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