Solved

c++ dll and Visual Basic 5.0

Posted on 1997-07-07
10
290 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

16 Experts available now in Live!

Get 1:1 Help Now