Cpp function to return a string

N

Norm Cook

Hope there are some mixed language programmers here. Trying to write a c++
dll for my VB6 program. To help me with my understanding, I just want to
pass it
a VB long (c++ int) & have it return that number of characters as a string,
any character. I know about the def file, using stdcall etc. I envision it
as
something like

LPCSTR _stdcall ReturnAString(int NumChars)
but not sure about LPCSTR

and the VB Declare would be something like

Private Declare Function ReturnAString Lib "MyDLL" (ByVal NumChars As Long)
As String

In VB,
Dim A As String
A = ReturnAString(10) & get A = "AAAAAAAAAA"

I suspect the c++ function has to allocate the string with one of
the SysAlloc family of calls. Any ideas?
 
V

Victor Bazarov

Norm said:
Hope there are some mixed language programmers here. Trying to write a c++
dll for my VB6 program. [...]

I suspect the c++ function has to allocate the string with one of
the SysAlloc family of calls. Any ideas?

You need to ask either in a Visual C++ newsgroup or in a Visual Basic
newsgroup or a Windows programming newsgroup. _Standard_ C++ language
(the sole subject of _this_ newsgroup) does not have any specific means
for mixed language programming except with C. All others are compiler-
specific.

V
 
P

Phlip

Norm said:
Hope there are some mixed language programmers here.

I am a very mixed programmer.
Trying to write a c++
dll for my VB6 program.

Start again, and this time create a New ATL ActiveX DLL project.

Use the class wizard to create a class in this project. Give it a method
Foo, with these arguments:

[out, retval] BSTR *

Inside that method, use (IIRC) SysAllocString to turn a local string into a
BSTR.

The deal is strings are arrays in memory, so they can't copy around easily.
But different programs in a protected mode architecture have different
memory locations. So the OS supports a "string handle" type, which is BSTR.
LPCSTR _stdcall ReturnAString(int NumChars)
but not sure about LPCSTR
Private Declare Function ReturnAString Lib "MyDLL" (ByVal NumChars As Long)
As String

That won't work because the VB String object _is_ a BSTR. A Basic STRing.
You might eventually figure out how to coerce your string pointer into a
BSTR (and your string is in-process, so it's in the same address space). But
you might as well learn to do things the correct way. Only legacy DLLs
should use the Declare Function system. Any new code should start with
ActiveX.

All of this topic is best served by Microsoft's newsgroups. This newsgroup
is only qualified to discuss raw C++ itself.
 
V

Victor Bazarov

Phlip said:
[OT response to an OT question removed]

Please, kindly supply at least some indication that you're posting
off-topic. I would really like avoid downloading those. "[OT]" in the
subject would do nicely. Thanks.
All of this topic is best served by Microsoft's newsgroups. This newsgroup
is only qualified to discuss raw C++ itself.


V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top