?
Solved

writing DLL library in VC++ problem

Posted on 2003-02-24
3
Medium Priority
?
187 Views
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
read"

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!
Jacky



0
Comment
Question by:wjacky
[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
3 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
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)
{
  strcpy(vbstring,"testing");
  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.
0
 
LVL 2

Accepted Solution

by:
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,
Sincerely,
J
0
 

Author Comment

by:wjacky
ID: 8014060
Very clear explanation!
Thanks everyone!
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

770 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