473,837 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning arrays and Objects in C++

I have two questions which are very similar:
Is it possible to return an object in C++. Below is part of my code
for reference however I am more concerned about the concept. It seems
like the function below is returning a pointer to pointers who are
GUID. I am trying to write a wrapper to use in my VB code and what I
would prefer to do is be able to return an array of GUID. I remember
(not sure) that the concept of arrays does not really exist in c++ and
they are all pointers however, i do not want to pass a pointer to the
global IdsOfJobs i would like to pass the array itself. So is it
possible to return an array of objects. I would like to be able to
change the function definition to

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs).
Thank you for your time. Also my second question is in regards to
creating COM+ components using VC++. Is there any easy to follow
tutorial? I have my classes written and working as a WIN32 Console
application. Having difficulties when I try to Create an instance of
the DLL object from VB.
Thanks a bunch.

Gent

Code below for reference

-------------------------------------------------
-------------------------------------------------
Declared inside the class AdminBits
Class AdminBits {
.....
IBackgroundCopy Manager* g_XferManager;
HRESULT hr;
GUID** IdsOfJobs;

.....
};

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs)
{
// GUID *tempIDs;
// GUID tempIDs[100];
GUID temp;
int i = 0;

// If passed in 1 than enumerates jobs for all users
// If passed in 0 than it enumerates job for current user logged on.
IEnumBackground CopyJobs* pJobs = NULL;
IBackgroundCopy Job* pJob = NULL;
ULONG cJobCount = 0;
ULONG idx = 0;

//Specify the appropriate COM threading model for your application.
hr = CoInitializeEx( NULL, COINIT_APARTMEN TTHREADED);
if (SUCCEEDED(hr))
{
//The impersonation level must be at least
RPC_C_IMP_LEVEL _IMPERSONATE.
hr = CoInitializeSec urity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEV EL_CONNECT,
RPC_C_IMP_LEVEL _IMPERSONATE,
NULL, EOAC_NONE, 0);
if (SUCCEEDED(hr))
{
hr = CoCreateInstanc e(__uuidof(Back groundCopyManag er), NULL,
CLSCTX_LOCAL_SE RVER,
__uuidof(IBackg roundCopyManage r),
(void**) &g_XferManager) ;
//Enumerate jobs in the queue. This example enumerates all jobs in
the transfer
//queue. This call fails if the current user does not have
administrator
//privileges. To enumerate jobs for only the current user, replace
//BG_JOB_ENUM_ALL _USERS with 0.

if(UserSelectio n == 1)
{
hr = g_XferManager->EnumJobs(BG_JO B_ENUM_ALL_USER S, &pJobs);
}
else
{
hr = g_XferManager->EnumJobs(0, &pJobs);
}
if (SUCCEEDED(hr))
{
//Get the count of jobs in the queue.
pJobs->GetCount(&cJob Count);

// IdsOfJobs= new GUID*[cJobCount+1];

NumberOfJobs = cJobCount;
//Enumerate the jobs in the queue.
for (idx=0; idx<cJobCount; idx++)
{
hr = pJobs->Next(1, &pJob, NULL);
if (S_OK == hr)
{
pJob->GetId(&temp) ;
IdsOfJobs[i] = &temp;
// tempIDs[i] = temp;
i++;
//Retrieve or set job properties.

pJob->Release();
pJob = NULL;
}
else
{
//Handle error
break;
}
}
}
}
}

if (g_XferManager)
{
g_XferManager->Release();
g_XferManager = NULL;
}

CoUninitialize( );
return IdsOfJobs;

}
Jul 22 '05 #1
5 3119
Gent wrote:
I have two questions which are very similar:
Is it possible to return an object in C++.
Most certainly.
Below is part of my code
for reference however I am more concerned about the concept. It seems
like the function below is returning a pointer to pointers who are
GUID.
That's right.
I am trying to write a wrapper to use in my VB code and what I
would prefer to do is be able to return an array of GUID.
It is impossible to return an array from a function.
I remember
(not sure) that the concept of arrays does not really exist in c++ and
they are all pointers however, i do not want to pass a pointer to the
global IdsOfJobs i would like to pass the array itself. So is it
possible to return an array of objects. I would like to be able to
change the function definition to

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs).
But you did, didn't you? And you're still returning a pointer to
a pointer to GUID, and not an array.
Thank you for your time. Also my second question is in regards to
creating COM+ components using VC++. Is there any easy to follow
tutorial? I have my classes written and working as a WIN32 Console
application. Having difficulties when I try to Create an instance of
the DLL object from VB.
That is OT here. Please find a better NG to ask your COM+ question.
Code below for reference

-------------------------------------------------
-------------------------------------------------
Declared inside the class AdminBits
Class AdminBits {
I believe you mean

class AdminBits {

C++ is case-sensitive, and there is no keyword "Class" in it.
.....
IBackgroundCopy Manager* g_XferManager;
HRESULT hr;
GUID** IdsOfJobs;
A data member which is a pointer to a pointer.

.....
};

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs)
{
GUID temp;
An automatic object of type GUID. This object will go away
[...]
//Enumerate the jobs in the queue.
for (idx=0; idx<cJobCount; idx++)
{
[...]
IdsOfJobs[i] = &temp;
You're accessing the i-th element of an [imaginary] array pointed to
by the 'IdsOfJobs' pointer. Does the array exist? Where is that
array created? That's unknown and you don't show it in your code.
Second, no less drastic, error is taking (and storing) an address of
an automatic object. 'temp' _shall_ be destroyed as soon as this
member function returns the control to its caller. That means that
the pointers you stored in that [imaginary] array are not valid any
longer (besides the fact that they all are the same).
[...]
}
}
}
}
[...]
return IdsOfJobs;

}


I don't know how VB deals with connecting to C++ programs (this is
not the right place to discuss it), but you might be better off just
passing an empty array of the right size to the function and expect
the function to fill it.

Victor
Jul 22 '05 #2

"Gent" <ge***********@ trustasc.com> wrote in message
news:1b******** *************** ***@posting.goo gle.com...
I have two questions which are very similar:
Is it possible to return an object in C++.
Yes.
Below is part of my code
for reference however I am more concerned about the concept. It seems
like the function below is returning a pointer to pointers who are
GUID. I am trying to write a wrapper to use in my VB code and what I
would prefer to do is be able to return an array of GUID.
Arrays are not objects, it is not possible to return arrays in C++.
I remember
(not sure) that the concept of arrays does not really exist in c++ and
they are all pointers however,
That is a badly confused understanding of a tricky concept. Arrays certainly
exist in C++ but they are somewhat limited and pointers are sometimes used
to get round some of those limitations. In particular you cannot return an
array in C++ but you can return a pointer which points to the first element
of an array.
i do not want to pass a pointer to the
global IdsOfJobs i would like to pass the array itself.
You cannot pass arrays either, but you can pass a pointer which points to
the first element of an array.
So is it
possible to return an array of objects. I would like to be able to
change the function definition to
It's not possible to return an array in C++.

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs).
That is a function returning a pointer not an array. Its a perfectly legal
declaration.

I think you need to revise pointers and arrays and the similarities and
differences. Any book on C or C++ will cover this. In particular you seem to
be confusing functions which return a pointer, which happens to point to the
first element of an array, (legal) with a functions which return an array
(illegal), they are not the same thing at all.

Thank you for your time. Also my second question is in regards to
creating COM+ components using VC++. Is there any easy to follow
tutorial? I have my classes written and working as a WIN32 Console
application. Having difficulties when I try to Create an instance of
the DLL object from VB.
Thanks a bunch.


COM+ is not on topic in this group.

john


Jul 22 '05 #3
On Wed, 15 Sep 2004 22:46:39 +0100, "John Harrison"
<jo************ *@hotmail.com> wrote in comp.lang.c++:

"Gent" <ge***********@ trustasc.com> wrote in message
news:1b******** *************** ***@posting.goo gle.com...
I have two questions which are very similar:
Is it possible to return an object in C++.


Yes.
Below is part of my code
for reference however I am more concerned about the concept. It seems
like the function below is returning a pointer to pointers who are
GUID. I am trying to write a wrapper to use in my VB code and what I
would prefer to do is be able to return an array of GUID.


Arrays are not objects, it is not possible to return arrays in C++.


Arrays are so objects in C++, although it is not possible to return
bare arrays.

========
[intro.object] 1.8 The C++ object model

1 The constructs in a C++ program create, destroy, refer to, access,
and manipulate objects. An object is a region of storage. [Note: A
function is not an object, regardless of whether or not it occupies
storage in the way that objects do. ] An object is created by a
definition (3.1), by a new expression (5.3.4) or by the implementation
(12.2) when needed. The properties of an object are determined when
the object is created. An object can have a name (clause 3). An object
has a storage duration (3.7) which influences its lifetime (3.8). An
object has a type (3.9). The term object type refers to the type with
which the object is created.
========

While bare arrays cannot be passed to, or returned from, functions by
value, encapsulated arrays can.

struct encapsulated_ar ray
{
int array [10];
};

encapsulated_ar ray some_func(encap sulated_array);

Perfectly valid.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #4
Thank you for your reply Victor. I actually like the idea of passing
an empty array to the function and have the function fill it. I will
try it today. However I am not sure how to resize an array in C++. I
might not know the size of the array before I pass it to the function.
Is it possible to do that? If not I will break down the function in
two pieces. One the finds out the number of elements (size of array)
and the other one that gets the info.
I plan on changing the function declaration to
bool AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs, GUID&
IdSofJobs[])
{
// how can i resize the IdSofJobs inside here. I can calculate the
NumberOfJobs and I wantet that to be of size [NumberOfJobs+1]
}
Thanks,

Gent
I don't know how VB deals with connecting to C++ programs (this is
not the right place to discuss it), but you might be better off just
passing an empty array of the right size to the function and expect
the function to fill it.

Victor
Victor Bazarov <v.********@com Acast.net> wrote in message news:<fy******* ********@newsre ad1.dllstx09.us .to.verio.net>. .. Gent wrote:
I have two questions which are very similar:
Is it possible to return an object in C++.


Most certainly.
> Below is part of my code
for reference however I am more concerned about the concept. It seems
like the function below is returning a pointer to pointers who are
GUID.


That's right.
> I am trying to write a wrapper to use in my VB code and what I
would prefer to do is be able to return an array of GUID.


It is impossible to return an array from a function.
> I remember
(not sure) that the concept of arrays does not really exist in c++ and
they are all pointers however, i do not want to pass a pointer to the
global IdsOfJobs i would like to pass the array itself. So is it
possible to return an array of objects. I would like to be able to
change the function definition to

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs).


But you did, didn't you? And you're still returning a pointer to
a pointer to GUID, and not an array.
Thank you for your time. Also my second question is in regards to
creating COM+ components using VC++. Is there any easy to follow
tutorial? I have my classes written and working as a WIN32 Console
application. Having difficulties when I try to Create an instance of
the DLL object from VB.


That is OT here. Please find a better NG to ask your COM+ question.
Code below for reference

-------------------------------------------------
-------------------------------------------------
Declared inside the class AdminBits
Class AdminBits {


I believe you mean

class AdminBits {

C++ is case-sensitive, and there is no keyword "Class" in it.
.....
IBackgroundCopy Manager* g_XferManager;
HRESULT hr;
GUID** IdsOfJobs;


A data member which is a pointer to a pointer.

.....
};

GUID** AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs)
{
GUID temp;


An automatic object of type GUID. This object will go away
[...]
//Enumerate the jobs in the queue.
for (idx=0; idx<cJobCount; idx++)
{
> [...]
IdsOfJobs[i] = &temp;


You're accessing the i-th element of an [imaginary] array pointed to
by the 'IdsOfJobs' pointer. Does the array exist? Where is that
array created? That's unknown and you don't show it in your code.
Second, no less drastic, error is taking (and storing) an address of
an automatic object. 'temp' _shall_ be destroyed as soon as this
member function returns the control to its caller. That means that
the pointers you stored in that [imaginary] array are not valid any
longer (besides the fact that they all are the same).
> [...]
}
}
}
}
[...]
return IdsOfJobs;

}


I don't know how VB deals with connecting to C++ programs (this is
not the right place to discuss it), but you might be better off just
passing an empty array of the right size to the function and expect
the function to fill it.

Victor

Jul 22 '05 #5
Gent wrote:
Thank you for your reply Victor. I actually like the idea of passing
an empty array to the function and have the function fill it. I will
try it today. However I am not sure how to resize an array in C++. I
might not know the size of the array before I pass it to the function.
Is it possible to do that? If not I will break down the function in
two pieces. One the finds out the number of elements (size of array)
and the other one that gets the info.
I plan on changing the function declaration to
bool AdminBits::GetJ obIDs(int UserSelection, int& NumberOfJobs, GUID&
IdSofJobs[])
No, that's not going to work. There is no array of references. You will
still need a pointer, but the outside function should pass an array to
'GetJobIDs':

bool ... , GUID *IDsOfJobs);
....
GUID IDsOfJobs[100];
....
blah.GetJobIDs( ..., IDsOfJobs);
{
// how can i resize the IdSofJobs inside here. I can calculate the
NumberOfJobs and I wantet that to be of size [NumberOfJobs+1]
}
The usual way is to return the necessary number of jobs if the pointer
passed is NULL (IDsOfJobs == NULL). See DeviceCapabilit ies Windows SDK
function as an example: if its fourth argument is NULL, it returns the
number of bytes required. Yes, it would require two calls, but short of
allocating memory in the function itself, that's all you can do usually
to get the array sized properly.

Another way, of course, is to give it an array sized definitely larger
than it might ever be necessary. Yet another way is to tell the function
how big the array is and ask only for as many Job IDs as there are array
elements. Yet another way is to actually implement a Job ID _enumeration_
procedure and organize your interaction that way (see EnumDesktopWind ows
function in Windows SDK for an example of using a callback).


Thanks,

Gent

[...]


V
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7307
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic array e.g: array *a; a = function(...)
41
3836
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x = rand(); y = rand();
5
1923
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
9
2445
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for example: sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate object and
3
2706
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
19
2504
by: Robert Smith | last post by:
I am wondering why it is possible to return a pointer to a string literal (ie. 1) but not an array that has been explicitly allocated. (ie. 2) ? Both would be allocated on the stack, why does the first one not cause a compiler warning? #include <stdio.h> char * funca() { char *a = "blah"; //1 - ok // char a = "blah"; //2 - not ok
41
4996
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in the hash are alphabetically sorted if the key happens to be alpha numeric. Which I believe makes sense because it allows for fast lookup of a key.
4
1949
by: =?Utf-8?B?a2lzaG9y?= | last post by:
Hi, has any one used webservices for returning custom objects other than datasets like custom classes and their internal classes ?. What problems you have faced if any ? is there any limitation ? what are advantages and disadvantages.. please let me know Kishor
8
1797
by: jodleren | last post by:
Hi! I have a function, a part of my code which I can use as a function. It will return 2 arrays, and I am wondering what way to do so. Both arrays hold strings, there are no special keys. 1) setting the arrays as globals 2) returnin an array of arrays 3) returning a large array with a known marker to indicate when the 2nd part starts.
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
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
10875
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...
0
10272
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...
0
9401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5669
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
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
2
4040
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.