Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c++ dll and Visual Basic 5.0

Posted on 1997-07-07
10
Medium Priority
?
303 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

618 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