Solved

c++ dll and Visual Basic 5.0

Posted on 1997-07-07
10
289 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

758 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

21 Experts available now in Live!

Get 1:1 Help Now