Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


writing DLL library in VC++ problem

Posted on 2003-02-24
Medium Priority
Last Modified: 2010-04-01
Hi, Expert,

I've done an external function as following,
char* abc()
    return "testing";

I generated the "test.dll" file for the above coding in VC++,
and I wanna get it connected with the VB by the following coding,

Declare Function abc lib "test.dll" () As String

When I call the function, it returns an error message of "memory cannot be

but, when I declare the function as integer in both C++ and VB, it works.
So the point is, what data type should I declare in VB in order to get
connected with the C++ "char*" data type.

Thanks a lot!

Question by:wjacky

Expert Comment

ID: 8013794
the fact that your returning a constant string might be different to what im going to say but basically it's not good to return a char array and if you do that data has to be copied to a new variable or array. the fact that your going from char* to vb string is also different then what im saying but in c++ terms you cant return a char * and then try to use that pointer..it doesnt exist.
if in your function you create an array
char somearray[50] = {"whatever"};
then return somearray;
if you return that into a char pointer then you MIGHT get data that says "whatever" but that would only be by chance and only because the memory hasn't been overwritten yet.
once the function ends the memory space that was used for somearray is deallocated, not erased or zeroed out but just deallocated. so when the function returns and you try to use that pointer, your trying to access memory thats no longer allocated and youy'll get messed up results. bottom line, best thing for you to do is void the function and take it in vb as a sub or return int for some kind of fail/success indicator if needed, and try to make vb pass a string by referemce. I'm sure it'll work
so in your c++ function have it be
int abc(char *vbstring)
  return 1;
then to import into vb
declare function abc(byref vbstring as string) as integer
byref tells vb to pass as reference and im sure that'll work.

Accepted Solution

BorlandMan earned 200 total points
ID: 8013816

What you are returning (char*) from your C function is probably not the way to go. It could be dangerous.. I think it's referred to as a "dangling pointer".. something like that.. I'd go with passing in an initialized string and letting your dll manipulate that and return that back to the caller - and basically the caller is responsible for the memory passed in.

If you look at some of the Windows API's, like GetUserName(), you'll see they use the return value for success indication or number of bytes read, but not typically the data.. that's just my .02 there. I imagine other people's opinions may differ, but I feel this is a safer approach... saves me headaches..

I would use a pointer to zero terminated string as a parameter ex:  

int Doit (char* psz, int iBuffLen)
   strncpy(psz, "Test", strlen("Test"));
   return strlen("Test"); // maybe return length read?

and on the vb side use string

on the vb side something like this:

' Assign the buffer size constant to lpUserName.
dim lpnLength as integer
dim lpUserName as string

lpnLength= 255
lpUserName = Space$(lpnLength + 1)

Public Declare Function DoIt Lib "Bogus.dll" _
  (ByVal lpBuffer As String, nSize As Long) As Long      

Something like that.

Hope that helps,

Author Comment

ID: 8014060
Very clear explanation!
Thanks everyone!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

572 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