?
Solved

Passing a string reference from VB to C++

Posted on 2003-03-06
4
Medium Priority
?
323 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
[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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

764 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