Solved

c++ dll and Visual Basic 5.0

Posted on 1997-07-07
10
292 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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