473,986 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cpp function to return a string

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(i nt 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(1 0) & 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
3 2845
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
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(i nt 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3710
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
8
1426
by: Johno | last post by:
I have written the two associated base classes below (Digital_camera and Review) to manage digital camera and review objects. They are base classes for which other derived classes can be written to provide more detail. You'll notice that I've also declared the display functions as "virtual" to allow for polymorphism. I now need to write a function to display all the contents of an STL "deque" of pointers to Digital_camera objects. It needs...
1
3567
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
2
6351
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the winamp API will only return a pointer to the position of the asked path. An article available on http://msmvps.com/ch21st/archive/2004/02/26.aspx provides a VB6 implementation of this, using the ReadProcessMemory Windows API command, but something...
4
7712
by: Paul | last post by:
Anyone have code that emulates the Nz function in Microsoft Access? In Access it is: Nz(Value as variant, Optional ValueIfNull as Variant) as Variant
6
7624
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
4
162771
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: session_set_save_handler(array(&$sess_handler, 'open'), array(&$sess_handler, 'close'), array(&$sess_handler, 'read'), array(&$sess_handler, 'write'), array(&$sess_handler, 'destroy'), array(&$sess_handler, 'gc'));
4
1758
by: default | last post by:
Hello All. I was looking around for a function like strtok() which would tokenize on the complete list of delimiters, rather than tokenize on *any* of the delimiters in the group. I ended up just rolling a function. Thought I would post it here for discussion. Thanks.
27
3155
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
20
9042
by: Andrew Morton | last post by:
Is it possible to have two function declarations which take the same parameters but return different types depending on how the function is used? function f(x) as string ' return a string end function function f(x) as string() ' return an array of strings end function
0
10204
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.