[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I define two dimentional SAFEARRAY?

Posted on 2001-09-11
14
Medium Priority
?
354 Views
Last Modified: 2007-12-19
How can I define two dimentional SAFEARRAY of type integer?
0
Comment
Question by:MohdAsalah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +3
14 Comments
 

Expert Comment

by:markvp
ID: 6473943
Could you be more specific about what you're trying to do? Do you simply want to define a two dimensional array or define a type? Are the dimensions known at compile time?

Normally you define it as

     int a[10][10];
     a[5][6] = 14;

You can also typedef it for convenience:

     typedef int safearray[10][10];
     safearray a;
     a[5][6] = 14;

If the dimensions are not known at compile time, it's more difficult.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6474020
I assume you're talking COM.  Well, you use SafeAffayCreate (look in MSDN library).  There's an example here
http://support.microsoft.com/support/kb/articles/Q182/8/31.ASP
(using VT_VARIANT - you'll have to use VT_UI1 for ints).

Have a look here, too:
http://community.borland.com/article/0,1410,22016,00.html

you should have something like this.
<altered version of that MSDN link>
SAFEARRAY FAR* psa;
SAFEARRAYBOUND rgsabound[2];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 3;
rgsabound[1].lLbound = 0;
rgsabound[1].cElements = 3;
psa = SafeArrayCreate(VT_UI1, 2, rgsabound);
0
 
LVL 2

Expert Comment

by:danelroisman
ID: 6474087
//like int arr[8][4];

HRESULT hres;
SAFEARRAY * psaiNew;
SAFEARRAYBOUND aDim[2];

aDim[0].lLbound = 0;
aDim[0].cElements = 7;
aDim[1].lLbound = 0;
aDim[1].cElements = 3;

psaiNew = SafeArrayCreate(VT_I2, 2, aDim);
if (psaiNew == NULL) throw ERROR_NOT_ENOUGH_MEMORY;
     
//use array ....
//.....


//destroy!
if (hres = SafeArrayDestroy(psaiNew)) throw hres;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MohdAsalah
ID: 6474101
That's fine but how can I fill this array ,I mean how can I use SafeArrayPutElement API function in this two dimentional array.
0
 

Author Comment

by:MohdAsalah
ID: 6474120
That's fine but how can I fill this array ,I mean how can I use SafeArrayPutElement API function in this two dimentional array.
0
 
LVL 2

Expert Comment

by:danelroisman
ID: 6474123
//like int arr[8][4];

HRESULT hres;
SAFEARRAY * psaiNew;
SAFEARRAYBOUND aDim[2];

aDim[0].lLbound = 0;
aDim[0].cElements = 7;
aDim[1].lLbound = 0;
aDim[1].cElements = 3;

psaiNew = SafeArrayCreate(VT_I2, 2, aDim);
if (psaiNew == NULL) throw ERROR_NOT_ENOUGH_MEMORY;
     
//use array ....
//.....


//destroy!
if (hres = SafeArrayDestroy(psaiNew)) throw hres;
0
 
LVL 2

Expert Comment

by:danelroisman
ID: 6474145
for(long i =0; i < 4; i++)
{
for(long j=0; j < 8; j++)
{
int  someVal = 4; //some value
//j means dimension              
::SafeArrayPutElement(psaiNew,&j, &someVal);
}
}
0
 
LVL 2

Accepted Solution

by:
danelroisman earned 40 total points
ID: 6474154
Sorrry, the previows example was realy bad. :)

This example populates the safearray elements with double values. (Using MFC)

VARIANT _stdcall retVariantArray(void)
{
  COleSafeArray saRet;        
  DWORD numElements[] = {10, 10}; // 10x10

  // Create the two dimensional safe-array of type VT_R8 with size 10x10.
  saRet.Create(VT_R8, 2, numElements);

  // Initialize safearray with values...
  long index[2];
  for(index[0]=0; index[0]<10; index[0]++)
  {
    for(index[1]=0; index[1]<10; index[1]++)
     {
      double val = index[0] + index[1]*10;
      saRet.PutElement(index, &val);
    }
  }
  // Return the safe-array encapsulated in a VARIANT...
  return saRet.Detach();
}

0
 
LVL 4

Expert Comment

by:IainHere
ID: 6474194
>>How can I define two dimentional SAFEARRAY of type integer?

... solution ...


>> That's fine but how can I fill this array?

Normally this would be a different question, especially if the original question is 5 times easier than an easy question.  Please don't take this the wrong way - I'm just giving my opinion.
0
 

Author Comment

by:MohdAsalah
ID: 6474233
This Comment to lainHere:

If the first question is 5 times easier than an easy question why u do not complete ur answer.
and please keep your opinion to yourself.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6474504
Interesting.  As you will have been advised when you asked the question, 50 points = easy question; 100 points = moderate question; 200 points = difficult question.

>> If the first question is 5 times easier than an easy question
I was referring to the number of points you put up.  10 points = 1/5 of easy question.

>> why u do not complete ur answer.
I did.  You will notice that the SafeArrayCreate function call I made did what you asked in your original question.

>>keep your opinion to yourself.
>>Please don't take this the wrong way
:-)
0
 

Author Comment

by:MohdAsalah
ID: 6474570
That is very silly,I do not put 10 points because this <comment edited by Community Support> question is very easy but because I just have 10 points.
<comment edited by Community Support> .
0
 
LVL 16

Expert Comment

by:Kyle Schroeder
ID: 6476902
where is "the" hell?
0
 

Expert Comment

by:Lab1
ID: 6476991
MohdAsalah We do not allow profanity or flaming here at EE. Please keep your comments professional.
Thank you
Lab1
CS Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question