?
Solved

c++ dll and Visual Basic 5.0

Posted on 1997-07-07
10
Medium Priority
?
304 Views
Last Modified: 2013-11-25
I'm trying to write a dll that will work in VB 5.0.
I just tried to write a simple "hello world" function in
the dll and it worked when I called the dll from another
c++ program, but not vb.  

Basically, here is my code.

c++
------
#define DLL_EXPORT __declspec(dllexport)

extern "C"
DLL_EXPORT int TestText(char* out)
{
 strcpy(out, "Hello world");
 return 1;
}

vb 5.0 - form1
---------------------
Private Sub Command1_Click()
    Dim Text2 As String * 20
    Dim ret As Integer
   
    ret = TestText(Text2)
    MsgBox Text2
End Sub

vb5.0 module1.bas
-----------------------------
Declare Function TestText Lib "mime.dll" (ByRef sHold As String) As Long
0
Comment
Question by:ivanh
10 Comments
 
LVL 4

Expert Comment

by:md041797
ID: 1164328
Just a guess.  Basic strings and c strings are different.  Basic stores a count byte before the text.  c strings are zero delimited.
0
 

Expert Comment

by:mjkajen
ID: 1164329
Strings in VB5 are not represented as they are in Microsoft C++. In VB5, strings are stored in a structure that is invisible to the programmer called a VARIANT. The VARIANT represents a variant union, and the type in the union to represent string on the C++ side is called BSTR. See the Microsoft help on BSTR. One has to allocate and de-allocate BSTR strings as one would normal memory.

The C++ program should look something like:

DLL_EXPORT int TestText (BSTR *pbstrOut)
{
   *pbstrOut = SysAllocString (L"Hello World");
 if (*pbstrOut == NULL)
    return 0; // didn't get any space for string.
else
   return 1;
}

BSTR is a built-in Microsoft type the prefixes string with the length AND zero  byte terminates them.  There are a number of "SYS..." functions to manipulate BSTR's. SysAllocString allocates space for the argument, and does a string copy of the argument to the allocated space.

I believe your VB program should work as is.  

Good luck, MK.
0
 
LVL 1

Author Comment

by:ivanh
ID: 1164330
I tried what you said, but VB keeps telling me
  Error '49':  Bad DLL calling convention


0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 3

Expert Comment

by:rmichels
ID: 1164331
Try changing the ByRef keyword in your Declare statement for the string parameter to a ByVal.  This is how you deal with char * paramaters (and it does not pass by val, but converts strings to/from C ASCIIZ still passing ByRef)

0
 
LVL 1

Author Comment

by:ivanh
ID: 1164332
So you are saying to use my first code and just change
from byref to byval?  I tried that and still got the 49 error.
I also tried with the modified code and recieved the same 49
error.
0
 
LVL 3

Expert Comment

by:rmichels
ID: 1164333
what compiler are you using?  I know that newer microsoft compilers will dork with the C function name appended and @ and a number (I think the number of bytes on that paramater stack?). I don't have a C++ compiler here rite now, so I can't figure it out.  I think maybe the calling convention for your function may be incorrect. Sorry I can't give you an exact answer..it's been a few years since I wrote DLL's for VB and things have changed some, at least in MSVC

0
 
LVL 1

Author Comment

by:ivanh
ID: 1164334
I'm using MSVC 5.0.  
0
 

Accepted Solution

by:
Groby earned 400 total points
ID: 1164335
Error 49 indicates that you're using the wrong calling convention for your function.

Your function should not be declared extern "C" (which, by the way, should rather be __cdecl) but __pascal

If you're using VC++4.2 or above, use WINAPI instead of __pascal

So the prototype should read
DLL_EXPORT WINAPI int TestText(LPSTR *p)
or
DLL_EXPORT __pascal int TestText(LPSTR *p)

And your VB declaration should be

Declare Function TestText Lib "mime.dll" (ByVal sHold As String) As Long
Hope this helps,
  Robert
0
 
LVL 1

Author Comment

by:ivanh
ID: 1164336
Actually, the first answer was right, but Groby's answer is
really the one I was looking for.  
My real big problem was that I didn't have a def file.
I also found out out that doing a ByVal, you can use LPSTR and
if you are doing a ByRef you have to use BSTR.  But this
is what you guys demonstrated in your code, but I just didn't
make the connection until I started reading some docs.  
Thanks all for the help.  I learned a lot!
0
 
LVL 2

Expert Comment

by:unamigo
ID: 3857853
Hi, I'm trying to make a dll and I'm looking for some help with this. I tried what you say here but didn't work, this is how I declared the function in VB:

Private Declare Function copystrings Lib "O:\mypath\usuarios.dll" (ByVal Dest As String, ByVal Source As String)


This is my dll code:



//usuarios.def
LIBRARY usuarios.dll
EXPORTS
copystrings               @1





// Usuarios.cpp : Defines the entry point for the DLL application.
//

/*  
      A2CW       (LPCSTR)  -> (LPCWSTR)
      A2W        (LPCSTR)  -> (LPWSTR)
      W2CA       (LPCWSTR) -> (LPCSTR)
      W2A        (LPCWSTR) -> (LPSTR)
*/

//#include <afxpriv.h>
#include "stdafx.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <wchar.h>
#include <winerror.h>
#include <lmcons.h>
#include <lmaccess.h>
#include <lmapibuf.h>
#include <ole2.h>

#define DllExport __declspec(dllexport)

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                               )
{
    return TRUE;
}




DllExport PASCAL copystrings( char* dest, char* src )
{
//      wcscpy(dest,src);
      return 1;
}

Can you tell me what's wrong, please?

Thanks in advanced.

0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

862 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