473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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::GetJobIDs(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 {
.....
IBackgroundCopyManager* g_XferManager;
HRESULT hr;
GUID** IdsOfJobs;

.....
};

GUID** AdminBits::GetJobIDs(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.
IEnumBackgroundCopyJobs* pJobs = NULL;
IBackgroundCopyJob* pJob = NULL;
ULONG cJobCount = 0;
ULONG idx = 0;

//Specify the appropriate COM threading model for your application.
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (SUCCEEDED(hr))
{
//The impersonation level must be at least
RPC_C_IMP_LEVEL_IMPERSONATE.
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(__uuidof(BackgroundCopyManager), NULL,
CLSCTX_LOCAL_SERVER,
__uuidof(IBackgroundCopyManager),
(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(UserSelection == 1)
{
hr = g_XferManager->EnumJobs(BG_JOB_ENUM_ALL_USERS, &pJobs);
}
else
{
hr = g_XferManager->EnumJobs(0, &pJobs);
}
if (SUCCEEDED(hr))
{
//Get the count of jobs in the queue.
pJobs->GetCount(&cJobCount);

// 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 3025
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::GetJobIDs(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.
.....
IBackgroundCopyManager* g_XferManager;
HRESULT hr;
GUID** IdsOfJobs;
A data member which is a pointer to a pointer.

.....
};

GUID** AdminBits::GetJobIDs(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.google.c om...
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::GetJobIDs(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.google.c om...
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_array
{
int array [10];
};

encapsulated_array some_func(encapsulated_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.learn.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::GetJobIDs(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.********@comAcast.net> wrote in message news:<fy***************@newsread1.dllstx09.us.to.v erio.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::GetJobIDs(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.
.....
IBackgroundCopyManager* g_XferManager;
HRESULT hr;
GUID** IdsOfJobs;


A data member which is a pointer to a pointer.

.....
};

GUID** AdminBits::GetJobIDs(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::GetJobIDs(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 DeviceCapabilities 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 EnumDesktopWindows
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
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...
41
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...
5
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
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,...
3
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...
19
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...
41
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...
4
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...
8
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.