Solved

Using Strings in Win32 DLLs

Posted on 2000-03-03
9
284 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
  • 5
  • 3
9 Comments
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:imackey
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mysql vs Oracle 10 119
VB6 add a minute to the date time value 8 44
VBA color chart bars 12 64
vb6 connector to SQL Server 2 32
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now