Solved

Using Strings in Win32 DLLs

Posted on 2000-03-03
9
302 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

623 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