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

x
?
Solved

Passing a string reference from VB to C++

Posted on 2003-03-06
4
Medium Priority
?
329 Views
Last Modified: 2011-04-14
Hello,

I want to passe a string reference out of VB to C++

Public Declare Sub SubmitAddress Lib "ProtA.dll" (Byval Filename as
string, ByRef output as string)

In C++ I need to read the contents of a file and put them in the
string

void _stdcall SubmitAddress(char *Filename, char *Output)
{ string s;


    ifstream in(FileName);
    while(getline(in,line))
       {
         s += line;
       }
....

}

what I need is that the address takes the reference of the Output so that s[0] = Output

Thanks for your help
0
Comment
Question by:kteetaert
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8082566
If you pass the string by reference, you have to use

void _stdcall SubmitAddress(char *Filename, char **Output)
{ string s;


   ifstream in(FileName);
   while(getline(in,line))
      {
        s += line;
      }

strcpy ( *Output, s.c_str());

}

See also http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B187912 ("HOWTO: Pass a String Between Visual Basic and Your C DLL")
0
 

Author Comment

by:kteetaert
ID: 8083397

Hi,

Thanks for the answer but it isn't working on that way!
When I executing the program and want to load a file it give an " acces violation error".

Mayby it's my computer but on the strcpy(*Output,s_cstr()); is gives the terminating error.


0
 
LVL 12

Accepted Solution

by:
Salte earned 150 total points
ID: 8087759
I think it is an error to use char ** str for the byref string.

String data type is already a reference type so for the C++ side the 'byref' doesn't do anything. The types should possibly be:

char * for both.

One problem I have with this is that this assumes that VB string is the same as "char *" in C/C++. That is most likely not the case and so you have problems.

Check your VB documentation and see what it says about how string is implemented internally. Especially how you transfer it as parameter to DLL's.

Most likely a byval string can work as const char * since VB then can pass the string's pointer.

A ByRef string is a whole other matter and is most likely NOT char ** ptr, that would allow you to change the pointer to the string and that is not a smart thing to do. This pointer is most likely maintained by VB and shouldn't be changed.

One thing, if you have byref I assume your C/C++ function is supposed to modify the string.

I think your original parameters is possibly correct. There's only one problem. The string as given by VB has a certain size and you can't arbitarily change that size on the C side. I don't know how VB internally handle strings but they are probably closer to CString or std::string in nature (although probably not exactly those types) than a plain C style 'char *'. This means that the string type itself handles allocation and deallocation of buffer space for the strings and so you cannot just change that size in your C code.


I would assume that any of the following ways would get around these problems:

1. do some research I am pretty sure that whatever type VB uses for strings is also available in C code. After all, the VB engine itself is written in C or C++ by microsoft. One possible alternative is BSTR.

If it is BSTR, then declare your byref type to be BSTR or BSTR * and use it.

If it happened to be transferred to the DLL as a regular char, then you must also be wary of how much buffer space is available. You cannot copy your string data to the Output beyond the buffer space available. If there is anyway you can allocate more space (if VB engine export a function to manipulate strings from C, there should be such a function available) and then the C code can simply call that function to enlarge the Output string as needed.

In any case, as far as I know - byval strings in VB are transferred as 'const char *' to DLL's just so that C and C++ can easily handle them. How ByRef strings are transferred I really don't know, but some research should reveal it soon enough.

I also believe that the VB engine exports functions to manipulate VB strings so that a C or C++ function called from VB can call those functions to allocate room for strings etc.

However, you should consider to change the function interface:

Public Declare function SubmitAddress Lib "ProtA.dll" (Byval Filename as string) as string

Then in C++:

char * _stdcall SubmitAddress(char *Filename, char *Output)
{
   static string s; // make it static so it's not on stack.


   ifstream in(FileName);
   s = ""; // if called several times...
   while(getline(in,line))
      {
        s += line;
      }
....

   return s.c_str();
}

This would probably make life easier for you.

Alf
0
 
LVL 12

Expert Comment

by:Salte
ID: 8087767
err..sorry, make that return type:

const char * __stdcall .... etc...

otherwise C++ will complain about it cannot convert a const char * to char *.

Alf
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

564 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