473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing an array of LPSTR to C++ by reference

Below is a straightforward C++ question but i'm giving example code to
demonstrate the issue i'm having. THe issue might seem to be out of
the topic but it's mostly related to C++ and is not necessarily
environment specific.

I have written a VB program that passes an array of strings to a C++
dll. The c++ dll is supposed to fill the array of strings with the
appropriate value. The only way i figured out how to pass was it by
reference. I create the array and populate with blank spaces in VB
then pass it be reference to C++ dll.

In my C++ example the only values we are concerned about are LPSTR &
strReturnedJobI d. Now what happens is that the only value i populate
is the first element of the VB array. It would be a lot easier if i
could declare the header on the C++ function as long __stdcall
GetJobIds(LPSTR & strReturnedJobI d[NUMBER_OF_ELEME NT], int
numberOfJobs, int UserSelection)
but apparently you cannot put a & in front of an array declaration.

My question is how do i pass an array of strings by value (from VB) to
a C++ (well this has to be by reference so C++ can fill up the array)
dll. I have no problem solving that if i change the data type from
string to int or so. but as strings do not exist in C++ then i'm
stuck.

Thank you for your help!

Gent


vb 6 code:
my call fails:

Public Declare Function GetJobIds Lib "AdminBits. dll" _
(ByRef strReturnedJobI d As String, ByVal intNumberOfJobs As
Integer, ByVal intUserSelectio n As Integer) As Integer

Private Sub GetBITSJobsGUID s(ByVal UserSelection As Integer)
Dim numberOfJobs As Integer
numberOfJobs = CountJobs(UserS election)

Dim LoopArr As Long
Dim arrayOfGUIDs() As String

ReDim arrayOfGUIDs(nu mberOfJobs - 1) As String

For LoopArr = 0 To numberOfJobs - 1
arrayOfGUIDs(Lo opArr) = Space(100)

Next LoopArr
Dim intflag As Integer
'''Attention
intflag = GetJobIds(array OfGUIDs(0), numberOfJobs,
UserSelection)
End Sub
c++ code:

long __stdcall GetJobIds(LPSTR & strReturnedJobI d, int numberOfJobs,
int UserSelection)
{
USES_CONVERSION ; //Use Atlconv.h macros
wchar_t sbuff[100];
LPOLESTR tempstrReturned JobId = sbuff;
GUID* ReturnedJobId = NULL;

int retvalue = -1;
// If passed in 1 than enumerates jobs for all users
// If passed in 0 than it enumerates job for current user logged on.
// Function returns the number of JOBS or returns -1 if it failed to
count the jobs
IEnumBackground CopyJobs* pJobs = NULL;
IBackgroundCopy Job* pJob = NULL;
ULONG cJobCount = 0;
ULONG idx = 0;

IBackgroundCopy Manager* g_XferManager = NULL;
HRESULT hr;

//Specify the appropriate COM threading model for your application.
hr = CoInitializeEx( NULL, COINIT_APARTMEN TTHREADED);
if (SUCCEEDED(hr))
{
retvalue = -2;

hr = CoCreateInstanc e(__uuidof(Back groundCopyManag er), NULL,
CLSCTX_LOCAL_SE RVER,
__uuidof(IBackg roundCopyManage r),
(void**) &g_XferManager) ;
if (SUCCEEDED(hr))
{
//failed to enumerate the jobs in the queue.

retvalue = -3;
//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))
{
retvalue = -4; // Could not get the job count

//Get the count of jobs in the queue.
pJobs->GetCount(&cJob Count);

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

//Enumerate the jobs in the queue.
for (idx=0; idx<cJobCount; idx++)
{
hr = pJobs->Next(1, &pJob, NULL);
if (S_OK == hr)
{
retvalue = 5; //could not retrieve the job properties such as
ID
//Retrieve or set job properties.
GUID JobId;

pJob->GetId(&JobId );

int abc = StringFromGUID2 (JobId, tempstrReturned JobId, 100);
char *buffer = W2A(tempstrRetu rnedJobId);

if (40 > strlen(buffer) )
{
//***********ATTE NTION HERE *************** *****
strcpy(&strRetu rnedJobId[idx], buffer);
//*************en d attention *********
retvalue = 1; //successfully got the job IDs
}

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

pJobs->Release();
pJobs = NULL;
}
}
}

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

CoUninitialize( );
return retvalue;

}
Jul 22 '05 #1
4 4879
Gent wrote:
[...]
My question is how do i pass an array of strings by value (from VB) to
a C++ (well this has to be by reference so C++ can fill up the array)
dll. I have no problem solving that if i change the data type from
string to int or so. but as strings do not exist in C++ then i'm
stuck. [...]


Please ask in a VB newsgroup or in a Microsoft C++ newsgroup. Here it
is OT because standard C++ has no definition of "passing from VB".

V
Jul 22 '05 #2

"Gent" <ge***********@ trustasc.com> wrote in message
news:1b******** *************** ***@posting.goo gle.com...
Below is a straightforward C++ question but i'm giving example code to
demonstrate the issue i'm having. THe issue might seem to be out of
the topic but it's mostly related to C++ and is not necessarily
environment specific.

I have written a VB program that passes an array of strings to a C++
dll. The c++ dll is supposed to fill the array of strings with the
appropriate value. The only way i figured out how to pass was it by
reference. I create the array and populate with blank spaces in VB
then pass it be reference to C++ dll.

In my C++ example the only values we are concerned about are LPSTR &
strReturnedJobI d. Now what happens is that the only value i populate
is the first element of the VB array. It would be a lot easier if i
could declare the header on the C++ function as long __stdcall
GetJobIds(LPSTR & strReturnedJobI d[NUMBER_OF_ELEME NT], int
numberOfJobs, int UserSelection)
but apparently you cannot put a & in front of an array declaration.


Says who?

long GetJobIds(LPSTR (& strReturnedJobI d)[NUMBER_OF_ELEME NT], int
numberOfJobs, int UserSelection)

is a prefectly valid declaration. strReturnedJobI d is a reference to an
array of size NUMBER_OF_ELEME NT. Whether this is what you need I have no
idea.

john
Jul 22 '05 #3
I changed declaration to long __stdcall GetJobIds(LPSTR
(&strReturnedJo bId)[100], int numberOfJobs, int UserSelection). It
compiles fine however when i call it from VB the same way as previous
post but i get the error message below:

The instruction at "0x0f473c1" referenced memory at "0x20202002 0". The
memory could not be written
The instruction at "0x6a9ecd39 " referenced memory at "0x0000002c ". The
memory could not be read. It bombs at the line where it's trying to do
strcpy(strRetur nedJobId[idx], buffer);

I am not sure why I would get that error. Can anyone please tell me
what i'm doing wrong.

Full code is below.

Thanks

Gent Metaj

long __stdcall GetJobIds(LPSTR (&strReturnedJo bId)[100], int
numberOfJobs, int UserSelection)
{
USES_CONVERSION ; //Use Atlconv.h macros
wchar_t sbuff[100];
LPOLESTR tempstrReturned JobId = sbuff;
GUID* ReturnedJobId = NULL;

int retvalue = -1;
// If passed in 1 than enumerates jobs for all users
// If passed in 0 than it enumerates job for current user logged on.
// Function returns the number of JOBS or returns -1 if it failed to
count the jobs
IEnumBackground CopyJobs* pJobs = NULL;
IBackgroundCopy Job* pJob = NULL;
ULONG cJobCount = 0;
ULONG idx = 0;

IBackgroundCopy Manager* g_XferManager = NULL;
HRESULT hr;

//Specify the appropriate COM threading model for your application.
hr = CoInitializeEx( NULL, COINIT_APARTMEN TTHREADED);
if (SUCCEEDED(hr))
{
retvalue = -2;

hr = CoCreateInstanc e(__uuidof(Back groundCopyManag er), NULL,
CLSCTX_LOCAL_SE RVER,
__uuidof(IBackg roundCopyManage r),
(void**) &g_XferManager) ;
if (SUCCEEDED(hr))
{
//failed to enumerate the jobs in the queue.

retvalue = -3;
//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))
{
retvalue = -4; // Could not get the job count

//Get the count of jobs in the queue.
pJobs->GetCount(&cJob Count);

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

//Enumerate the jobs in the queue.
for (idx=0; idx<cJobCount; idx++)
{
hr = pJobs->Next(1, &pJob, NULL);
if (S_OK == hr)
{
retvalue = 5; //could not retrieve the job properties such as
ID
//Retrieve or set job properties.
GUID JobId;

pJob->GetId(&JobId );

int abc = StringFromGUID2 (JobId, tempstrReturned JobId, 100);
char *buffer = W2A(tempstrRetu rnedJobId);

if (40 > strlen(buffer) )
{

strcpy(strRetur nedJobId[idx], buffer);
retvalue = 1; //successfully got the job IDs
}

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

pJobs->Release();
pJobs = NULL;
}
}
}

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

CoUninitialize( );
return retvalue;

}
"John Harrison" <jo************ *@hotmail.com> wrote in message news:<2u******* ******@uni-berlin.de>...
"Gent" <ge***********@ trustasc.com> wrote in message
news:1b******** *************** ***@posting.goo gle.com...
Below is a straightforward C++ question but i'm giving example code to
demonstrate the issue i'm having. THe issue might seem to be out of
the topic but it's mostly related to C++ and is not necessarily
environment specific.

I have written a VB program that passes an array of strings to a C++
dll. The c++ dll is supposed to fill the array of strings with the
appropriate value. The only way i figured out how to pass was it by
reference. I create the array and populate with blank spaces in VB
then pass it be reference to C++ dll.

In my C++ example the only values we are concerned about are LPSTR &
strReturnedJobI d. Now what happens is that the only value i populate
is the first element of the VB array. It would be a lot easier if i
could declare the header on the C++ function as long __stdcall
GetJobIds(LPSTR & strReturnedJobI d[NUMBER_OF_ELEME NT], int
numberOfJobs, int UserSelection)
but apparently you cannot put a & in front of an array declaration.


Says who?

long GetJobIds(LPSTR (& strReturnedJobI d)[NUMBER_OF_ELEME NT], int
numberOfJobs, int UserSelection)

is a prefectly valid declaration. strReturnedJobI d is a reference to an
array of size NUMBER_OF_ELEME NT. Whether this is what you need I have no
idea.

john

Jul 22 '05 #4

"Gent" <ge***********@ trustasc.com> wrote in message
news:1b******** *************** **@posting.goog le.com...
I changed declaration to long __stdcall GetJobIds(LPSTR
(&strReturnedJo bId)[100], int numberOfJobs, int UserSelection). It
compiles fine however when i call it from VB the same way as previous
post but i get the error message below:

The instruction at "0x0f473c1" referenced memory at "0x20202002 0". The
memory could not be written
The instruction at "0x6a9ecd39 " referenced memory at "0x0000002c ". The
memory could not be read. It bombs at the line where it's trying to do
strcpy(strRetur nedJobId[idx], buffer);

I am not sure why I would get that error. Can anyone please tell me
what i'm doing wrong.


The interaction of VB with C++ is not something I know anything about,
neither is it on topic in this group. I just corrected a misconception you
had about C++. You might have better luck in a VB group.

John
Jul 22 '05 #5

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

Similar topics

3
14920
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
16824
by: Mick | last post by:
Does anyone know how to pass an array of structures to a DLL? Here's what I'm doing... the VB.Net error is at the end. *** C++ Structure Declaration: typedef struct _SOME_STRUCT { char *pId; char *pBuffer;
58
10093
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
5
2270
by: Mircea Dragan | last post by:
Hi, I have a problem. Here is the description: I have a dll which I call from a C# program. The C# program passes an array of doubles to the dll function. Everything works fine as long as I don't change any array element in the dll function.
3
2893
by: dbru | last post by:
I need to pass an address of a Managed float array to a DLL. The following doesn't seem to work extern static float GetXXX( StringBuilder HWND, long nWhat, ref float lparam );
11
8106
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub...
1
1166
by: jg | last post by:
I have searched marshaling articles from MSDN and Google but I am stuck with solving my problem to marshal arrays to COM caller from my managed COM class I built a .net COM class in VB and tested it from an application built with C tool.. It worked well except for those functions in the VB com class with arrays. I had no trouble with...
3
2421
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I thought passing by reference, your passing the address in memory. public bool DoSomething(ref byte data) { byte retVal = null; try
4
8098
by: robert.waters | last post by:
I have to parse a binary file having a number of fixed records, each record containing datas in fixed positions. I would like to parse this binary file into an array of structures having members that represent those fields, so that I can access the records in a meaningful way. Using C, I would have defined a struct that I could cast a byte...
0
7839
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...
0
8200
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. ...
1
7954
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...
1
5710
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...
0
5390
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...
0
3836
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...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
0
1179
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...

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.