By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Cpp function to return a string

P: n/a
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?
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Norm Cook wrote:
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
Jul 23 '05 #2

P: n/a
Norm Cook wrote:
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.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand

Jul 23 '05 #3

P: n/a
Phlip wrote:
[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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.