Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Strings in Win32 DLLs

Posted on 2000-03-03
9
Medium Priority
?
306 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 800 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

730 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